Help a PhD candidate out!解题报告

来源:互联网 发布:数据字典 javaweb 编辑:程序博客网 时间:2024/05/18 01:38

题目摘要:Jon Marius forgot how to add twonumbers while doing research for his PhD. And now he has a long list of additionproblems that he needs to solve, in addition to his computer science ones!Canyou help him?On his current list Jon Marius has two kinds of problems: additionproblems on the form "a+b" and the ever returning problem"P=NP".Jon Marius is a quite distracted person, so he might have so solvethis last problem several times, since he keeps forgetting the solution.Also,he would like to solve these problems by himself, so you should skip these.

题目大意:若是出入a+b,那么输出其值。如果输入P=NP,那么输出skipped。

输入输出要求

Input

The first line of input will be a singleinteger N

(1<=N<=1000) denoting the number of testcases.

Then follow N lines with either"P=NP" or an addition problem on the form "a + b",

where a, b∈ [0, 1000] are integers.

 

Output

Output the result of each addition. Forlines containing \P=NP", output \skipped".

输入输出样例

Sample input

4

2+2

1+2

P=NP

0+0

 

Sample output

4

3

skipped

0

 

解题思路:用一个字符数组储存输入的内容,如果第一个字符是P,那么输出skipped。否则的话用sscanf提取数字a,b,然后输出a+b即可。

代码

#include<stdio.h>

#include<string.h>

char str[20];

int main()

{

   int n;

   while(scanf("%d",&n)!=EOF)

    {

       getchar();

       while(n--)

       {

           memset(str,'\0',sizeof(str));

           int a,b;

           gets(str);

           if(str[0]=='P')

                printf("skipped\n");

           else

           {

               sscanf(str,"%d+%d",&a,&b);

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

           }

       }

    }

   return 0;

}

解题感想:巧用sscanf,so easy!