package provincialGames_05_2014;public class A05_圆周率 {public static void main(String[] args) {double x = 111;for (int n = 10000; n >= 0; n--) {int i = 2 * n + 1; //把i变为奇数x = 2 + (i * i / x);}// x=2+1/x'System.out.println(String.format("%.4f", 4 / (x - 1)));}
}
六、奇怪的分式
标题:奇怪的分式
上小学的时候,小明经常自己发明新算法。一次,老师出的题目是:
1/4 乘以 8/5
小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (参见图1.png)
图1.png??????
老师刚想批评他,转念一想,这个答案凑巧也对啊,真是见鬼!
对于分子、分母都是 1~9 中的一位数的情况,还有哪些算式可以这样计算呢?
请写出所有不同算式的个数(包括题中举例的)。
显然,交换分子分母后,例如:4/1 乘以 5/8 是满足要求的,这算做不同的算式。
但对于分子分母相同的情况,2/2 乘以 3/3 这样的类型太多了,不在计数之列!
注意:答案是个整数(考虑对称性,肯定是偶数)。请通过浏览器提交。不要书写多余的内容。
【答案】:14
package provincialGames_05_2014;public class A06_奇怪的分式 {private static int ans;public static void main(String[] args) {for (int a = 1; a < 10; a++) {for (int b = 1; b < 10; b++) {if (a == b) {continue;}for (int c = 1; c < 10; c++) {for (int d = 1; d < 10; d++) {if (c == d) {continue;}int gcd1 = gcd(a * c, b * d);int gcd2 = gcd(a * 10 + c, b * 10 + d);if (a * c / gcd1 == (a * 10 + c) / gcd2 && b * d / gcd1 == (b * 10 + d) / gcd2)ans++;}}}}System.out.println(ans);}private static int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);}}
七、扑克排序
标题:扑克序列
A A 2 2 3 3 4 4, 一共4对扑克牌。请你把它们排成一行。 要求:两个A中间有1张牌,两个2之间有2张牌,两个3之间有3张牌,两个4之间有4张牌。
请填写出所有符合要求的排列中,字典序最小的那个。
例如:22AA3344 比 A2A23344 字典序小。当然,它们都不是满足要求的答案。
请通过浏览器提交答案。“A”一定不要用小写字母a,也不要用“1”代替。字符间一定不要留空格。
【答案】:2342A3A4
package provincialGames_05_2014;import java.util.HashSet;
import java.util.Set;public class A07_扑克排序 {static Set<String> set = new HashSet<String>();public static void main(String[] args) {char[] a = {'A', 'A', '2', '2', '3', '3', '4', '4'};f(a, 0);for (String x : set) { // 遍历set()System.out.println(x);}}private static void f(char[] a, int k) {if (k == a.length) {String s = new String(a);if (check(s)) {// System.out.println(s);set.add(s);}}for (int i = k; i < a.length; i++) {char t = a[k];a[k] = a[i];a[i] = t;f(a, k + 1);t = a[k];a[k] = a[i];a[i] = t;}}private static boolean check(String s) {if (s.lastIndexOf('A') - s.indexOf('A') == 2 && s.lastIndexOf('2') - s.indexOf('2') == 3 && s.lastIndexOf('3') - s.indexOf('3') == 4 &&s.lastIndexOf('4') - s.indexOf('4') == 5)return true;return false;}
}
public class _09地宫取宝 {private static final int MOD = 1000000007;static int[][] data;private static int n;private static int m;private static int k;public static void main(String[] args) {Scanner sc = new Scanner(System.in);n = sc.nextInt();m = sc.nextInt();k = sc.nextInt();data = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {data[i][j] = sc.nextInt();}}for (int i = 0; i < 51; i++) {for (int j = 0; j < 51; j++) {for (int l = 0; l < 14; l++) {for (int o = 0; o < 14; o++) {cache[i][j][l][o] = -1;}}}}long ans = dfs(0, 0, -1, 0);System.out.println(ans);}static long[][][][] cache = new long[51][51][14][14];private static long dfs(int x, int y, int max, int cnt) {if (cache[x][y][max + 1][cnt] != -1) return cache[x][y][max + 1][cnt];if (x == n || y == m || cnt > k) return 0;int cur = data[x][y];int ans = 0;if (x == n - 1 && y == m - 1) {if (cnt == k || (cnt == k - 1 && cur > max)) return 1;return ans;}if (cur > max) {ans += dfs(x, y + 1, cur, cnt + 1);ans += dfs(x + 1, y, cur, cnt + 1);}ans += dfs(x, y + 1, max, cnt);ans += dfs(x + 1, y, max, cnt);cache[x][y][max + 1][cnt] = ans % MOD;return ans;}
}
package provincialGames_05_2014;import java.util.Scanner;public class A09_地宫取宝 {public static int n, m, k;public static long MOD = 1000000007;public static int[][] map;public static long[][][][] visited = new long[51][51][102][13];public static long dfs(int x, int y, int num, int max) {if(visited[x][y][num][max + 1] != -1)return visited[x][y][num][max + 1];if(x == n - 1 && y == m - 1) {if(num == k)visited[x][y][num][max + 1] = 1;else if(num == k - 1 && max < map[x][y])visited[x][y][num][max + 1] = 1;else visited[x][y][num][max + 1] = 0;return visited[x][y][num][max + 1];}long result = 0;if(x + 1 < n) { //向下移动一步if(max < map[x][y]) {result += dfs(x + 1, y, num + 1, map[x][y]);result %= MOD;}result += dfs(x + 1, y, num, max);result %= MOD;}if(y + 1 < m) { //向右移动一步if(max < map[x][y]) {result += dfs(x, y + 1, num + 1, map[x][y]);result %= MOD;}result += dfs(x, y + 1, num, max);result %= MOD;}return visited[x][y][num][max + 1] = result % MOD;}public static void main(String[] args) {Scanner in = new Scanner(System.in);n = in.nextInt();m = in.nextInt();k = in.nextInt();map = new int[n][m];for(int i = 0;i < n;i++)for(int j = 0;j < m;j++)map[i][j] = in.nextInt();for(int i = 0;i < 51;i++)for(int j = 0;j < 51;j++)for(int x = 0;x < 102;x++)for(int y = 0;y < 13;y++)visited[i][j][x][y] = -1;long ans = dfs(0, 0, 0, -1);System.out.println(visited[0][0][0][0]);}}
/**import java.util.Scanner;public class Main {public static int n, m, k;public static long MOD = 1000000007;public static int[][] map;public static long[][][][] visited = new long[51][51][102][13];public long dfs(int x, int y, int num, int max) {if(visited[x][y][num][max + 1] != -1)return visited[x][y][num][max + 1];if(x == n - 1 && y == m - 1) {if(num == k)visited[x][y][num][max + 1] = 1;else if(num == k - 1 && max < map[x][y])visited[x][y][num][max + 1] = 1;else visited[x][y][num][max + 1] = 0;return visited[x][y][num][max + 1];}long result = 0;if(x + 1 < n) { //向下移动一步if(max < map[x][y]) {result += dfs(x + 1, y, num + 1, map[x][y]);result %= MOD;}result += dfs(x + 1, y, num, max);result %= MOD;}if(y + 1 < m) { //向右移动一步if(max < map[x][y]) {result += dfs(x, y + 1, num + 1, map[x][y]);result %= MOD;}result += dfs(x, y + 1, num, max);result %= MOD;}return visited[x][y][num][max + 1] = result % MOD;}public static void main(String[] args) {Main test = new Main();Scanner in = new Scanner(System.in);n = in.nextInt();m = in.nextInt();k = in.nextInt();map = new int[n][m];for(int i = 0;i < n;i++)for(int j = 0;j < m;j++)map[i][j] = in.nextInt();for(int i = 0;i < 51;i++)for(int j = 0;j < 51;j++)for(int x = 0;x < 102;x++)for(int y = 0;y < 13;y++)visited[i][j][x][y] = -1;test.dfs(0, 0, 0, -1);System.out.println(visited[0][0][0][0]);}}*/
42分
package provincialGames_05_2014;import java.util.Scanner;public class A09_地宫取宝42points { //非记忆型数组private static int n, m, k;
// private static int m;
// private static int k;private static final int MOD = 1000000007;static int[][] data;public static void main(String[] args) {Scanner sc = new Scanner(System.in);n = sc.nextInt();m = sc.nextInt();k = sc.nextInt();data = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {data[i][j] = sc.nextInt();}}long ans = dfs(0, 0, -1, 0);System.out.println(ans);}// x y: 当前所处位置[0, n-1\m-1]; max: 所遇到的最大值; cnt: 已取宝贝的数量private static long dfs(int x, int y, int max, int cnt) {if (x == n || y == m || cnt > k) //越界 返回 0return 0;int cur = data[x][y]; //所处格子中的宝贝价值int ans = 0;if (x == n - 1 && y == m - 1) {if (cnt == k || (cnt == k - 1 && cur > max))// cnt == k - 1, 再加上max这个值, cnt 刚好 == kreturn 1;return ans;}//如果那个格子中的宝贝价值比小明手中任意宝贝价值都大, 小明就可以拿起它if (cur > max) {ans += dfs(x, y + 1, cur, cnt + 1);ans += dfs(x + 1, y, cur, cnt + 1);}//当然,也可以不拿ans += dfs(x, y + 1, max, cnt);ans += dfs(x + 1, y, max, cnt);return ans % MOD;}
}
57分
package provincialGames_05_2014;import java.util.Scanner;public class A09_地宫取宝57points { //记忆型数组private static int n;private static int m;private static int k;private static final int MOD = 1000000007;static int[][] data;static long[][][][] cache = new long[51][51][14][14]; //四维public static void main(String[] args) {Scanner sc = new Scanner(System.in);n = sc.nextInt();m = sc.nextInt();k = sc.nextInt();data = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {data[i][j] = sc.nextInt();}}for (int i = 0; i < 51; i++) { // 初始化for (int j = 0; j < 51; j++) {for (int l = 0; l < 14; l++) {for (int o = 0; o < 14; o++) {cache[i][j][l][o] = -1;}}}}long ans = dfs(0, 0, -1, 0);System.out.println(ans);}private static long dfs(int x, int y, int max, int cnt) {// max == -1, max + 1 防止 数组越界if (cache[x][y][max + 1][cnt] != -1) //曾经有记录 写缓存return cache[x][y][max + 1][cnt];if (x == n || y == m || cnt > k)return 0;int cur = data[x][y];int ans = 0;if (x == n - 1 && y == m - 1) {if (cnt == k || (cnt == k - 1 && cur > max))return 1;return ans;}if (cur > max) {ans += dfs(x, y + 1, cur, cnt + 1);ans += dfs(x + 1, y, cur, cnt + 1);}ans += dfs(x, y + 1, max, cnt);ans += dfs(x + 1, y, max, cnt);cache[x][y][max + 1][cnt] = ans % MOD; // 取缓存return ans;}
}