比赛地址
http://172.21.40.250/web/contest/show/cid/129
(仅支持校园网访问)密码rjkflx
1.输出字符画
这一题没什么好说的,对于Java来说,直接复制粘贴就可以。
public class Main {
public static void main(String[] args) {
System.out.println(" ___ _ _ __ ___ _ __ __ \r\n" +
" | _ \\ _ | | | |/ / | __| | | \\ \\/ /\r\n" +
" | / | | | | | ' < | _| | |__ > < \r\n" +
" | _|_\\ _\\__/ |_|\\_\\ _|_|_ |____| /_/\\_\\");
}
}
2.数列求值
这是一道类斐波那契的题目,一般都会使用数组来做,也可以不用数组,但一定要记得把取余步骤放到循环里面,否则会溢出
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=1,b=1,c=1,d = 0,i,n;
n=sc.nextInt();
for(i=4;i<=n;i++){
d=(a+b+c)%10000;
a=b;b=c;c=d;
}
System.out.println(d);
}
}
3.方格计数
这道题主要还是数学问题,我们只取第一象限的数据。最终结果乘四即可
这道题的主要思路在于,如果一个方格的右上方顶点在圆内(第一象限),那么这个正方形一定就在圆内。对每个点遍历即可。
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n--!=0) {
int R = sc.nextInt();
int count = 0;
for (int i = 1; i < R; i++) {
for (int j = 1; j < R; j++) {
if (i * i + j * j <= R * R)
count++;
}
}
System.out.println(4*count);
}}
}
4.特别数字
wanghang的代码先拿过来用用,暴力题目直接判断就是了。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanf=new Scanner(System.in);
int n=scanf.nextInt();
int sum=0;
for(int i=1; i<=n; i++){
int g,b,s,q;
g=i%10;
b=i/10%10;
s=i/100%10;
q=i/1000;
if( q==1 || q==2 || q==9 || g==2 || g==0 || g==1 || g==9 || b==2 || b==1 || b==9 || s==2 || s==1 ||s==9 )
sum+=i;
else if(b==0){
if(i>=100)
sum+=i;
}
else if(s==0){
if(i>1000)
sum+=i;
}
}
System.out.println(sum);
}
}
5.特别数的和
mashiji的代码同样被我拿过来用了
这道题和上题一样,也是暴力判断,不过Java可以用indexof来判断是否包含。会比C语言实现容易一点(Java有额外的判题时间)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k, sum = 0;
for (int i = 1; i < n; i++) {
if ((i + "").indexOf("2") != -1 || (i + "").indexOf("4") != -1)
continue;
for (int j = i + 1; j < n; j++) {
if ((j + "").indexOf("2") != -1 || (j + "").indexOf("4") != -1)
continue;
k = n - i - j;
if (i == j || i == k || j == k)
continue;
if (k > 0 && (k + "").indexOf("2") == -1 && (k + "").indexOf("4") == -1)
sum++;
}
}
System.out.println(sum / 3);
}
}
6.交换瓶子
这道题有点小分歧,可能是我的测试数据有问题,所以暂时把较大的测试数据删除,这题只需要把序号遍历,第N位上瓶子的编号不为N就让它与编号N的瓶子进行交换。
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int n;
Scanner scanf = new Scanner(System.in);
n = scanf.nextInt();
int a[] = new int[10005];
for (int i = 1; i <= n; i++) {
a[i] = scanf.nextInt();
}
int t, num = 0;
for (int i = 1; i <=n; i++) {
if (a[i] == i)
continue;
t = a[i];
a[i] = a[t];
a[t] = t;
num++;
}
System.out.println(num);
}
}
7.摔手机
这道题主要是思路上的问题,暂且不贴代码,以后还会有这种类似的题目
8.相关性分析
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
int K = input.nextInt();
input.nextLine();
String text = input.nextLine();
String[] words = text.split("\\s+|\\.");
int[] wordsLength = new int[words.length];
for (int i = 0; i < words.length; i++) {
wordsLength[i] = words[i].length();
}
int num = 0;
for (int i = 0; i < words.length; i++) {
if (words[i].equals("Alice")) {
for (int j = i + 1; j < words.length; j++) {
int sum = 1;
if (words[j].equals("Bob")) {
for (int k = i + 1; k < j; k++) {
sum += wordsLength[k] + 1;
}
if (sum <= K) {
num++;
}
}
}
}
}
for (int i = 0; i < words.length; i++) {
if (words[i].equals("Bob")) {
for (int j = i + 1; j < words.length; j++) {
int sum = 1;
if (words[j].equals("Alice")) {
for (int k = i + 1; k < j; k++) {
sum += wordsLength[k] + 1;
}
if (sum <= K) {
num++;
}
}
}
}
}
System.out.println(num);
} catch (Exception e) {
input.close();
}
}
}
9.字符子串
这题算是个口算题,直接输出即可,当然也是可以代码实现的
public class Main {
public static void main(String[] args) {
System.out.println(100);
}
}
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
String s1 = "aaab";
String s2 = "0100110001010001";
System.out.println(subSum(s1));
System.out.println(subSum(s2));
}
public static int subSum(String s) {
Set<String> set = new HashSet<String>();
for(int i = 0; i < s.length(); i++) {
for(int j = i ; j < s.length(); j++) {
set.add(s.substring(i, j + 1 ));
}
}
return set.size();
}
}