赞
踩
好像去年做的时候印象深刻
所以先不着急
挑出几个特殊情况,比如路程比兔子领先休息的路程短
比如兔子休息完第一次乌龟也追不上,那乌龟永远追不上
……
然后我就不知道怎么写了
麻了
所以找了去年的代码来看
另辟蹊径
不用数学方法,时间按秒数自增
计算每时刻的路程变化
然后我发现有一个测试数据没通过
原因出现在兔子休息的计算过程
原方法是在兔子休息时乌龟路程增加,时间加快到兔子休息完毕
错误应该是乌龟的到达时间不准确
更改为从兔子休息的那一刻起,兔子突然减少自己休息时本应该走的路程,即兔子突然后退,在本该休息的时间再走到刚刚休息的地方
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); int v1, v2, t, s, l; v1 = scanner.nextInt(); v2 = scanner.nextInt(); t = scanner.nextInt(); s = scanner.nextInt(); l = scanner.nextInt(); //数学方法,不成功,放弃了 /* * if (l <= t) { if (v1 > v2) { System.out.println("R"); int time = l / v1; * System.out.println(time); } else if (v1 < v2) { System.out.println("T"); int * time = l / v2; System.out.println(time); } else { System.out.println("D"); * int time = l / v1; System.out.println(time); } } else if (v1 * (t / (v1 - * v2)) > v2 * ((t / (v1 - v2)) + s)) { System.out.println("R"); int time = l / * v1; System.out.println(time); } */ int s1 = 0, s2 = 0, time = 0; while (s1 < l && s2 < l) { s1 += v1; s2 += v2; time++; if (s1 == l || s2 == l) { break; } if (s1 - s2 >= t) { // s2 += v2 * s;跳过兔子休息时间 // time += s; s1 -= v1 * s; } } if (s1 > s2) { System.out.println("R"); } else if (s2 > s1) { System.out.println("T"); } else { System.out.println("D"); } System.out.println(time); } }
回形取数
哈这道题我会
去年错过一万次了
所以很快写完
发现有好几个很长的样例出了问题,没有输出结果
更改长整型之后还是没有解决
最后对比去年的代码发现是已读数字占位出了问题
我写的是已读数字用“0”代替(讲道理应该没有问题因为都是正整数)
正确的是“-1”
所以就尝试改了一下
居然正确了
到现在也没找到原因,只能记住
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); int m = scanner.nextInt(); int n = scanner.nextInt(); int a[][] = new int[m][n]; int i, j; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { a[i][j] = scanner.nextInt(); } } int x = 0, y = 0; int cnt = 0; while (cnt < m * n) { while (x < m && a[x][y] != -1) { System.out.print(a[x][y] + " "); a[x][y] = -1; cnt++; x++; } x--; y++; while (y < n && a[x][y] != -1) { System.out.print(a[x][y] + " "); a[x][y] = -1; cnt++; y++; } y--; x--; while (x >= 0 && a[x][y] != -1) { System.out.print(a[x][y] + " "); a[x][y] = -1; cnt++; x--; } x++; y--; while (y >= 0 && a[x][y] != -1) { System.out.print(a[x][y] + " "); a[x][y] = -1; cnt++; y--; } y++; x++; } } }
报时助手
今天做的最简单的一道题
前20个用字符串数组存储
其他特殊情况摘出来单独讨论
为了减少重复代码,最好写个函数
需要注意的知识点
字符串数组赋值
很简单但是我老是错
String[] a = { "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen",
"twenty" };
还要注意不要忘记介于20到30之间的数字的前半段和后半段
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); int h = scanner.nextInt(); int m = scanner.nextInt(); if (m == 0) { read(h); System.out.print("o'clock"); } else { read(h); read(m); } } public static void read(int t) { String[] a = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty" }; if (t > 0 && t <= 20) { System.out.print(a[t] + " "); } else if (t == 30) { System.out.print("thirty "); } else if (t == 40) { System.out.print("forty "); } else if (t == 50) { System.out.print("fifty "); } else { int num = t / 10; if (num == 2) { System.out.print(a[20] + " "); } else if (num == 3) { System.out.print("thirty "); } else if (num == 4) { System.out.print("forty "); } else if (num == 5) { System.out.print("fifty "); } System.out.print(a[t % 10] + " "); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。