ACM输入输出技术总结

来源:互联网 发布:mac os shimo 破解版 编辑:程序博客网 时间:2024/06/06 01:44

输入:

第一类:输入不说明有多少个InputBlock,EOF为结束标志。

例子:HDOJ_1089(http://acm.hdu.edu.cn/showproblem.PHP?pid=1089)

C:

[cpp] view plain copy
print?
  1. #include<cstdio>  
  2. int main()  
  3. {  
  4.     int a, b;  
  5.     while(scanf("%d%d",&a, &b) != EOF)  
  6.     {  
  7.         printf("%d\n",a + b);  
  8.     }  
  9. }  
说明:int scanf( const char *format, ... );  返回输入数据个数,没有则返回EOF(-1)

C++:

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     long a,b;  
  6.     while(cin >> a >> b)  
  7.     {  
  8.         cout << a + b << endl;  
  9.     }  
  10.     return 0;  
  11. }  

第二类:输入一开始就会说有NInput Block,下面接着是NInput Block 

例子:HDOJ_1090 (http://acm.hdu.edu.cn/showproblem.php?pid=1090)

C:

[cpp] view plain copy
print?
  1. #include<cstdio>  
  2. int main()  
  3. {  
  4.     int a,b,n,i;  
  5.     scanf("%d",&n);  
  6.     for(i = 0; i < n; i++)  
  7.     {  
  8.         scanf("%d%d",&a,&b);  
  9.         printf("%d\n",a + b);  
  10.     }  
  11.     return 0;  
  12. }  

C++:
[cpp] view plain copy
print?
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     long a,b,n;  
  6.     cin >> n;  
  7.     while(n--)  
  8.     {  
  9.         cin >> a >> b;  
  10.         cout << a + b << endl;  
  11.     }  
  12.     return 0;  
  13. }  

第三类:输入不说明有多少个Input Block,但以某个特殊输入为结束标志。

例子:HDOJ_1091(http://acm.hdu.edu.cn/showproblem.php?pid=1091)

C:

[cpp] view plain copy
print?
  1. #include<cstdio>  
  2. int main()  
  3. {  
  4.     int a,b;  
  5.     while(scanf("%d%d",&a,&b))  
  6.     {  
  7.         if(0 == a && 0 == b)  
  8.             break;  
  9.         printf("%d\n",a + b);  
  10.     }  
  11.     return 0;  
  12. }  
注意:while(scanf("%d %d",&a,&b) &&(a!=0 && b!=0))  由于&&运算符是短路运算,所以,如果你输入的是

5,输入正确,a!= 0为假,整个逻辑表达式的值就为假,跳出结束程序,与题目输入规则相矛盾!
C++:

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int a, b;  
  6.     while(cin >> a >> b)  
  7.     {  
  8.         if(0 == a && 0 == b)  
  9.             break;  
  10.         cout << a + b << endl;  
  11.     }  
  12.     return 0;  
  13. }  

第四类:以上三类的组合。

例子:

HDOJ_1092(http://acm.hdu.edu.cn/showproblem.php?pid=1092)

C:

[cpp] view plain copy
print?
  1. #include<cstdio>  
  2. int main()  
  3. {  
  4.     int tmp,sum,n,i;  
  5.     while(scanf("%d",&n))  
  6.     {  
  7.         if(0 == n)  
  8.             break;  
  9.         sum = 0;  
  10.         for(i = 0; i < n; i++)  
  11.         {  
  12.             scanf("%d",&tmp);  
  13.             sum += tmp;  
  14.         }  
  15.         printf("%d\n",sum);  
  16.   
  17.     }  
  18.     return 0;  
  19. }  

C++:

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int tmp,sum,n,i;  
  6.     while(cin >> n)  
  7.     {  
  8.         if(0 == n)  
  9.             break;  
  10.         sum = 0;  
  11.         for(i = 0; i < n; i++)  
  12.         {  
  13.             cin >> tmp;  
  14.             sum += tmp;  
  15.         }  
  16.         cout << sum << endl;  
  17.     }  
  18.     return 0;  
  19. }  

