第四届“传智杯”全国大学生IT技能大赛(练习赛)java/c++AC代码与个人理解(更新中)


A题:
听了直播讲解后……我才明白这题是纯纯暴力啊,我还以为要用string判断再转回int啥啥啥的……
这份代码是C++的

#include 
using namespace std;

int main(){
    int n, k;
    scanf("%d%d", &n, &k);
    int index, i;
    int num = 0;    //判断3的个数
    
    // //如果n为偶数的话肯定不满足题意的
    // //为后面的循环降低一点循环次数
    // if(n % 2 == 0){
    //     n++;
    // }
    
    for(i = n; ; i++){
        index = i;  //暂存现在循环到的这个数
        //对index做拆分,不影响i的值
        while(index != 0){
            //当index此时取余得到改数末尾的数字为3时,num++
            if(index % 10 == 3){
                num++;
            }
            index /= 10; //除以十进入下次循环用来判断
        }
        //当index = 0时,就证明i被拆分完了,
        //判断一下此时的num和k是否相等
        //相等就代表我们找到答案了
        if(num == k){
            break;
        }
        //num置为0
		num = 0;
    }
    printf("%d", i);
    return 0;
}

这是java的代码,原理其实是一样的。

import java.util.Scanner;

public class Main {
	//我还不会除了Scanner外的输入方式,所以只能用这个了
	static Scanner input;
	public static void main(String[] args) {
		input = new Scanner(System.in);
		
		int n = input.nextInt();
		int k = input.nextInt();
		int i = n;
		int index = 0;
		int num = 0;
		
		//这个循环是一直直到找到答案为止的,所以是死循环
		while(true) {
			index = i;
			
			while(index != 0) {
				if(index % 10 == 3) {
					num++;
				}
				index /= 10;
			}
			if(num == k) {
				break;
			}
			//不是for循环,所以需要重新赋值的条件往后放
			num = 0;
			i++;
		}
		//出了循环就代表答案出来了
		System.out.println(i);
	}
}

B题:
C++的代码

#include 
//纯纯暴力啊,我还以为要用string判断再转回int啥啥啥的……
using namespace std;

const int N = 105;
int a[N]; 

int main(){
	int n, id;
	scanf("%d", &n);
	//a数组纯粹是判断某个1-100的数字是不是已经输入过了
	//为了防止初始值有问题,所以赋值一下
	//为了省空间的话用bool数组是最好的 
	memset(a, 0, sizeof(a));
	for(int i = 0; i < n; i++){
		scanf("%d", &id);
		//如果id已经输入过了就continue
		//如果id没有输入就输出 并且 把a[id]赋值为1 
		if(!a[id]){
			//如果已经输入了,进不来这个条件
			 printf("%d ", id);
			 a[id] = 1; 
		}
	} 
    return 0;
}

java代码。
正好我最近学到了集合,所以用的集合写的。
这是比赛时的源码,所以没有注释。

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	private static Scanner input;
	
	public static void main(String[] args) {
		input = new Scanner(System.in);
		int n = input.nextInt();
		ArrayList id = new ArrayList();
		for(int i = 0; i < n; i++) {
			int index = input.nextInt();
			if(id.contains(index)) {
				continue;
			}else {
				id.add(index);
			}
		}
		for (Integer i : id) {
			System.out.print(i + " ");
		}
	}
}

C题:
C++代码,是比赛上交源码所以没有注释

#include 

using namespace std;

struct student{
    string name;
    int num;
    int totalScore;
}s[105];

bool cmp(student m, student n){
    if(m.totalScore == n.totalScore){
        return m.num < n.num;
    }
    return m.totalScore > n.totalScore;
}

int main(){
    int n;  //学生数
    scanf("%d", &n);
    int scoreOne, scoreTwo;
    for(int i = 0; i < n; i++){
        cin >> s[i].name >> scoreOne >> scoreTwo;
        s[i].num = i;
        s[i].totalScore = (int)round(0.4 * scoreOne + 0.6 * ceil((sqrt(scoreTwo) * 10)));
    }
    sort(s, s + n, cmp);
    for(int i = 0; i < n; i++){
        cout << s[i].name << " " << (s[i].totalScore) << endl;
    }
    return 0;
}

