浅结在OJ中的输入格式问题(总结可能多处不足与错误,发现请各位大咔评论指导)

来源:互联网 发布:webshell箱子系统v2.0 编辑:程序博客网 时间:2024/06/06 05:49
链接题目已附上
http://ccpc.ahu.edu.cn:8080/OJ/Problem.aspx?id=1                A+B Problem 
http://ccpc.ahu.edu.cn:8080/OJ/Problem.aspx?id=177           A+B Problem (EOF)
http://ccpc.ahu.edu.cn:8080/OJ/Problem.aspx?id=178           A+B Problem (Case Count)
http://ccpc.ahu.edu.cn:8080/OJ/Problem.aspx?id=179           A+B Problem (0)
http://ccpc.ahu.edu.cn:8080/OJ/Problem.aspx?id=180           A+B Problem (0+EOL)
http://ccpc.ahu.edu.cn:8080/OJ/Problem.aspx?id=509           A+B Problem (64bit Integer + EOF)  


针对第一种 A+B Problem  问题自行进链接
最简单的输入  ;类似于"hello word"的输出与平时所用一样输入一组数在用这组数输出计算即可
Description
Calculate the result of a+b

Input
Two space seperated integer a,b (0<=a,b<=10)

Output
Output a+b in a single line

Sample Input
OriginalTransformed
1 2

Sample Output
OriginalTransformed
3

Hint
Don't output extra charactors.
Don't forget the newline charactor(s).


1#include<stdio.h>

 int main()  

 {  

 int a,b;  

scanf("%d %d",&a, &b);  

printf("%d\n",a+b); //最简单的输入

return 0;  

}  

2.A+B Problem (EOF) 

 

Description
计算A+B

Input
输入第1行为一个整数n(1≤n≤10),代表测试的组数。 
下面有n组测试数据,每组1行,为2个整数,为A, B, A,B∈[0,32767]。

Output
输出A+B的值。

Sample Input
OriginalTransformed
21 23 4

Sample Output
OriginalTransformed
37

       输入多组数据(未要求什么时候结束)但要用EOF作为结束

2.#include <stdio.h>  
int main()   
{  
    int a,b;  
    while(scanf("%d %d",&a, &b) != EOF) // 输入结束时,scanf函数返回值为EOF为-1,当输入非正确格式的数值和无数值输入时跳出循环提前结束
   {  
        printf("%d\n",a+b);  
   }  
    return 0;   
}  

3. A+B Problem (Case Count)     当以组数输出时组数是第一个输出的数字决定的

Description
计算A+B

Input
输入第1行为一个整数n(1≤n≤10),代表测试的组数。 
下面有n组测试数据,每组1行,为2个整数,为A, B, A,B∈[0,32767]。

Output
输出A+B的值。

Sample Input
OriginalTransformed
21 23 4

Sample Output
OriginalTransformed
37


#include<stdio.h>
int main()
{
int i ,n;
int a ,b;
scanf("%d",&n);
for (i=0;i<n;i++)//定义一个输出的组数循环里面输出可以表示多组输出
{
scanf("%d %d",&a, &b);
printf("%d",a+b);
}
return 0;
}

4.A+B Problem (0)    输入不说明有多少组数据,但以特殊输入 (0  0)为结束标志。只要达到这个条件就结束,否者无限输出

Description
计算A+B

Input
输入数据有多组。 
每组一行,为两个整数A, B, A,B∈[0,32767]。 
输入以0 0结束。

Output
输出A+B的值。

Sample Input
OriginalTransformed
1 20 0

Sample Output
OriginalTransformed
3


#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)&&(a||b))//根据scanf函数的返回值与(a  b)是否同时为假来判断是否继续循环
{
printf("%d\n",a+b);
}
return 0;

}

5.  A+B Problem (0+EOL)    输入不说明有多少组数据,但以特殊输入 (0  0)为结束标志,但输入格式中间与结束的不同要加上判断



#include<stdio.h>
int main()
{
int i,a,b,t;
while(scanf("%d %d",&a, &b)&&(a||b))
{

printf("%d\n\n",a+b);



}


printf("\n"); //结束时特殊加上与上面数不同的输出格式

return 0;
}


6     A+B Problem (64bit Integer + EOF)      以64位的输出输入格式进行计算


Description
Calculate the result of a+b

Input
Line i: two integer a and b seperated by one space
Multiple cases, end with EOF
0<=a,b,(a+b)<=263-1

Output
Line i: (a+b)
the corresponding result of a+b

Sample Input
OriginalTransformed
573247196999136902 11718740113834620592093017816426442939 11726439800073197152560745550527527105 3566574549894016800

Sample Output
OriginalTransformed
174512120838259896132656617964337626546127320100421543905
 

#include<stdio.h>
int main()
{
__int64 a,b; // 定义时用上长长整形  详情见下表
while(scanf("%I64d %I64d",&a, &b)!=EOF)
{
printf("%I64d\n",a+b);
}

return 0;

}


  • 有符号型64位整数,值域为:-9223372036854775808 .. 9223372036854775807。

    语言GNU C/C++PascalVisual C/C++类型名称__int64
    or
    long longint64__int64输入方法scanf("%I64d", &x);
    or
    cin >> x;read(x);scanf("%I64d", &x);输出方法printf("%I64d", x);

    cout << x;write(x);printf("%I64d", x);
  • 无符号型64位整数,值域为:0 .. 18446744073709551615。

    语言GNU C/C++PascalVisual C/C++类型名称unsigned __int64
    or
    unsigned long longqwordunsigned __int64输入方法scanf("%I64u", &x);
    or
    cin >> x;read(x);scanf("%I64u", &x);输出方法printf("%I64u", x);
    or
    cout << x;write(x);printf("%I64u", x);

   -------------------------------------------------------------------------------------------------------------------------------------------------------

各种各样的OJ输入输出格式,在看清楚题目的同时,要善于利用函数的的返回值去判断下手


阅读全文
0 0
原创粉丝点击