HDOJ_1093(http://acm.hdu.edu.cn/showproblem.php?pid=1093)

C:

[cpp] view plain copy
print?
  1. #include<cstdio>  
  2. int main()  
  3. {  
  4.     int n,k,tmp,sum;  
  5.     scanf("%d",&n);  
  6.     while(n--)  
  7.     {  
  8.         sum = 0;  
  9.         scanf("%d",&k);  
  10.         while(k--)  
  11.         {  
  12.             scanf("%d",&tmp);  
  13.             sum += tmp;  
  14.         }  
  15.         printf("%d\n",sum);  
  16.     }  
  17.     return 0;  
  18. }  

C++:

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int n,k,tmp,sum,i,j;  
  6.     cin >> n;  
  7.     for(i = 0; i < n; i++)  
  8.     {  
  9.         sum = 0;  
  10.         cin >> k;  
  11.         for(j = 0; j < k; j++)  
  12.         {  
  13.             cin >> tmp;  
  14.             sum += tmp;  
  15.         }  
  16.         cout << sum << endl;  
  17.     }  
  18.     return 0;  
  19. }  

HDOJ_1094(http://acm.hdu.edu.cn/showproblem.php?pid=1094)

C:

[cpp] view plain copy
print?
  1. #include<cstdio>  
  2. int main()  
  3. {  
  4.     int n,tmp,sum;  
  5.     while(scanf("%d",&n) == 1)  
  6.     {  
  7.         sum = 0;  
  8.         while(n--)  
  9.         {  
  10.             scanf("%d",&tmp);  
  11.             sum += tmp;  
  12.         }  
  13.         printf("%d\n",sum);  
  14.     }  
  15.     return 0;  
  16. }  

C++:

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int n,tmp,sum,i;  
  6.     while(cin >> n)  
  7.     {  
  8.         sum = 0;  
  9.         for(i = 0; i < n; i++)  
  10.         {  
  11.             cin >> tmp;  
  12.             sum += tmp;  
  13.         }  
  14.         cout << sum << endl;  
  15.     }  
  16.     return 0;  
  17. }  

第五类:输入的是一整行字符串

