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

本文详细解析了多种A+B问题的解决方法,包括基本输入输出、处理多组数据、使用EOF及特定标志结束输入等,涵盖了从简单到复杂的各类场景。
链接题目已附上
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
Original Transformed
1 2

Sample Output
Original Transformed
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
Original Transformed
2
1 2
3 4

Sample Output
Original Transformed
3
7

       输入多组数据(未要求什么时候结束)但要用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
Original Transformed
2
1 2
3 4

Sample Output
Original Transformed
3
7


#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
Original Transformed
1 2
0 0

Sample Output
Original Transformed
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
Original Transformed
573247196999136902 1171874011383462059
2093017816426442939 1172643980007319715
2560745550527527105 3566574549894016800

Sample Output
Original Transformed
1745121208382598961
3265661796433762654
6127320100421543905
 

#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 long
    int64__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 long
    qwordunsigned __int64
    输入方法scanf("%I64u", &x);
    or
    cin >> x;
    read(x);scanf("%I64u", &x);
    输出方法printf("%I64u", x);
    or
    cout << x;
    write(x);printf("%I64u", x);

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值