目录
一,输入结构
1,一级输入结构
2,嵌套输入结构
二,一级输入结构
HDU 1089 A+B for Input-Output Practice (I)
HDU 1090 A+B for Input-Output Practice (II)
HDU 1091 A+B for Input-Output Practice (III)
HDU 1095 A+B for Input-Output Practice (VII)
三,嵌套输入结构
HDU 1092 A+B for Input-Output Practice (IV)
HDU 1093 A+B for Input-Output Practice (V)
HDU 1094 A+B for Input-Output Practice (VI)
HDU 1096 A+B for Input-Output Practice (VIII)
四,输入字符串
(1)gets函数
CodeForces 616A Comparing Two Long Integers
(2)gets的替代函数
(3)scanf
HDU 5842 Lweb and String
(4)char数组 + cin
(4.1)cin
(4.2)cin.getline
(4.3)cin.get
(5)string
(5.1)cin
(5.2)getline
五,输入其他内容
1,输入整数、实数
CSU 1000 A+B (I)
CSU 1001 A+B (II)
CSU 1002 A+B (III)
2,用字符串输入整数、实数
CSU 1340 A Sample Problem
else
3,输入数字和字符的组合
4,表格型数据
5,其他输入技巧
(1)忽略输入
一,输入结构
1,一级输入结构
输入结构分为4类:
- 只输入一个用例
- 输入用例直到特殊用例:HDU 1091
- 输入用例直到文件尾:HDU 1089
- 先输入整数t再输入t个用例:HDU 1090
其中,每个用例都有明确的结束条件。
2,嵌套输入结构
每个用例内部,又可以包含若干部分,每一部分都有可能是只输入一个用例、输入用例直到特殊用例、先输入整数t再输入t个用例等情况,其中每个用例内部还可以继续嵌套。。。
二,一级输入结构
HDU 1089 A+B for Input-Output Practice (I)
题目:
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
代码:
<iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a >> b)cout << a + b << endl;
return 0;
}
HDU 1090 A+B for Input-Output Practice (II)
题目:
Description
Your task is to Calculate a + b.
Input
Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair 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
2 1 5 10 20
Sample Output
6 30
代码:
<iostream>
using namespace std;
int main()
{
int n, a, b;
cin >> n;
while (n--)
{
cin >> a >> b;
cout << a + b << endl;
}
return 0;
}
HDU 1091 A+B for Input-Output Practice (III)
题目:
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
代码:
<iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a >> b)
{
if (a == 0 && b == 0)break;
cout << a + b << endl;
}
return 0;
}
HDU 1095 A+B for Input-Output Practice (VII)
题目:
Description
Your task is to Calculate a + b.
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, and followed by a blank line.
Sample Input
1 5 10 20
Sample Output
6 30
代码:
#include<iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a >> b)cout << a + b << endl << endl;
return 0;
}
三,嵌套输入结构
HDU 1092 A+B for Input-Output Practice (IV)
题目:
Description
Your task is to Calculate the sum of some integers.
Input
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.
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
4 1 2 3 4 5 1 2 3 4 5 0
Sample Output
10 15
代码:
#include<iostream>
using namespace std;
int main()
{
int n, a, sum;
while (cin >>n)
{
if (n == 0)break;
sum = 0;
while (n--)
{
cin >> a;
sum += a;
}
cout << sum << endl;
}
return 0;
}
HDU 1093 A+B for Input-Output Practice (V)
题目:
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
代码:
#include<iostream>
using namespace std;
int main()
{
int t, n, a, sum;
cin >> t;
while (t--)
{
cin >> n;
sum = 0;
while (n--)
{
cin >> a;
sum += a;
}
cout << sum << endl;
}
return 0;
}
HDU 1094 A+B for Input-Output Practice (VI)
题目:
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
代码:
#include<iostream>
using namespace std;
int main()
{
int n, a, sum;
while (cin >> n)
{
sum = 0;
while (n--)
{
cin >> a;
sum += a;
}
cout << sum << endl;
}
return 0;
}
HDU 1096 A+B for Input-Output Practice (VIII)
题目:
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 you must note that there is a blank line between outputs.
Sample Input
3 4 1 2 3 4 5 1 2 3 4 5 3 1 2 3
Sample Output
10 15 6
代码:
#include<iostream>
using namespace std;
int main()
{
int t, n, a, sum;
cin >> t;
while (t--)
{
cin >> n;
sum = 0;
while (n--)
{
cin >> a;
sum += a;
}
cout << sum << endl;
if (t)cout << endl;
}
return 0;
}
四,输入字符串
(1)gets函数
C语言输入字符串,都是用char数组来表示字符串。
gets函数读取整行输入,直到遇到换行符,然后丢弃换行符,储存其他的字符,并在后面补一个'\0'
CodeForces 616A Comparing Two Long Integers
题目:
Description
You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.
The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the functioninput() in Python2 instead of it use the function raw_input().
Input
The first line contains a non-negative integer a.
The second line contains a non-negative integer b.
The numbers a, b may contain leading zeroes. Each of them contains no more than 106 digits.
Output
Print the symbol "<" if a < b and the symbol ">" if a > b. If the numbers are equal print the symbol "=".
Sample Input
Input
9 10
Output
<
Input
11 10
Output
>
Input
00012345 12345
Output
=
Input
0123 9
Output
>
Input
0123 111
Output
>
就是比较2个整数的大小,很简单。
主要就是c语言不熟悉。
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char*s1 = new char[1000000];
char*s2 = new char[1000000];
int l1, l2;
char c;
while (gets(s1) && gets(s2))
{
l1 = strlen(s1);
l2 = strlen(s2);
int i = 0, j = 0;
while (s1[i] == '0'&& l1)
{
i++;
l1--;
}
while (s2[j] == '0'&& l2)
{
j++;
l2--;
}
if (l1 > l2)c='>';
else if (l1 < l2)c='<';
else
{
while (s1[i])
{
if (s1[i]>s2[j])
{
c = '>';
break;
}
if (s1[i]<s2[j])
{
c = '<';
break;
}
i++;
j++;
l1--;
}
if (l1 == 0)c = '=';
}
printf("%c\n", c);
}
return 0;
}
(2)gets的替代函数
可以用fgets或者gets_s替代gets函数,函数功能有差异但类似。
(3)scanf
scanf是读单词的形式,忽略开头的所有空白符,读到任意空白符就结束。
HDU 5842 Lweb and String
题目:
这个题目是说,任选一个从26个字母的集合到整数的集合的映射,然后求每一行的字符数组对应的数列的最长递增子序列的长度的最大值。
如果你以为我要说的是动态规划,那你就傻了。
因为映射可以任选,所以这个题目的意思是,输入一个字符串(只有小写字母),输出它有多少个不同的小写字母。
对,没错,就是这样,没了。
代码:
#include<iostream>
#include<cstring>
using namespace std;
char c[100001];
int l[26];
int main()
{
int t;
scanf("%d", &t);
for (int cas = 1; cas <= t; cas++)
{
scanf("%s", c);
int len = strlen(c);
for (int i = 0; i < 26; i++)l[i] = 0;
for (int i = 0; i < len; i++)l[c[i] - 'a']++;
int sum = 0;
for (int i = 0; i < 26; i++)sum += (l[i]>0);
printf("Case #%d: %d\n", cas, sum);
}
return 0;
}
(4)char数组 + cin
(4.1)cin
cin输入用char数组表示的字符串,遇到空白符会结束输入。
如CSU 1260、CSU 1059、CSU 1019
(4.2)cin.getline
cin.getline可以读取整行字符串,每读取一行,把换行符丢弃。
如 CSU 1029
(4.3)cin.get
cin.get和cin.getline非常相似,但是换行符不会丢弃,而是继续放在输入缓冲区中。
CSU 2050
(5)string
(5.1)cin
cin输入字符串以空白符为结束,如CSU 1041、CSU 1112、CSU 1042 遥控机器人
(5.2)getline
getline读取一整行,如POJ 1035
五,输入其他内容
输入的内容,简单分类就是2种,数字和字符串,仔细分类的话有很多种。
1,输入整数、实数
输入整数是最常见的情况,例子非常多,如上面的HDU IO系列。
CSU 1000/1001/1002 A+B问题 覆盖了普通整数、大整数、实数的输入输出问题。
CSU 1000 A+B (I)
Description
Your task is to Calculate a + b.
Input
Input contains multiple test cases. Each test case consists of a pair of integers a and b(0<=a,b<=20), 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 1
Sample Output
2
代码:
#include<iostream>
using namespace std;
int main()
{
inta, b;
while(cin >> a >> b)
{
cout << a + b << endl;
}
return 0;
}
CSU 1001 A+B (II)
Description
Your task is to Calculate a + b.
Input
There are multiple test cases. Each test case contains only one line. Each line consists of a pair of real number a and b(0<=a,b<=1000000), separated by a space.
Output
For each case, output the answer in one line rounded to 4 digits after the decimal point.
Sample Input
1 5
10 20
0.1 1.5
Sample Output
6.0000
30.0000
1.6000
代码:
#include<iostream>
using namespace std;
int main()
{
double a, b;
while(cin >> a >> b)
{
printf("%.4f",a+b);
cout << endl;
}
return 0;
}
CSU 1002 A+B (III)
Description
Your task is to Calculate a + b.
Input
There are multiple test cases. Each test case contains only one line. Each line consists of a pair of integers a and b(1=< a,b <=1016), separated by a space. Input is followed by a single line with a = 0, b = 0, which should not 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
10000000000000000 10000000000000000
0 0
Sample Output
6
30
20000000000000000
代码:
#include<iostream>
using namespace std;
int main()
{
long long a, b;
while(cin >> a >> b)
{
if(a == 0 && b == 0)break;
cout << a + b<<endl;
}
return 0;
}
2,用字符串输入整数、实数
CSU 1340 A Sample Problem
题目:
Description My girlfriend loves 7 very much, she thinks it is lucky! If an integer contains one or more 7, she will think it is lucky too! Input The first line has one integer T (1 <= T <= 100), means there are T test cases. Output For each test case, if X contains one or more 7, output “Lucky” (without quotation marks), otherwise output “Unlucky” (without quotation marks). Sample Input Sample Output |
HINT
我们主要通过这个题目来感受一下一个完整的ACM竞赛的题目应当是什么样的。
ACM竞赛题目本身的“格式”很明显:
第一部分是对题目的描述,会交待题目的背景,以及你需要编程完成哪些任务等等。
第二部分是对输入的约定,会告诉你题目是否有多组测试数据,每组测试数据的格式,每个参数的类型(整数,浮点数,字符串等)以及取值范围等等。
第三部分是对输出的约定,也就是你的程序应当输出哪些内容以及具体的格式是什么。
第四部分是一些样例,这些例子只是向你展示一部分测试数据以及对应的答案,目的是更好地帮助你理解题意,以及了解对输出格式的要求等。
大家也许会和我当初刚接触ACM时一样心中充满了疑问,在这里我就为大家列举几个常见的问题:
<1> 如果题目有多组数据,我们是不是也要像样例那样把所有的结果先都计算出来,然后再一起输出?
这个是没必要的,你完全可以每处理完一组数据之后就立刻将这组数据对应的结果输出出来。
但如果你是手动去敲这些样例的话,你会发现在屏幕上输入和输出是混在一起的,并不像样例那样输入和输出是分离的,这样会不会不符合要求呢?那么请看<2>,相信你的疑问就会被解答了。
<2> OJ (Online Judge)怎么知道我的程序是不是写对了?
OJ并不会去智能化地分析你的代码正确与否,而是用另一种巧妙的思路来判断你的程序是否符合要求:“喂”给你的程序一些输入数据(就像你在键盘上敲一些东西后敲回车一样),之后再将你的程序“产出”的东西(就像你在屏幕上看到的你的程序输出的东西一样)和标准的答案进行对比,如果你的程序得到的答案和标准的答案一模一样,那么就算你通过了这个题目。
因此,即使你手动输入样例时屏幕上的输入和输出是混在一起也没关系,只要你的程序“产出”的东西和标准答案一模一样就可以了。
也正是这种评判机制的关系,你不应当让程序打印一些提示语句,如“Please input two integers”等等,因为凡是你的程序输出的内容都会被拿来和标准答案进行对比,一旦输出了类似这样的题目没有要求输出的语句,就会和标准答案大相径庭了,自然就会认为你的程序是不正确的了。
值得一提的是,尽管这样的评判机制似乎并不能完美地知道你的程序究竟是不是对的(想想看为什么?),不过如果测试数据足够强大的话,“你的程序是对的”的概率就会很高很高了。
<3> 为什么我的程序通过了所有的样例,但是最后还是过不了这个题目?
题目的样例只是一小部分测试数据,目的是为了更好地帮助你理解题意,以及了解对输出格式的要求等,因此通过了样例并不能够代表能通过所有的测试数据。只有通过了所有的测试数据才能得到“Accept”,也就是通过了这个题目。
<4> 每组测试数据的答案输出完之后,是否都要换行?
是的,即使题目里面没有明确说明,我们也应该在每组测试数据的答案输出完之后再输出一个“换行”(也就是“\n”),就像样例示意的那样。
接下来我们谈谈这个题目的思路吧,相信大家应该都想到算法了:只要依次判断读入的整数的每一位是否是7就可以了。
代码:
#include<iostream>
#include<cstring>
using namespace std;
char c[10];
int main()
{
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
cin >> c;
int f = 0;
for (int i = 0; i < strlen(c); i++)if (c[i] == '7')f = 1;
if (f)cout << "Lucky" << endl;
else cout << "Unlucky" << endl;
}
return 0;
}
ACM的题目就是这样,明明输入的是1个整数,但是我完全可以看成输入的就是一个个字符,只需要看每个字符是不是7,而不需要任何数学计算。
else
CSU 1003
CSU 1053
HDU 1717 小数化分数2
3,输入数字和字符的组合
CodeForces 697B Barnicle
4,表格型数据
SGU 454 Kakuro
5,其他输入技巧
(1)忽略输入
有些输入内容是不需要的,那就可以把变量也省掉。
如输入m s n三个整数,但是s不需要,那么代码就是:
int main()
{
int m, n;
cin >> m >> n >> n;
......
如果需要忽略的输入是排在最后的,甚至连这种覆盖都不需要,直接不输入就行,OJ并不要求必须把输入数据全部输入到程序中,OJ只校验输出。