赞
踩
前天做最后一批华为软件机试时,因为完全不清楚这种所谓的ACM模式,刚看到的时候就傻眼了,即使已经看到了这种区别,但是也没有充分的提前准备好应对,因此光输入输出都卡了好久,最后也有自己的原因基本等于交了白卷。
看网上评论说大家都会有这个问题,因此也算是正常。自己也要从哪里跌倒就从哪里爬起来,把这个难点给克服了,不然以后遇到同样的机试还是干瞪眼。
学习的过程顺便留下记录,以便日后翻阅。
题目链接:OJ在线编程常见输入输出练习场
提交记录 :ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛
分别采用Python3.9和Java1.8来解决,前者因为确实代码简单易懂,后者只是因为近期刷题都是Java,刷完剑指offer后应对机试也考虑主要用python,能免掉很多问题。
Python代码部分参考了B站up主以鹅爱折腾 的 牛客网笔试不会数据导入??看这个视频就够了(1) ,感兴趣的同学也可以去学习。
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。
输出a+b的结果
1 5
10 20
6
30
while True:
try:
num = list(map(int,input().split(" ")))
print(sum(num))
except:
break
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
#include <iostream>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b) { // 注意 while 处理多个 case
// 64 位输出请用 printf("%lld")
cout << a + b << endl;
}
}
输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)
输出a+b的结果
2
1 5
10 20
6
30
t = int(input())
for i in range(t):
num = list(map(int,input().split(" ")))
print(sum(num))
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
#include<iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a;
while(cin >> b >> c) {
cout << b + c << endl;
}
}
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
输出a+b的结果
1 5
10 20
0 0
6
30
while True:
try:
num = list(map(int,input().split(" ")))
if num[0] == num[1] == 0:
break
print(sum(num))
except:
break
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
if(a ==0 && b == 0) break;
System.out.println(a + b);
}
}
}
#include<iostream>
using namespace std;
int main() {
int a, b;
while(cin >> a >> b) {
if (a == 0 && b == 0) break;
else cout << a + b << endl;
}
}
输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。
每组数据输出求和的结果
4 1 2 3 4
5 1 2 3 4 5
0
10
15
while True:
try:
num = list(map(int,input().split(" ")))
if num[0] == 0:
break
print(sum(num[1: ]))
except:
break
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { // 注意 while 处理多个 case 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); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { // 注意 while 处理多个 case int n = in.nextInt(); if(n == 0) break; int sum = 0; while (n > 0) { sum += in.nextInt(); n--; } System.out.println(sum); } } }
#include<iostream> using namespace std; int main() { int n; while(cin >> n) { int sum = 0; int temp; if (n == 0) break; for(int i = 0; i < n; i++) { cin >> temp; sum += temp; } cout << sum << endl; } }
输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
每组数据输出求和的结果
2
4 1 2 3 4
5 1 2 3 4 5
10
15
t = int(input())
for i in range(t):
num = list(map(int,input().split(" ")))
print(sum(num[1:]))
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); for(int i = 0; i < num; i++) { // 注意 while 处理多个 case int n = in.nextInt(); if(n == 0) break; int sum = 0; while (n > 0) { sum += in.nextInt(); n--; } System.out.println(sum); } } }
#include<iostream> using namespace std; int main() { int num; cin >> num; while(num--) { int n; cin >> n; int sum = 0; int temp; for(int i = 0; i < n; i++) { cin >> temp; sum += temp; } cout << sum << endl; } return 0; }
输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
每组数据输出求和的结果
4 1 2 3 4
5 1 2 3 4 5
10
15
while True:
try:
num = list(map(int,input().split(" ")))
print(sum(num[1:]))
except:
break
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
int sum = 0;
while (n > 0) {
sum += in.nextInt();
n--;
}
System.out.println(sum);
}
}
}
#include<iostream>
using namespace std;
int main() {
int n;
while(cin >> n) {
int sum = 0;
int temp;
for(int i = 0; i < n; i++) {
cin >> temp;
sum += temp;
}
cout << sum << endl;
}
}
输入数据有多组, 每行表示一组输入数据。
每行不定有n个整数,空格隔开。(1 <= n <= 100)。
每组数据输出求和的结果
1 2 3
4 5
0 0 0 0 0
6
9
0
while True:
try:
num = list(map(int,input().split(" ")))
print(sum(num))
except:
break
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) { // 注意 while 处理多个 case
String[] s = in.nextLine().split(" ");
int sum = 0;
for (int i = 0; i < s.length; i++) {
sum += Integer.parseInt(s[i]);
}
System.out.println(sum);
}
}
}
#include<iostream>
using namespace std;
int main() {
int sum = 0;
int temp;
while (cin >> temp) {
sum += temp;
if (cin.get() == '\n') {
cout << sum << endl;
sum = 0;
}
}
}
输入有两行,第一行n
第二行是n个空格隔开的字符串
输出一行排序后的字符串,空格隔开,无结尾空格
5
c d a bb e
a bb c d e
t = int(input())
num = list(input().split(" "))
num.sort()
print(" " .join(num))
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); while (in.hasNext()) { // 注意 while 处理多个 case String[] s = in.nextLine().split(" "); Arrays.sort(s); for (int i = 0; i < s.length; i++) { System.out.println(s[i] + " "); } } } }
执行结果:
答案错误:您提交的程序没有通过所有的测试用例
case通过率为 50.00%
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); while (in.hasNext()) { // 注意 while 处理多个 case String[] s = in.nextLine().split(" "); Arrays.sort(s); for (int i = 0; i < s.length; i++) { System.out.print(s[i] + " "); } } } }
执行结果:
答案正确:恭喜!您提交的程序通过了所有的测试用例
case通过率为 100.00%
print将它的参数显示在命令窗口,并将输出光标定位在所显示的最后一个字符之后。
println 将它的参数显示在命令窗口,并在结尾加上换行符,将输出光标定位在下一行的开始。
print意思是:打印
而println是print+line的缩写,即:换行打印
所以有了上面的错误
#include<iostream> #include<set> using namespace std; int main(){ int n; cin >> n; set<string> m; while(n--){ string s; cin >> s; m.insert(s); } for(auto a:m){ cout << a << " "; } }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; vector<string> res(n); for(int i=0;i<n;i++){ cin>>res[i]; } sort(res.begin(),res.end()); for(int i=0;i<n;i++){ cout<<res[i]<<" "; } }
多个测试用例,每个测试用例一行。
每行通过空格隔开,有n个字符,n<100
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开
a c bb
f dddd
nowcoder
a bb c
dddd f
nowcoder
while True:
try:
num = list(input().split(" "))
num.sort()
print(" " .join(num))
except:
break
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) { // 注意 while 处理多个 case
String[] s = in.nextLine().split(" ");
Arrays.sort(s);
for (String c:s) {
System.out.print(c + " ");
}
System.out.println();
}
}
}
#include <bits/stdc++.h> using namespace std; int main() { string s; vector<string> res; while(cin>>s){ res.push_back(s); if(cin.get()=='\n'){ sort(res.begin(),res.end()); for(auto c:res) cout<<c<<" "; cout<<endl; res.clear(); } } }
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格
a,c,bb
f,dddd
nowcoder
a,bb,c
dddd,f
nowcoder
while True:
try:
num = list(input().split(","))
num.sort()
print("," .join(num))
except:
break
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) { // 注意 while 处理多个 case
String[] s = in.nextLine().split(",");
Arrays.sort(s);
int l = s.length;
for (int i = 0; i < l - 1; i++) {
System.out.print(s[i] + ",");
}
System.out.println(s[l-1]);
}
}
}
#include <bits/stdc++.h> using namespace std; int main() { string s; vector<string> res; while(cin>>s) { stringstream ss(s); string temp; while(getline(ss,temp,',')) res.push_back(temp); sort(res.begin(),res.end()); for(int i=0;i<res.size()-1;i++) cout<<res[i]<<','; cout<<res.back()<<endl; res.clear(); } }
输入有多组测试用例,每组空格隔开两个整数
对于每组数据输出一行两个整数的和
1 1
2
while True:
try:
num = list(map(int,input().split(" ")))
print(sum(num))
except:
break
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLong()) { // 注意 while 处理多个 case
Long a = in.nextLong();
Long b = in.nextLong();
System.out.println(a + b);
}
}
}
#include <iostream>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b) { // 注意 while 处理多个 case
// 64 位输出请用 printf("%lld")
cout << a + b << endl;
}
}
在平时的刷题过程中一般会遇到题目要求写的是ACM模式代码,即自己写输入输出,下面参考了博主 一热爱摄影的 写输入输出模式的部分总结。
Scanner 类是获取键盘输入的一个类,首先先创建 Scanner 对象
Scanner sc = new Scanner(System.in);
接下来通过Scanner 类的方法来获取输入,在调用方法之前一般可以采取has…方法判断是否有输入。
next 和 nextLine 都是获取输入字符串的方法
next( )方法 nextLine( )方法
只能读取到空格之前的字符串 可以读取空格的字符串
比如“你好 java”,只能读取“你好” 比如“你好 java”,可以读取“你好 java”
在读取前可以使用 hasNext 与 hasNextLine 判断是否有输入的数据
if ( sc.hasNext()) { String str1=sc.next(); }
if ( sc.hasNextLine()) { String str2=sc.nextLine(); }
此外,还可以接受整数和小数,方法如下:
i = scan.nextInt(); // 接收整数
f = scan.nextFloat(); // 接收小数
2.Integer.parseInt和Integer.valueOf的区别
parseInt( )://返回的是基本类型int
valueOf( )://返回的是包装类Integer
以上就是关于OJ和牛客网ACM模式输入输出的相关内容了,再接再厉,继续冲。
欢迎讨论,共同进步。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。