当输入的数量不定时:
int x;
while (scanf("%d", &x) == 1) {
}
输入输出重定向:
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int x;
while (scanf("%d", &x) == 1) {
printf("%d", x);
}
提交时将#define LOCAL去掉或者注释
#include<stdio.h>
#define LOCAL
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int x;
while (scanf("%d", &x) == 1) {
printf("%d", x);
}
return 0;
}
若禁止采用重定向的方式,则要利用文件:
FILE *fin, *fout;
fin = fopen("input.txt", "rb");
fout = fopen("output.txt", "wb");
int x;
while (fscanf(fin,"%d", &x) == 1) {
fprintf(fout,"%d", x);
}
fclose(fin);
fclose(fout);
return 0;
当需要根据样例个数打出case x:时,可以套用如下模板:
#include<stdio.h>
int main() {
int kbase = 0;
int a;
while (scanf("%d", &a) == 1)
{
if (kbase)
printf("\n");
printf("Case %d:", ++kbase);
//这里是你的输出部分
printf("\n");
}
return 0;
}