CSP往届认证试题练习(JAVA)

202109-1(数组推导)

import java.util.Scanner;
public class Main {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] A = new int[n];
        int[] B = new int[n];
        for (int i = 0; i < n;i++){
            B[i] = scanner.nextInt();
        }
        int sum_max=0, sum_min = 0, max = 0;
        for (int i = 0; i < n; i++){
            sum_max = sum_max + B[i];
        }
        System.out.println(sum_max);
        for (int i = 0; i < n; i++){
            if(max < B[i]){
                max = B[i];
                sum_min = sum_min + B[i];
            }
        }
        System.out.println(sum_min);
    }
}

202004-1(灰度图)

import java.util.Scanner;

public class Main {
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		int k;
		int m = scanner.nextInt();
		int n = scanner.nextInt();
		int L = scanner.nextInt();
		int[] h = new int[256];
		for(int i = 0; i < n ; i++){
			for(int j = 0; j < m; j++){
				k = scanner.nextInt();
				h[k]++;
			}
		}
		for(int i = 0; i < L ; i++){
			System.out.print(h[i] + " ");
		}
	}
}

 202012-1(期末预测之安全指数)

import java.util.Scanner;
public class Main{
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int ReLU = 0;
		int w, score;
		for(int i = 0; i < n; i++){
			w = scanner.nextInt();
			score = scanner.nextInt();
			ReLU = w * score + ReLU;
		}
		if(ReLU > 0){
			System.out.println(ReLU);
		}else{
			System.out.println(0);
		}
	}
}

202009-1(称检测点查询)

import java.util.Scanner;

public class Main{
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int X = scanner.nextInt();
		int Y = scanner.nextInt();
		int D, min, k = 0;
		int[] h = new int[n];
		for(int i = 0; i < n; i++){
			int x = scanner.nextInt();
			int y = scanner.nextInt();
			D = (X - x) *  (X - x) + (Y - y) * (Y - y);
			h[i]=D;
		}
		for(int i = 0; i < 3; i++){
			k = 0;
			min = h[0];
			for(int j = 1; j < n; j++){
				if(h[j] < min){
					min = h[j];
					k = j;
				}
			}
			System.out.println(k + 1);
			h[k] = 9999999;
		}
	}
}

201912-1(报数)

import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int i = 1, a = 0, b = 0, c = 0, d = 0, p = 1;
        while(p <= n){
            if(i % 7 == 0 || i % 10 == 7 || i / 10 == 7 || i / 100 ==7 ||(i % 100) / 10 == 7){
                if(i % 4 == 1){
                    a++;
                }
                else if(i % 4 == 2){
                    b++;
                }
                else if(i % 4 == 3){
                    c++;
                }
                else if(i % 4 == 0){
                    d++;
                }
            }
            else{
                p++;
            }
            i++;
        }
//        System.out.println(p+a+b+c+d);
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

201909-1(小明种苹果)

import java.util.Scanner;

public class Main{
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		int M = scanner.nextInt();
		int T = 0, K = 0, k_i = 0, k_isum = 0, i_1 = 0;
		int[][] A= new int[N][M+1];
		for(int i = 0; i < N; i++){
			for(int j = 0; j < M+1; j++){
				A[i][j] = scanner.nextInt();
			}
		}
		for(int i = 0; i < N; i++){
			for(int j = 0; j < M+1; j++){
				T += A[i][j];
			}
		}
		System.out.print(T + " ");
		for(int i = 0; i < N; i++){
			k_isum = 0;
			for(int j = 1; j < M+1; j++){
				k_isum = A[i][j] + k_isum;
				i_1 = i;
			}
			if(k_isum<0){
				k_isum = -k_isum;
			}
			if(k_isum > K){
				K = k_isum;
				k_i = i_1;
			}
		}
		System.out.print(k_i+1 + " " + K);
	}
}

201903-1(小中大)

import java.util.Scanner;

