第二届全国高校计算机能力挑战赛-Java程序设计赛
16题
题目:统计从1到N的整数中,所有立方值的平方根为整数的数的个数
输入说明:整数N(N<10000);
输出说明:符合条件的数的个数,如43=64=82
输入样例:10
输出样例:3
(说明:样例中符合条件的3个数是1、4、9)
import java.util.Scanner;
/*
* 统计从1到N的整数中,所有立方值的平方根为整数的数的个数
* 输入说明:整数N(N<10000)
* 输出说明:符合条件的数的个数,如4^3=64=8^2
* 输入样例:10
* 输出样例:3
* (说明:样例中符合条件的三个数是1、4、9)*/
public class Number_16 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入N(N<10000):");
int N = sc.nextInt();
int count =0;
for (int i = 1; i < N+1; i++) {
int temp = (int) Math.sqrt(i*i*i);
int Value = temp*temp ;
int realValue = i*i*i;
if (realValue==Value)
count+=1;
}
System.out.println(count);
}
}
17题
题目:数组a和b分别记录着球队A和B本赛季N场比赛的净胜球,其中正数为胜利积3分,负数为失败积0分,0为平局积1份,求解A和B的排名先后。如果积分相同则净胜球总数多则排名靠前。如果净胜球总数仍相同,则输出: Draw。
输入说明:第一行,数组中元素个数N(N<1000);第二行,A的净胜球;第三行,B的净胜球
输出说明:排名靠前球队(A或B或Draw)
输入样例:5
1 0 -1 0 1
0 6 0 0 -1
输出样例:A
import java.util.Scanner;
/*数组a和b分别记录着球队A和B本赛季N场比赛的净胜球,
其中正数为胜利积3分,负数为失败积0分,0为平局积1分,
求解A和B的排名先后。如果积分相同则净胜球数总数多则
排名靠前。如果净胜球总数仍相同,则输出:Draw
输入说明:第一行,数组中元素个数N(N<1000);第二行,A的净胜球;
第三行,B的净胜球
输出说明:排名靠前球队(A或B或Draw)
输入样例:
5
1 0 -1 0 1
0 6 0 0 -1
输出样例:A
* */
public class Number_17 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
int N =sc.nextInt();
String sa = sc2.nextLine();
String sb = sc2.nextLine();
int[] a = stringToIntArray(N,sa);
int[] b = stringToIntArray(N,sb);
int score_a = 0,score_b=0,count_a=0,count_b=0;
for (int i = 0; i < N; i++) {
if (a[i]>0)
score_a+=3;
else if (a[i]==0)
score_a+=1;
count_a+=a[i];
if (b[i]>0)
score_b+=3;
else if (b[i]==0)
score_b+=1;
count_b+=b[i];
}
if (score_a>score_b)
System.out.println("A");
else if (score_a<score_b)
System.out.println("B");
else
if (count_a>count_b)
System.out.println("A");
else if (count_a<count_b)
System.out.println("B");
else
System.out.println("Draw");
}
/**
* 将一行由空格分开的数字字符串转换为int数组
* @param N 数字个数
* @param s 待处理的字符串
* @return int[]
*/
public static int[] stringToIntArray(int N,String s){
int[] array = new int[N];
String sLeft = s.trim();
int firstIndex = s.indexOf(" ");
if (firstIndex==-1)
array[0] = Integer.parseInt(sLeft);
else{
array[0] = Integer.parseInt(s.substring(0,firstIndex));
sLeft = sLeft.substring(firstIndex+1);
for (int i = 1;i< array.length ; i++) {
firstIndex = sLeft.indexOf(" ");
if (firstIndex==-1)
array[i] = Integer.parseInt(sLeft);
else{
array[i] = Integer.parseInt(sLeft.substring(0,firstIndex));
sLeft = sLeft.substring(firstIndex+1);
}
}
}
return array;
}
}
18题
题目:在一个小写英文字母(a-z)组成的字符串的最短子串,其包含这个字符串中出现过的所有字母。输出最左边的该类子串。
输入说明:待处理字串(长度≤200)
输出说明:子串
输入样例:adfasjdoiasdfa
输出样例:fasjdoi
import java.util.*;
/*
* 题目:在一个小写英文字母(a-z)组成的字符串的
* 最短子串,其包含这个字符串中出现过的所有字母。
* 输出最左边的该类子串。
*
* 输出说明:待处理字串(长度<=200)
* 输出说明:子串
* 输入样例:adfasjdoiasdfa
* 输出样例:fasjdoi*/
public class Number_18 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] array =str.toCharArray();
Set<Character> s = new TreeSet<>();
for(char c : array) s.add(c);
char[] array2 = new char[s.size()];
int i=0;
for (Object c : s){
array2[i] = (char)c;
++i;
}
//以上是将待处理字符串总共出现的字母种类筛选出来并放在char数组array2中
int minLength = array2.length;
String ps = processedString(str,array2,minLength);
System.out.println(ps);
}
private static String processedString(String str, char[] array,int Length) {
if (Length==str.length()) return str;
for (int i = 0; i < (str.length()-Length+1); i++) {
String tempStr = str.substring(i,i+Length);
char[] tempChar = tempStr.toCharArray();
Set<Character> tempSet = new TreeSet<>();
for (char value : tempChar) tempSet.add(value);
int k = 0 ;
for (Object c : tempSet){
tempChar[k] = (char)c;
++k;
}
if (Arrays.equals(array,tempChar))
return tempStr;
}
return processedString(str, array, Length+1);
}
}
19题
题目:某商品有2种不同数量的包装,对应不同的价格;同时提供满200元减50元的不限量购物券,试求解最佳购买策略,在单次购买中以最低总价购买正好500个商品。
输入说明:两种包装的数量和价格(均为整数)
输出说明:两种商品各自购买数量(无解则输出:-1)
输入样例:100 80 200 150
输出样例:5 0
import java.util.Scanner;
import static Competition2020.Number_17.stringToIntArray;
/*某商品有2种不同数量的包装,对应不同的价格;
* 同时提供满200元减50的不限量购物券,试求解
* 最佳购买策略,在单次购买中以最低总价购买正
* 好500个商品。
*
* 输入说明:两种包装的数量和价格(均为整数)
* 输出说明:两种商品各自购买数量(无解则输出:-1)
* 输出样例:100 80 200 150
* 输出样例: 5 0 */
public class Number_19 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] array = (stringToIntArray(4,sc.nextLine()));
int amount_A = array[0],price_A = array[1],amount_B = array[2],price_B = array[3];
int finalPrice = -1;
int x=-1,y=-1;
for (int i = 0; i <=(500/amount_A)+1; i++)
for (int j = 0; j <=((500-i*amount_A)/amount_B+1) ; j++)
if (i*amount_A+j*amount_A == 500){
int tempPrice = i*price_A + j*price_B - ((i*price_A + j*price_B)/200)*50;
if (tempPrice<finalPrice || finalPrice ==-1){
finalPrice = tempPrice;
x=i;
y=j;
}
}
if (x==-1 || y==-1) System.out.println(-1);
else System.out.println(x + " " + y);
}
}
萌新第一次发帖,欢迎大家评论!