题目1
- 统计从1到N的整数中,所有立方值的平方根为整数的数的格式
输入说明:整数N(N<10000)
输出说明:符合条件的数的个数,如4^3= 64 = 8^2
输入样例:10
输出样例:3
(说明:样例中符合条件的3个数是1、4、9)
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int count = 0;
for(int i = 1;i<= N;i++){
double value = Math.pow(Math.pow(i.3),0.5);
if((int) value == value){
count++;
}
}
System.out.println(count);
}
}
题目2
- 给出长度N的各不相同整数组成的数组,求解2个数相加为M的情况个数
输入说明:第一行,数组中元素个数N(N<1000),和值M;第二行,N个数组元素
输出样例:8 10
1 4 2 5 3 19 8 6
输出样例:2
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int target = sc.nextInt();
int[] array = new int[m];
sc.nextLine();
for (int i = 0; i < m; i++) {
array[i] = sc.nextInt();
}
int count = 0;
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < m; j++) {
if (array[i] + array[j] == target){
count++;
}
}
}
System.out.println(count);
}
}
题目3
- 在一个由小写英文字母(a-z)组成的字符串中,查找最长子串,其头尾字母相同,且中间不包含该头尾字母,并输出最左边的该类子串。
输入说明:待处理字串(长度≤ 200)
输出说明:子串
输入样例:adfasjdoiasldhfa
输出样例:fasjdoiasldhlf
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String result = "";
for (int i = 0; i < str.length(); i++) {
int start = i;
int end = str.length() - 1;
while (start < end) {
if (str.charAt(start) == str.charAt(end)) {
String newStr = str.substring(start, end+1);
String substr = newStr.substring(1, newStr.length()-1);
if (substr.contains(String.valueOf(newStr.charAt(0)))) {
start++;
break;
} else {
result = result.length() > newStr.length() ? result : newStr;
break;
}
} else {
end--;
}
}
}
System.out.println(result);
}
}
题目4
- 某饮料店有两种奶茶饮料,其中一种每瓶含奶粉15g含茶粉5g,另一种每瓶含奶粉10g含茶粉10g。设某天饮料店消耗的奶粉和茶粉重量分别为x和y(单位g),求当天饮料店两种饮料的销量。
输入说明:奶粉和茶粉的消耗量(均为整数)
输出说明:两种饮料各自销量(无解则输出:-1)
输入样例:400 300
输出样例:10 25
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int M = (x- y)/10;
int N = (3*y-x)/20;
if(M<0){
System.out.println("-1");
}else{
System.out.print(M + " ");
}
if(N<0){
System.out.println("-1");
}else{
System.out.println(N);
}
}
}
去年题目的解答:2019年全国高校计算机能力挑战赛初赛java语言解答
全国高校计算机能力挑战赛大赛官网:计算机能力挑战赛