例子:HDOJ_1048(http://acm.hdu.edu.cn/showproblem.php?pid=1048)

C:

[cpp] view plain copy
print?
  1. #include<cstdio>  
  2. #include<cstring>  
  3. #include<cctype>  
  4. #define MAX 1000 + 10  
  5. char buf[MAX],fs[15] = "START",fe[15] = "END",fend[15] = "ENDOFINPUT";  
  6. int main()  
  7. {  
  8.   
  9.     int i;  
  10.     //while(fgets(buf,sizeof(buf),stdin))  
  11.     while(gets(buf))  
  12.     {  
  13.         if( strcmp(buf,fs) == 0 || strcmp(buf,fe) == 0 )  
  14.            continue;  
  15.         if(strcmp(buf,fend) == 0)  
  16.             break;  
  17.         i = 0;  
  18.         while(buf[i] != '\0')  
  19.         {  
  20.             if(isalpha(buf[i]))  
  21.                 printf("%c",(buf[i] - 'A' + 21) % 26 + 'A');  
  22.             else  
  23.                 printf("%c",buf[i]);  
  24.             i++;  
  25.         }  
  26.         printf("\n");  
  27.         //printf("%s\n",buf);  
  28.     }  
  29.   
  30.   
  31. }  

注意:gets(s)会存在缓冲区溢出漏洞,一般ACM或在线OJ都尽量避免这个漏洞的出现,如果出现RE,可能就是缓冲区漏洞溢出,可以使用fgets(buf,MAXN,fin),这个方法会连同换行回车一起读入,如果读文件到文件尾时没有回车,就没有读入回车。

C++:

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. #include<cctype>  
  3. #include<string>  
  4. #define MAX 1000 + 10  
  5. using namespace std;  
  6. string buf;  
  7. int main()  
  8. {  
  9.   
  10.     int i;  
  11.     string fs,fe,fend;  
  12.     fs = "START";  
  13.     fe = "END";  
  14.     fend = "ENDOFINPUT";  
  15.     while(getline(cin,buf))  
  16.     {  
  17.         if(buf == fs || buf == fe)  
  18.         {  
  19.             //getline(cin,buf);  
  20.             continue;  
  21.         }  
  22.         if(buf == fend)  
  23.             break;  
  24.         for(i = 0; i < buf.length(); i++)  
  25.         {  
  26.             if(isalpha(buf[i]))  
  27.                 buf[i] = (buf[i] - 'A' + 21) % 26 + 'A';  
  28.         }  
  29.         cout << buf << endl;  
  30.         //getline(cin,buf);  
  31.     }  
  32.     return 0;  
  33. }  
注意:如果用char [255]保持,可以使用cin.getline(char,255),上述使用了string进行保存

一般来说,读入一行的可以使用以上方法,如果读入一个字符串,可以使用scanf("%s",str);或者cin << str;读者可以根据题目自行分析,如果使用C语言中的getc(char)或者scanf("%c",chr),则会读入空格,回车,这个的地方要注意。


练习:

HDOJ_1013(http://acm.hdu.edu.cn/showproblem.php?pid=1013)

HDOJ_1018(http://acm.hdu.edu.cn/showproblem.php?pid=1018)

输出:

第一类:一个Input Block对应的一个Output Block,Output Block之间没有空行。

例子:HDOJ_1089(代码以及输出请浏览上面输入第一类的例子)

 语法:

C:  printf("%d\n",ans);

C++:cout << ans << endl;

第二类:一个Input Block对应一个Output Block,每个Output Block之后都有空行。

例子:HDOJ_1095(http://acm.hdu.edu.cn/showproblem.php?pid=1095)

C:

[cpp] view plain copy
print?
  1. #include<cstdio>  
  2. int main()  
  3. {  
  4.     int a,b;  
  5.     while(scanf("%d%d",&a,&b) == 2)  
  6.     {  
  7.         printf("%d\n\n",a + b);  
  8.     }  
  9.     return 0;  
  10. }  

C++:

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int a,b;  
  6.     while(cin >> a >> b)  
  7.     {  
  8.         cout << a + b << endl << endl;  
  9.     }  
  10.     return 0;  
  11. }  

第三类:一个Input Block对应一个Output Block,Output Block之间有空行。

例子:HDOJ_1096(http://acm.hdu.edu.cn/showproblem.php?pid=1096)
C:

[cpp] view plain copy
print?
  1. #include<cstdio>  
  2. int main()  
  3. {  
  4.     int n,i,j,k,sum,tmp;  
  5.     scanf("%d",&n);  
  6.     for(i = 1; i <= n; i++)  
  7.     {  
  8.         scanf("%d",&k);  
  9.         sum = 0;  
  10.         for(j = 0; j < k; j++)  
  11.         {  
  12.             scanf("%d",&tmp);  
  13.             sum += tmp;  
  14.         }  
  15.         printf("%d\n",sum);  
  16.         if(i != n)  
  17.             printf("\n");  
  18.     }  
  19.     return 0;  
  20. }  

C++:

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int i,j,k,sum,tmp,n;  
  6.     cin >> n;  
  7.     for(i = 1; i <= n; i++)  
  8.     {  
  9.         cin >> k;  
  10.         sum = 0;  
  11.         for(j = 0; j < k; j++)  
  12.         {  
  13.             cin >> tmp;  
  14.             sum += tmp;  
  15.         }  
  16.         cout << sum << endl;  
  17.         if(i != n)  
  18.             cout << endl;  
  19.     }  
  20.     return 0;  
  21. }  

练习:

HDOJ_1016:http://acm.hdu.edu.cn/showproblem.php?pid=1016

HDOJ_1017:http://acm.hdu.edu.cn/showproblem.php?pid=1017


学有余力的读者可以尝试一下题目:(作者如完成一道便在对应题目加上超链接)

HDOJ:

1016-1018、1013、1061、1170、2000-2043


本文代码测试均在codeblocks下测试成功,并且在HDOJ能AC。


如有转载,请申明转载地址。

转载地址为:http://blog.csdn.net/wyrhero/article/details/8944542 






1 0
原创粉丝点击