当前位置:网站首页>ACM模式输入输出练习
ACM模式输入输出练习
2022-07-26 22:47:00 【余小盒】
考虑到有的公司在笔试时会要求ACM模式,因此针对ACM模式的输入输出进行总结练习
目录
1.输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。
2.输入第一行包括一个数据组数t(1 <= t <= 100)接下来每行包括两个正整数a,b(1 <= a, b <= 1000)
3.输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
1.输入数据包括多组。每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。接下来n个正整数,即需要求和的每个正整数。
3.输入数据有多组, 每行表示一组输入数据。每行的第一个整数为整数的个数n(1 <= n <= 100)。接下来n个正整数, 即需要求和的每个正整数。
4.输入数据有多组, 每行表示一组输入数据。每行不定有n个整数,空格隔开。(1 <= n <= 100)。
1.输入有两行,第一行n ,第二行是n个字符串,字符串之间用空格隔开
2.多个测试用例,每个测试用例一行。 每行通过空格隔开,有n个字符,n<100
3.多个测试用例,每个测试用例一行。 每行通过,隔开,有n个字符,n<100
计算a+b
1.输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。
输出a+b的结果

import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNextInt()){
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a+b);
}
}
}2.输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 1000)
输出a+b的结果

import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] array = new int[n][2];
for(int i = 0; i < n; i++) {
array[i][0] = in.nextInt();
array[i][1] = in.nextInt();
System.out.println(array[i][0] + array[i][1]);
}
}
}3.输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
输出a+b的结果

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
int i = in.nextInt();
int j = in.nextInt();
if (i == 0 && j == 0) break;
System.out.println(i+j);
}
}
}计算一系列数的和
1.输入数据包括多组。每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。接下来n个正整数,即需要求和的每个正整数。
每组数据输出求和的结果

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
int n = in.nextInt();
if(n==0) break;
int sum = 0;
for(int i = 0; i < n; i++) {
sum += in.nextInt();
}
System.out.println(sum);
}
}
}2.输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。接下来t行, 每行一组数据。每行的第一个整数为整数的个数n(1 <= n <= 100)。接下来n个正整数, 即需要求和的每个正整数。
每组数据输出求和的结果

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int k = sc.nextInt();
int sum = 0;
for (int j = 0; j < k; j++) {
sum += sc.nextInt();
}
System.out.println(sum);
}
}
}
}3.输入数据有多组, 每行表示一组输入数据。每行的第一个整数为整数的个数n(1 <= n <= 100)。接下来n个正整数, 即需要求和的每个正整数。
每组数据输出求和的结果

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
int n = in.nextInt();
int sum = 0;
for(int i = 0; i < n; i++) {
sum += in.nextInt();
}
System.out.println(sum);
}
}
}4.输入数据有多组, 每行表示一组输入数据。每行不定有n个整数,空格隔开。(1 <= n <= 100)。
每组数据输出求和的结果

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
String[] n = s.split(" ");
int sum = 0;
for (int i = 0; i < n.length; i++) {
sum += Integer.parseInt(n[i]);
}
System.out.println(sum);
}
}
}对输入的字符串进行排序后输出
1.输入有两行,第一行n ,第二行是n个字符串,字符串之间用空格隔开
输出一行排序后的字符串,空格隔开,无结尾空格

import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
String[] s = new String[n];
for (int i = 0; i < s.length; i++) {
s[i] = sc.next();
}
Arrays.sort(s);
for (String t : s) {
System.out.print(t+" ");
}
}
}
}2.多个测试用例,每个测试用例一行。 每行通过空格隔开,有n个字符,n<100
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开

import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
String[] t = s.split(" ");
Arrays.sort(t);
for (String a : t) {
System.out.print(a+" ");
}
System.out.println();
}
}
}3.多个测试用例,每个测试用例一行。 每行通过,隔开,有n个字符,n<100
对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
String[] temp = in.nextLine().split(",");
Arrays.sort(temp);
String res = "";
for(int i = 0; i < temp.length; i++) {
if(i != temp.length - 1) {
res += temp[i] + ",";
}
else {
res += temp[i];
}
}
System.out.println(res);
}
}
}自测本地通过提交为0
输入有多组测试用例,每组空格隔开两个整数
对于每组数据输出一行两个整数的和
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
long i = in.nextLong();
long j = in.nextLong();
System.out.println(i+j);
}
}
}边栏推荐
- mysql存储引擎及其区别
- GAN的训练技巧:炼丹师养成计划 ——生成式对抗网络训练、调参和改进
- Machine learning exercise 7 - K-means and PCA (principal component analysis)
- SQL anti injection regular expression
- 6.29 众安暑期测开实习一面
- [translation] reduced precision in tensorrt
- 负载均衡的运用
- Notes during in-depth learning (to be improved)
- MySQL stored procedure function
- 解决方案:炼丹师养成计划 Pytorch+DeepLearning遇见的各种报错与踩坑避坑记录(一)
猜你喜欢

24ssh service
![[polymorphism] the detailed introduction of polymorphism is simple and easy to understand](/img/85/7d00a0d9bd35d50635a0e41f49c691.png)
[polymorphism] the detailed introduction of polymorphism is simple and easy to understand
![[daily question] 565. Array nesting](/img/d7/b3fbdbabdc4193816c490b684bba66.png)
[daily question] 565. Array nesting

mysql一个select语句的执行过程

使用ECS和OSS搭建个人网盘

解决方案:炼丹师养成计划 Pytorch+DeepLearning遇见的各种报错与踩坑避坑记录(二)

Use of shell (11) brackets

Shell (8) cycle

Hash索引和B+树相关知识

Application of load balancing
随机推荐
Es specify user name and password when instantiating resthighlevelclient
SQL anti injection regular expression
mysql的安装
[translation] explicit and implicit batch in tensorrt
Freytek central computing platform 360 degree sensing system solves the challenges behind NOA mass production
6.29 众安暑期测开实习一面
PHP processing tree and infinite processing
Which securities company is better or safer for retail investors to open accounts
DF-GAN实验复现——复现DFGAN详细步骤 及使用MobaXtem实现远程端口到本机端口的转发查看Tensorboard
云数据库管理初体验
引用的通俗讲解
--Project summary
Flink1.13.6详细部署方式
Homework 1-4 learning notes
[reprint] NVIDIA hardware and the accuracy mode supported by each hardware
MySQL备份恢复
Realize data interaction between two apps through fileprovider
mysql一个select语句的执行过程
Connect mysql detailed graphic operations in ECs docker (all)
24ssh service