java的代码用上了Arrays包,Comparator包。
不熟悉的话就写着很麻烦……

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	static Scanner input;
	
	public static final int N = 105;
	
	static class Student{
		public String name = "";
		public int id = 0;
		public int score = 0;
	}
	
	static class cmp implements Comparator{
		//重写接口中的方法
		public int compare(Student s1, Student s2) {
			//我们希望达到的要求是 按成绩降序排列
			//成绩相等的情况下 先输入的排前面
			/**
			 * 背下来叭,我一下子也没绕清楚
			 * 如果要按照升序排序 则o1 小于o2 返回-1(负数) 相等返回0 01大于02返回+1(正数)
			 * 如果要按照降序排序 则o1 小于o2 返回+1(正数) 相等返回0 01大于02返回-1(负数)
			 */
			if(s1.score == s2.score) {
				//不像C++是返回一个boolean类型了
//				return s1.id < s2.id;
				if(s1.id < s2.id) {
					return -1;
				}
				return 1;
			}else if(s1.score > s2.score) {
				return -1;
			}else {
				return 1;
			}
		}
	}
	
	public static void main(String[] args) {
		input = new Scanner(System.in);
		//定义一个Student类型的数组
		Student[] student = new Student[N];
		
		double score1 = 0, score2 = 0;	//平时成绩和期末成绩
		int n = input.nextInt();
		for(int i = 0; i < n; i++) {
			student[i] = new Student();	//这个地方容易漏
			
			student[i].name = input.next();
			student[i].id = i;
			score1 = input.nextDouble();
			score2 = input.nextDouble();
			
			score2 = (int)Math.ceil((Math.sqrt(score2) * 10));
			student[i].score = (int) Math.round(0.4 * score1 + 0.6 * score2);
		}
		//用四个参数的
		//1.需要排序的数组 2.起始位置 3.结束位置 4.排序规则
		//注意静态不能调用非静态 所以写的内部类都要为静态的
		Arrays.sort(student, 0, n, new cmp());
		
		for (int i = 0; i < n; i++) {
			System.out.println(student[i].name + " " + student[i].score);
		}
	}
}

D题:
C++的代码,也是纯纯暴力……
巧方法应该是用map,键值对,但是我不会doge

#include 

using namespace std;

const int N = 105;
string que[N];
string ans[N];

int main(){
	int n, q;	//题库的题目数量和试卷上的题目数量 
	string qus, a, b, c, d;
	int qusNum;
	scanf("%d%d", &n, &q);
	for(int i = 0; i < n; i++){
		cin >> que[i] >> ans[i];
	}
	for(int i = 0; i < q; i++){
		cin >> qus >> a >> b >> c >> d;
		//找到题库里该题目的题号
		for(int j = 0; j < n; j++){
			if(qus == que[j]){
				qusNum = j;
				break;
			}
		}

		if(a == ans[qusNum]){
            puts("A");
		}
		if(b == ans[qusNum]){
			puts("B");
		}
		if(c == ans[qusNum]){
			puts("C");
		}
		if(d == ans[qusNum]){
			puts("D");
		}
	}
	
    return 0;
}

java代码,和C++的代码思维没有区别,就是注意equals方法就ok

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	static Scanner input;
	public static final int N = 105;
	public static void main(String[] args) {
		input = new Scanner(System.in);
		
		//不会键值对,所以还是开两个数组叭
		String[] qus = new String[N];
		String[] ans = new String[N];
		String index, a, b, c, d;
		int qusNum = 0;
		
		int n = input.nextInt();
		int q = input.nextInt();
		
		for(int i = 0; i < n; i++) {
			qus[i] = input.next();
			ans[i] = input.next();
		}
		for (int i = 0; i < q; i++) {
			index = input.next();
			a = input.next();
			b = input.next();
			c = input.next();
			d = input.next();
			for(int j = 0; j < n; j++) {
				//如果我没记错的话,equals里面最好放保证有值的,不然有可能出问题
				if(qus[j].equals(index)) {
					qusNum = j;
				}
			}
			if(ans[qusNum].equals(a)) {
				System.out.println("A");
			}
			if(ans[qusNum].equals(b)) {
				System.out.println("B");
			}
			if(ans[qusNum].equals(c)) {
				System.out.println("C");
			}
			if(ans[qusNum].equals(d)) {
				System.out.println("D");
			}
		}
		
	}	
}


E题:

相关