Java算法练习2020.03.20-蓝桥网基础题、杭电1000 1089(未完结)


依据大佬建议的 刷题步骤(杭电oj题目)

算法练习2020.03.20

题目01

  • 123321是一个非常特殊的数,它从左边读和从右边读是一样的。
  • 输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
  • 输入格式
  • 输入一行,包含一个正整数n。
  • 输出格式
  • 按从小到大的顺序输出满足条件的整数,每个整数占一行。
  • 样例输入
  • 52
  • 样例输出
  • 899998
  • 989989
  • 998899
  • 数据规模和约定
  • 1<=n<=54。
编码中遇到的问题
  1. 将本该放在循环中的变量放在了循环外,导致程序运行中这些变量在该被重新复制的地方没有被重新赋值,或者是造成了不必要的值的累加。从而导致程序运行崩溃。
  2. 作为循环的计数器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一样的答案。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值