public class Main{
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		float[] A = new float[n];
		for(int i = 0; i < n; i++){
			A[i] = scanner.nextInt();
		}
		if(A[0] > A[n-1]){
			int a = (int) A[0];
			System.out.print(a + " ");
		}
		else{
			int a = (int) A[n-1];
			System.out.print(a + " ");
		}
		if(n % 2 == 1){
			int a = (int) A[n / 2];
			System.out.print(a + " ");
		}
		else{
			float b = (A[n / 2 - 1] + A[n / 2]) / 2;
			int c = (int) b;
			if(b == c){
				System.out.print(c + " ");
			}
			else{
				System.out.printf("%.1f", b);
				System.out.print(" ");
			}
		}
		if(A[0] > A[n-1]){
			int a = (int) A[n-1];
			System.out.print(a);
		}
		else{
			int a = (int) A[0];
			System.out.print(a);
		}
	}
}

201812-1(小明上学)

import java.util.Scanner;

public class Main{
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		long r = scanner.nextLong();
		long y = scanner.nextLong();
		long g = scanner.nextLong();
		int n = scanner.nextInt();
		Long[][] A = new Long[n][2];
		long s = 0;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < 2; j++){
				A[i][j] = scanner.nextLong();;
			}
		}
		for(int i = 0; i < n; i++){
			if(A[i][0] == 0){
				s += A[i][1];
			}
			else if(A[i][0] == 1){
				s += A[i][1];
			}
			else if(A[i][0] == 2){
				s = s + A[i][1] + r;
			}
			else{
				continue;
			}
		}
		System.out.println(s);
	}
}

201809-1(卖菜)

import java.util.Scanner;

public class Main{
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] A = new int[n];
		int[] B = new int[n];
		for(int i = 0; i < n ; i++){
			A[i] = scanner.nextInt();
		}
		for(int i = 0; i < n ; i++){
			if(i == 0){
				B[i] =(A[i] + A[i + 1]) / 2;
			}
			else if(i == n - 1){
				B[i] =(A[i] + A[i - 1]) / 2;
			}
			else{
				B[i] = (A[i + 1] + A[i] + A[i - 1]) / 3;
			}
		}
		for(int i = 0; i < n ; i++){
			System.out.print(B[i] + " ");
		}
	}
}

201803-1(跳一跳)

201712-1(最小差值)

import java.util.Scanner;

public class Main{
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] A = new int[n];
		int min = 999;
		for(int i = 0; i < n; i++){
			A[i] = scanner.nextInt();
		}
		for(int i = 0; i < n-1; ++i){
			for(int j = i + 1; j < n; ++j){
				if(A[j] > A[i]){
					int t;
					 t = A[j];
					 A[j] = A[i];
					 A[i] = t;
				}
			}
		}
		for(int i = 0; i < n - 1; i++){
			if(min > (A[i] - A[i+1])){
				min = A[i] - A[i+1];
			}
		}
		System.out.println(min);
	}
}

201709-1(打酱油)

import java.util.Scanner;

public class Main{
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		int i_5 = 0, i_3 = 0, i_1 = 0, free = 0, sum = 0;
		if(N >= 50){
			i_5 = N / 50;
			free = i_5 * 2;
			sum = sum + i_5 * 5 + free;
			N = N - i_5 * 50;
		}
		if(N >= 30){
			i_3 = N / 30;
			free = i_3 * 1;
			sum = sum + i_3 * 3 + free;
			N = N - i_3 * 30;
		}
		if(N >= 10){
			i_1 = N / 10;
			sum = sum + i_1;
		}
		System.out.println(sum);
	}
}

201703-1(分蛋糕)

import java.util.Scanner;

public class Main{
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int k = scanner.nextInt();
		int[] A = new int[n];
		int sum = 0, p = 0, j = 0, z = 0;
		for(int i = 0; i < n; i++){
			A[i] = scanner.nextInt();
		}
		for(int i = 0; i < n; i++){
			sum = sum +A[i];
			if(sum >= k){
				p++;
				sum = 0;
				if(i == n -1){
					z = 1;
				}
			}
			if(i == n - 1 && z == 0){
				p++;
			}
		}
		System.out.println(p);
	}
}

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值