1. 读取未知行数的整数。输入:若干行,每行两个整数。输出:对于每行的两个整数,输出一行,即它们的和
输入样例:
1 2
3 4
-2 3
输出样例:
3
7
1
C/C++:
int main() {
int a, b;
while ( ~ scanf("%d %d", &a, &b)) { // 这里也可以 while(cin >> a >> b)
printf("%d\n", a + b);
}
return 0;
}
Python3:
while True:
Input = input().strip()
if Input == "":
break
a, b = map(int, Input.split())
print(a+b)
2. 读取未知行数的整数,最后一行是两个0。输入:若干行,每行两个整数,最后一行是两个0,不用处理。输出:对于每行的两个整数,输出一行,即它们的和
输入样例:
1 2
3 4
-2 3
0 0
输出样例:
3
7
1
C/C++:
int main() {
int a, b;
while (scanf("%d %d", &a, &b) && a && b) { // 这里也可以 while(cin >> a >> b && a && b)
printf("%d\n", a + b);
}
return 0;
}
Python3:
while True:
Input = input().strip()
if Input == "0 0":
break
a, b = map(int, Input.split())
print(a+b)
3. 读取未知行数的字符串,一行一个字符串,字符串中无空格。输出:对于每个字符串,首字母转大写,其他字符不变,一个一行。
输入样例:
China
cup
cpu
cITy
zoo
输出样例:
China
Cup
Cpu
CITy
Zoo
C/C++:
int main() {
string s;
while (cin >> s) { // 用char s[maxn]; while(~ scanf("%s", s))亦可
if (s[0] >= 'a')
s[0] -= 32;
cout << s << '\n';
}
return 0;
}
Python3:
while True:
Input = input().strip()
if Input == "":
break
s = Input[0].upper() + Input[1:]
print(s)
4. 读取未知行数的字符串,一行若干个字符串,字符串以空格分隔。输出:对于每个字符串,首字母转大写后输出,一个一行。
输入样例:
China cup cpu
cITy zoo Hello
world! abc
输出样例:
China
Cup
Cpu
CITy
Zoo
Hello
World!
Abc
C/C++:
int main() {
string s;
while (getline(cin, s)) { // 不能用 cin >> s,因为它会读到空格就结束,scanf("%s")类似
s += '\n';
bool start = true;
string word = "";
for (int i = 0; i < s.size(); i++) {
if (start) {
if (s[i] >= 'a') s[i] -= 32;
start = false;
}
if (s[i] == ' ' || s[i] == '\n') {
cout << word << '\n';
word = "";
start = true;
continue;
}
word += s[i];
}
}
return 0;
}
Python3:
while True:
s = input().strip()
if s == "":
break
for word in s.split():
res = word[0].upper() + word[1:]
print(res)
—— 本文来自火龙信奥(义乌睿码科技):义乌青少年信息学奥赛与编程教育平台,专注 CSP-J/S、NOIP、GESP 竞赛培训,线上线下融合教学,助力编程升学。网址:hlcoding.com