第一道题是数1到2020有几个’2’
直接遍历 答案:624
public class Main {
public static void main(String[] args)
{
int count = 0;
for (int i = 1; i <=2020; i++) {
String s = String.valueOf(i);
if (s.contains("2")) {
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j)=='2') {
count++;
}
}
}
}
System.out.println(count); //624
}
}
第二题 找2020
直接遍历
我是手动复制把数组测出数组是300*300,再来编程
import java.util.Scanner;
public class Main {
public static int count(int x,int y,char[][] num) {
int count = 0;
if(y + 3 < 300)
if( num[y+1][x] == '0' && num[y+2][x] == '2' && num[y+3][x] == '0')
count++;
if(x + 3 < 300)
if( num[y][x+1] == '0' && num[y][x+2] == '2' && num[y][x+3] == '0')
count++;
if(y + 3 < 300 && x + 3 < 300)
if( num[y+1][x+1] == '0' && num[y+2][x+2] == '2' && num[y+3][x+3] == '0')
count++;
return count;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
char[][]num = new char[300][300];
for (int i = 0; i < 300; i++) {
String string = in.next();
for (int j = 0; j < 300; j++) {
num[i][j] = string.charAt(j) ;
}
}
int count = 0;
for (int i = 0; i < 300; i++) {
for (int j = 0; j < 300; j++) {
count+=count(j, i, num);
}
}
System.out.println(count);
}
}
第三题
找规律题
在草稿纸上写一遍就会发现,每一斜行是x个数累加。
容易得出第二十行是39个数累加。
public class Main {
public static void main(String[] args)
{
int count = 0;
for (int i = 1; i <= 39; i++) {
for (int j = 1; j <= i; j++) {
count++;
System.out.print(count+" ");
}
System.out.println();
}
}
}
可以数出最后一行第20个(中间)数是761;
同时,因为看前几个数很眼熟:
1,5,13,25……
然后就找到规律,第n个数是n的平方加(n-1)的平方。
202+192 = 761.
第四题,数符号
乍一看很复杂。
其实穷举就只是从
C71+C72…+C77再排除。
从一笔到七笔,分别是
7+10+14+18+19+7+1=76
(不是很确定,欢迎指正)
第五题 冒泡排序
基础知识不牢固,通过找规律发现至少应该有15个字母。
14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
此时次数为105
按字典序排,应该保证第一个数最小
即
9 14 13 12 11 10 8 7 6 5 4 3 2 1 0
翻译为字符即可(char)(97+num[i])
第六题
int max = -1;
int min = 101;
for(){
}
double avg = sum/n;
syso
syso
system.out printf("%.2f",avg);
因为平时使用的时候是可以自动四舍五入的,但是不知道java1.6会不会自动四舍五入QAQ。
第七题
计算单词中频率最高的字符,
因为要按字典序,所以逆序遍历
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String s = in.next();
int []c = new int[26];
for (int i = 0; i < s.length(); i++) {
c[s.charAt(i)-97]++;
}
int maxV = 0;
int maxK = 0;
for(int i = 25;i>=0;i--){
if (c[i]>=maxV) {
maxK = i;
maxV = c[i];
}
}
System.out.println((char)(maxK+97));
System.out.println(maxV);
}
}
第八题
之前做过类似的动态规划题,从最后一行开始,选出最大的数加到上一行上。这题多了个条件,向左和向右的次数相差不超过一,一时想不出来,就用了dfs
添加了一个条件Math.abs(left-right)>level/2时return;
在边界条件添加Math.abs(left-right)<=1
第九题
hashmap 真是个神器
直接遍历,估计会超时,但是想想和我能想到的dp似乎差不多
for(……)
for(){
if(!hashmap.contains(string.charAt(j)))
hashmap.put……
sum+=hashmap.size();
}
第十题
输入完数据,开始做判断的时候,感觉答案遥不可及
直接交卷去吃饭了