Java算法练习20200324(杭电1094 1093 1091)


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

题目01

linkOJ
@description
Your task is to calculate the sum of some integers.
*
*

  • Input
  • Input contains multiple test cases, and one case one line.
  • Each case starts with an integer N, and then N integers follow in the same line.
  • Output
  • For each test case you should output the sum of N integers in one line,
  • and with one line of output for each line in input.
  • Sample Input
  • 4 1 2 3 4
  • 5 1 2 3 4 5
  • Sample Output
  • 10
  • 15
编码中遇到的问题

这题意思和1096差不多。只不过少了个输入规定行数。
题目中注意这句Input contains multiple test cases, and one case one line.
意思是输入只有遇到EOF的时候才停止,不然就是输出一次后继续进行下一次输入。

测试结果

ojbk。

全代码
import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       short sum;
       while (scanner.hasNextShort()) {
           sum = 0;
           short counter = scanner.nextShort();
           for (int i = 0; i < counter; i++) {
               sum+=scanner.nextShort();
           }
           System.out.println(sum);
       }
   }
}

题目02

linkOJ

@description
*Your task is to calculate the sum of some integers.

  • Input
  • Input contains an integer N in the first line, and then N lines follow.
  • Each line starts with a integer M, and then M integers follow in the same line.
  • Output
  • For each group of input integers you should output their sum in one line,
  • and with one line of output for each line in input.
  • Sample Input
  • 2
  • 4 1 2 3 4
  • 5 1 2 3 4 5
  • Sample Output
  • 10
  • 15
编码中遇到的问题

该题和1096唯一的不同就是输出换不换行。这题输出不换行。

测试结果

ojbk。

全代码
import java.util.Scanner;
public class Main {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       short n = scanner.nextShort();
       short[] results = new short[n];
       for (int i = 0; i < n; i++) {
           short counter = scanner.nextShort();
           for (int j = 0; j < counter; j++) {
               results[i]+=scanner.nextShort();
           }
       }
       for (int i = 0; i < n; i++) {
           System.out.println(results[i]);
       }
   }
}

题目03

linkOJ

@description

  • Your task is to Calculate a + b.
  • Input
  • Input contains multiple test cases. Each test case contains a pair of integers a and b,
  • one pair of integers per line.
  • A test case containing 0 0 terminates the input and this test case is not to be processed.
  • 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
  • 0 0
  • Sample Output
  • 6
  • 30
编码中遇到的问题

这题好判断,以“0 0”输入为结束符。

测试结果

ojbk。

全代码
import java.util.Scanner;
public class Main {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       short[] results = new short[Short.MAX_VALUE];
       short i = 0;
       while (sc.hasNextShort()) {
           short a = sc.nextShort();
           short b = sc.nextShort();
           if(a==0 && b==0) {
               for (int j = 0; j < results.length; j++) {
                   if(results[j] == 0) {
                       break;
                   }
                   System.out.println(results[j]);
               }
               return;
           }
           results[i] = (short) (a+b);
           i++;
       }
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值