依据大佬建议的 刷题步骤(杭电oj题目)
算法练习2020.03.20
题目01
- 123321是一个非常特殊的数,它从左边读和从右边读是一样的。
- 输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
- 输入格式
- 输入一行,包含一个正整数n。
- 输出格式
- 按从小到大的顺序输出满足条件的整数,每个整数占一行。
- 样例输入
- 52
- 样例输出
- 899998
- 989989
- 998899
- 数据规模和约定
- 1<=n<=54。
编码中遇到的问题
- 将本该放在循环中的变量放在了循环外,导致程序运行中这些变量在该被重新复制的地方没有被重新赋值,或者是造成了不必要的值的累加。从而导致程序运行崩溃。
- 作为循环的计数器
i
,在循环中要对i
的值进行改变处理,但是我直接对i
进行了处理,结果导致i
的原值丢失,造成程序逻辑判断的错误。应该将i
的值每次先赋值给一个中间变量,该中间变量用于数据操作,i
的值作为条件比对。
测试结果
60分/100分,还在考虑出错原因。如有大佬知晓原因请指点。
全代码
public class Main {
public static void main(String[] args) {
boolean flag = false;
Scanner scanner = new Scanner(System.in);
long n = scanner.nextLong();
long tmp;
long p;
long numTmp;
// System.out.println("输入是:"+n);
// scanner.next();
for (long i = 10000; i <= 1000000; i++) {
numTmp = i;
// System.out.println("正在计算"+String.valueOf(i)+"......");
// System.out.println(String.valueOf(i));
long sum = 0;
long reverse = 0;
if(i>=100000){
p = 100000;
} else {
p = 10000;
}
while (numTmp>1) {
tmp = numTmp%10;
sum+=tmp;
reverse+=tmp*p;
numTmp = numTmp/10;
p = p/10;
}
// System.out.println("反转值:"+reverse);
// System.out.println("各位和:"+sum);
if(reverse==i && sum==n) {
System.out.println(i);
// System.out.println("是的");
} else {
// System.out.println("不是的");
}
}
}
题目02
@description
- Calculate A + B.
- @Input
- Each line will contain two integers A and B. Process to end of file.
- @Output
- For each case, output A + B in one line.
- @Sample_Input
- 1 1
- @Sample_Output
- 2
编码中遇到的问题
前几次提交的时候确实很郁闷,因为这种看起来无比简单的问题初次提交居然还没通过。基本逻辑都没错,关键就在于程序的输入停止判断上:题目的要求是—— Process to end of file.
(end of file也就是C语言中的EOF
。在java中可以通过在键盘上按下ctrl+z来模拟EOF
的读入。)题目要求的意思是,不一定只是计算一组数字,而是计算一直在进行下去,输入一组,计算一组,直到读入EOF
字符为止。
那么来对比一下,我最初的核心逻辑是这样的:
Scanner scanner = new Scanner(System.in);
int a, b;
a = scanner.nextInt();
b = scanner.nextInt();
System.out.println(a+b);
这样的答案是Wrong Answer。
应该使用java中的new Scanner().hasNext()
来判断输入的字符是否是EOF。正确逻辑如下:
Scanner scanner = new Scanner(System.in);
int a, b;
// 这一句就是在读入EOF之前输入不停止
while (scanner.hasNext()){
a = scanner.nextInt();
b = scanner.nextInt();
System.out.println(a+b);
}
测试结果
显示Accept就是ojbk。
全代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a, b;
// 这一句就是在读入EOF之前输入不停止
while (scanner.hasNext()){
a = scanner.nextInt();
b = scanner.nextInt();
System.out.println(a+b);
}
}
}
题目03
- @description
- Your task is to Calculate a + b.
- Too easy?! Of course! I specially designed the problem for acm beginners.
- You must have found that some problems have the same titles with this one,
- yes, all these problems were designed for the same aim.
- @Input
- The input will consist of a series of pairs of integers a and b,
- separated by a space, one pair of integers per line.
- @Output
- For each pair of input integers a and b you should output
- the sum of a and b in one line, and with one line of output for each line in input.
- @Sample_Input
- 1 5
- 10 20
- @Sample_Output
- 6
- 30
个人感觉这题的示例输入和输出给的不太好,有点容易误导像我这样的新手。但其实是和题目2一样的答案,一样的尿性,就是要注意EOF
这个坑。
解答
和题目2一样的答案。