CCF部分机考题题目收集:
参照网上的题目和答案,自己也把这些题一个个敲代码实现了一遍,现记录下来,以防忘记。
样例输入
问题描述:
8
8 8 8 0 12 12 8 0
样例输出
5
样例说明
8 8 8是第一段,0是第二段,12 12是第三段,倒数第二个整数8是第四段,
最后一个0是第五段。 评测用例规模与约定
1 ≤ n ≤ 1000,0 ≤ ai ≤ 1000。
public class Question1 { public static void main(String[] args) { new Question1().run(); } public void run(){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); //整数的个数 int[] array = new int[n]; //用长度为n的整数数组来存储输入的整数 for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); //输入 } int count = 1; for (int i = 0; i < n - 1; i++) { if (array[i] == array[i + 1]) { }else { count += 1; } } System.out.println(count); scanner.close(); } }
public class Question2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int year = scanner.nextInt(); int days = scanner.nextInt(); int[] array = getOutput(year, days); System.out.println(array[0]+" "+array[1]); scanner.close(); } /** * 根据月份获取当月多少天 * @param month * @return */ public static int getDays(int year,int month){ boolean isLeapYear = false; if (year % 4 == 0 && year % 400 != 0) { isLeapYear = true; } int days = 0; switch (month) { case 1: days = 31; break; case 2: if (isLeapYear) { days = 29; }else { days = 28; } break; case 3: days = 31; break; case 4: days = 30; break; case 5: days = 31; break; case 6: days = 30; break; case 7: days = 31; break; case 8: days = 31; break; case 9: days = 30; break; case 10: days = 31; break; case 11: days = 30; break; case 12: days = 12; break; default: break; } return days; } /** * 获取输出(月份、天数)存入到int[] * @param year * @param days * @return */ public static int[] getOutput(int year,int days){ int tmpDays = 0; int countMonth = 1; while(tmpDays < days){ tmpDays += getDays(year, countMonth); countMonth++; } int[] array = new int[2]; array[0] = countMonth - 1; array[1] = days - (tmpDays - getDays(year, countMonth - 1)); return array; } }
public class Question3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int[][] array = new int[n][m];//用二维数组来表示矩阵 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { array[i][j] = scanner.nextInt();//矩阵的输入 } } for (int i = m - 1; i >= 0; i--) { for (int j = 0; j < n; j++) { System.out.print(array[j][i]+ " ");//输出 } System.out.println(); } scanner.close(); } }
public class Question4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] array = new int[n];//数组保存读者来访记录 for (int i = 0; i < array.length; i++) { array[i] = scanner.nextInt();//输入 } for (int i = 0; i < n; i++) { int count = 1; //计数器,默认值为1,因为每一个记录最少次数为1 if (i == 0) { System.out.print(count+" ");//第一个记录出现次数一定为1 }else { for (int j = 0; j < i; j++) { if (array[j] == array[i]) { count++; //前面出现一次记录则计数器加一 } } System.out.print(count+" "); } } scanner.close(); } }
public class Question5 { public static void main(String[] args) { new Question5().run2(); } /** * 解法一 */ public void run1(){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); } int count = 0; for (int i = 0; i < n - 1; i++) { for(int j = i + 1;j < n;j++){ if (Math.abs(array[i] - array[j]) == 1) { count++; } } } System.out.println(count); scanner.close(); } /** * 解法二 */ public void run2(){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] array = new int[10000]; for (int i = 0; i < n; i++) { array[scanner.nextInt()]++; } int count = 0;//计数器 for (int i = 0; i < 9999; i++) { count += Math.min(array[i], array[i + 1]); } System.out.println(count); scanner.close(); } }
public class Question6 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); } int count = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (array[i] + array[j] == 0) { count++; break; } } } System.out.println(count); scanner.close(); } }
public class Question7 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[][] array = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { array[i][j] = scanner.nextInt(); } } for (int i = 0; i < n; i++) { if (i == 0) { System.out.print(array[i][i]+" "); }else { int a = Integer.MAX_VALUE;int b = Integer.MAX_VALUE; if (i % 2 == 1) { a = 0;b = i; while(b != -1){ System.out.print(array[a][b]+" "); a++; b--; } continue; }else { a = i;b = 0; while(a != -1){ System.out.print(array[a][b]+ " "); a--; b++; } } } } int tmp = 1; for (int i = n; i < 2*n - 1; i++) { if (i == 2*n - 2) { System.out.print(array[n - 1][n - 1]); }else { if (i % 2 == 1) { int a = tmp;int b = i - a; while(b != tmp - 1){ System.out.print(array[a][b]+" "); a++; b--; } tmp++; }else { int b = tmp;int a = i - b; while(a != tmp - 1){ System.out.print(array[a][b]+" "); b++; a--; } tmp++; } } } scanner.close(); } }
public class Question8 { public static void main(String[] args) { new Question8().run(); } /** * 坐标标记法(解决重复区域不确定的问题) */ public void run(){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[][] array = new int[101][101]; for (int i = 0; i < n; i++) { int x1 = scanner.nextInt(); int y1 = scanner.nextInt(); int x2 = scanner.nextInt(); int y2 = scanner.nextInt(); for (int j = x1; j < x2; j++) { for (int j2 = y1; j2 < y2; j2++) { array[j][j2] = 1;//将已经覆盖的区域用1标记 } } } int result = 0; for (int i = 0; i < 101; i++) { for (int j = 0; j < 101; j++) { if (array[i][j] == 1) { result += 1; } } } System.out.println(result); scanner.close(); } }
public class Question9 { public static void main(String[] args) { new Question9().run(); } public void run(){ Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); int flag = scanner.nextInt(); int n = scanner.nextInt(); scanner.nextLine(); String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = scanner.nextLine(); } if (flag == 0) { String newStr = str.toUpperCase(); for (int i = 0; i < n; i++) { if (array[i].toUpperCase().contains(newStr)) { System.out.println(array[i]); } } }else if(flag == 1){ for (int i = 0; i < n; i++) { if (array[i].contains(str)) { System.out.println(array[i]); } } }else { } scanner.close(); } }
http://blog.csdn.net/dingding_12345/article/details/47706097
http://www.chinadmd.com/file/utoticasvip3oix6w3v6v6cw_2.html