这道送分题的难点在于一个常识, 如何判断一年是不是闰年?html
public class Main { public static void main(String[] args) { int day = 0; for(int year = 2000; ; year++){ if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){ day += 366; }else{ day += 365; } if(day % 7 == 2 && String.valueOf(year).endsWith("99")){ System.out.println(year); break; } } } }
在固定数量的数字中找出固定数量的数字, 拼凑出结果, 仍是填空题, 暴力循环.java
public class Main { public static void main(String[] args) { int num = 0; for(int a = 1; a <= 9; a++){ for(int b = 1; b <= 9; b++){ if(b == a){ continue; } for(int c = 1; c <= 9; c++){ if(c == a || c == b){ continue; } for(int d = 1; d <= 9; d++){ if(d == a || d == b || d == c){ continue; } for(int e = 1; e <= 9; e++){ if(e == a || e == b || e == c || e == d){ continue; } if(((a * 10 + b) * (c * 100 + d * 10 + e)) == ((a * 100 + d * 10 + b) * (c * 10 + e))){ num++; } } } } } } System.out.println(num); } }
<html> <img src="https://note.youdao.com/yws/public/resource/59583ca97021fbc487449f72067e1858/xmlnote/431C6CADC90744D091D7B68103BC1B66/32387" /> </html>c++
递归便可. 如今的目标是从左上角的'从'字到右下角的'华'字. 对于'从'字, 能够走两条路, 往右走或者往下走. 在矩阵中, 大部分字都像从字同样能够往下走或往右走. 为有最右边一列字不能往右走, 最下面一行字不能往下走. 在递归中进行判断便可.算法
public class Main { public static void main(String[] args) { int[][] arr = { {1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}, {3, 4, 5, 6, 7}, {4, 5, 6, 7, 8} }; System.out.println(getWays(arr)); } public static int getWays(int[][] arr){ if(arr == null || arr.length == 0){ return 0; } return process(0, 0, arr); } public static int process(int i, int j, int[][] arr){ if(i == arr.length - 1 && j == arr[0].length - 1){ return 1; } if(i == arr.length - 1){ return process(i, j + 1, arr); }else if(j == arr[0].length - 1){ return process(i + 1, j, arr); }else{ return process(i, j + 1, arr) + process(i + 1, j, arr); } } }
1 黄金数 = --------------------- 1 1 + ----------------- 1 1 + ------------- 1 1 + --------- 1 + ...
小数点后3位的值为:0.618 小数点后4位的值为:0.6180 小数点后5位的值为:0.61803 小数点后7位的值为:0.6180340 (注意尾部的0,不能忽略)
这个也不知道到搞多少次小数点后面的100位才精确. 干脆搞它1000次. 作题的时候尝试不一样的循环次数, 直到结果不变.编程
public class Main { public static void main(String[] args) { BigDecimal bd = new BigDecimal(1); for (int i = 0; i < 1000; i++) { bd = bd.add(BigDecimal.ONE); bd = BigDecimal.ONE.divide(bd, 100, BigDecimal.ROUND_HALF_UP); } System.out.println(bd.toString()); } }
class Rational { private long ra; private long rb; private long gcd(long a, long b){ if(b==0) return a; return gcd(b,a%b); } public Rational(long a, long b){ ra = a; rb = b; long k = gcd(ra,rb); if(k>1){ //须要约分 ra /= k; rb /= k; } } // 加法 public Rational add(Rational x){ return ________________________________________; //填空位置 } // 乘法 public Rational mul(Rational x){ return new Rational(ra*x.ra, rb*x.rb); } public String toString(){ if(rb==1) return "" + ra; return ra + "/" + rb; } } 使用该类的示例: Rational a = new Rational(1,3); Rational b = new Rational(1,6); Rational c = a.add(b); System.out.println(a + "+" + b + "=" + c);
输出一下题目中给的例子能够发现, 这个Rational类就是一个黑盒, 只要给出一个分数分子和分母, 它里面就会经过运算分别把分子和分母化为最简形式. 那么分数的加法就很简单了, 个人作法是在草稿纸上那两个最简分数运算一下, 走一次通分的过程就知道这个空怎么填了.数组
new Rational(ra*x.rb+rb*x.ra, rb*x.rb)
static void sort(int[] x) { int p = 0; int left = 0; int right = x.length-1; while(p<=right){ if(x[p]<0){ int t = x[left]; x[left] = x[p]; x[p] = t; left++; p++; } else if(x[p]>0){ int t = x[right]; x[right] = x[p]; x[p] = t; right--; } else{ _________________________; //代码填空位置 } } } 若是给定数组: 25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0 则排序后为: -3,-2,-16,-5,0,0,0,21,19,33,25,16,18,25 请分析代码逻辑,并推测划线处的代码,经过网页提交 注意:仅把缺乏的代码做为答案,千万不要填写多余的代码、符号或说明文字!!
快排的partition过程, 基础排序内容浏览器
p++
例如: 用户输入: 2 5 6 8 11 9 10 12 9 则程序输出: 7 9 再例如: 用户输入: 6 164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196 172 189 127 107 112 192 103 131 133 169 158 128 102 110 148 139 157 140 195 197 185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190 149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188 113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119 则程序输出: 105 120 资源约定: 峰值内存消耗(含虚拟机) < 64M CPU消耗 < 2000ms 请严格按要求输出,不要多此一举地打印相似:“请您输入...” 的多余内容。 全部代码放在同一个源文件中,调试经过后,拷贝提交该源码。 注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。 注意:主类的名字必须是:Main,不然按无效代码处理。
先遍历一遍用哈希表存起来, 直接找到重复的, 同时记录最大值最小值. 再遍历一遍找到缺乏的. 就两遍, 时间复杂度O(N).ide
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String line = ""; int n = sc.nextInt() + 1; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; int num = 0; int repeat = 0; int lack = 0; while(n-- != 0){ line = sc.nextLine(); Scanner sint = new Scanner(line); while(sint.hasNextInt()){ num = sint.nextInt(); if(map.containsKey(num)){ repeat = num; }else{ map.put(num, 1); } max = Math.max(num, max); min = Math.min(min, num); } } for(int i = min; i <= max; i++){ if(!map.containsKey(i)){ lack = i; } } System.out.println(lack + " " + repeat); } }
本题要求: 输入两个正整数m n, 用空格分开 (m < n < 1000*1000) 程序输出 位于m和n之间的幸运数的个数(不包含m和n)。 例如: 用户输入: 1 20 程序输出: 5 例如: 用户输入: 30 69 程序输出: 8 资源约定: 峰值内存消耗(含虚拟机) < 64M CPU消耗 < 2000ms 请严格按要求输出,不要多此一举地打印相似:“请您输入...” 的多余内容。 全部代码放在同一个源文件中,调试经过后,拷贝提交该源码。 注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。 注意:主类的名字必须是:Main,不然按无效代码处理。
我以为这题的难度在于按照位置序列删除数后, 把数列合并起来, 我才用的方法是在数组原地调整.spa
public class Eight { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i < arr.length; i++){ arr[i] = i * 2 + 1; } int luckyNum = 1; int count = 0; while(arr[luckyNum] < n){ for(int i = 0; i < arr.length; i++){ if((i + 1) % arr[luckyNum] == 0){ count++; }else{ arr[i - count] = arr[i];//调整 } } luckyNum++; count = 0; } int res = 0; for(int i = 0; i < arr.length; i++){ if(arr[i] > m && arr[i] < n){ res++; } } System.out.println(res); } }
题目要求: 从标准输入读入一个正整数N (N<1000*1000) 程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的所有种数。 注意:不要求输出每一个表示,只统计有多少表示法! 例如: 用户输入: 100 程序输出: 11 再例如: 用户输入: 105 程序输出: 6 资源约定: 峰值内存消耗(含虚拟机) < 64M CPU消耗 < 3000ms 请严格按要求输出,不要多此一举地打印相似:“请您输入...” 的多余内容。 全部代码放在同一个源文件中,调试经过后,拷贝提交该源码。 注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。 注意:主类的名字必须是:Main,不然按无效代码处理。
这题和第2题马虎的算式的不一样在于, 题目给出固定的数字, 你在凑东西的时候须要把数字所有都用上. 经过全排列就能够完成. 其余和第2题差很少了.调试
public class Main { public static int res; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; f(arr, 0, n); System.out.println(res); } public static void f(int[] arr, int index, int target){ if(index == arr.length){ checkArr(arr, target); } for(int i = index; i < arr.length; i++){ swap(arr, i, index); f(arr, index + 1, target); swap(arr, i, index); } } public static void checkArr(int[] arr, int target){ int add = 0; int mol = 0; int den = 0; for(int i = 0; i < 7; i++){ for(int j = i + 1; j < 8; j++){ add = getNum(arr, 0, i); mol = getNum(arr, i + 1, j); den = getNum(arr, j + 1, 8); if(mol % den == 0 && add + (mol / den) == target){ res++; } } } } public static int getNum(int[] arr, int i, int j){ int returnNum = 0; int system = 1; for(int n = j; n >= i; n--){ returnNum += arr[n] * system; system *= 10; } return returnNum; } public static void swap(int[] arr, int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
输入格式: 第一行是一个正整数N (1 <= N <= 50000), 表示全排列的规模。 第二行是N个不一样的数字Pi(1 <= Pi <= N), 表示这N个数字的某一全排列。 输出格式: 输出一个整数,表示不一样连号区间的数目。 示例: 用户输入: 4 3 2 4 1 程序应输出: 7 用户输入: 5 3 4 2 5 1 程序应输出: 9 解释: 第一个用例中,有7个连号区间分别是:[1,1], [1,2], [1,3], [1,4], [2,2], [3,3], [4,4] 第二个用例中,有9个连号区间分别是:[1,1], [1,2], [1,3], [1,4], [1,5], [2,2], [3,3], [4,4], [5,5] 资源约定: 峰值内存消耗(含虚拟机) < 64M CPU消耗 < 5000ms 请严格按要求输出,不要多此一举地打印相似:“请您输入...” 的多余内容。 全部代码放在同一个源文件中,调试经过后,拷贝提交该源码。 注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。 注意:主类的名字必须是:Main,不然按无效代码处理。
这题绕个小弯就行. 这题的思路是先拿到每一个子区间, 两个for循环嵌套, O(N^2). 这个时候就考验经验了, 判断几个数是否连续: 1)先排序, 排序后逐个比较是否差1. 排序算你快排代价O(NlogN), 总体O(N^3logN). 我跑过, 只得60分.
有没有牛逼点的? 有, 判断是否连续只须要判断区间上的最大值和最小值之差和区间上元素个数个关系便可, 最大值和最小值在产生子区间时生成, 因此直接O(1)出答案, 总体O(N^2). 能跑满分.
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); String s2 = sc.nextLine(); Scanner sc1 = new Scanner(s1); Scanner sc2 = new Scanner(s2); int n = sc1.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = sc2.nextInt(); } int count = 0; for(int i = 0; i < arr.length; i++){ int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for(int j = i; j < arr.length; j++){ max = Math.max(max, arr[j]); min = Math.min(min, arr[j]); if((max - min + 1) == j - i + 1){ count++; } } } System.out.println(count); } }