sscanf()函数具体用法举例,与其他函数的比较
例题:
Help a PhD candidate out! |
Description |
Jon Marius forgot how to add two numbers while doing research for his PhD. And now he has a long list of addition problems that he needs to solve, in addition to his computer science ones! Can you help him? On his current list Jon Marius has two kinds of problems: addition problems on the form "a+b" and the ever returning problem "P=NP". Jon Marius is a quite distracted person, so he might have so solve this last problem several times, since he keeps forgetting the solution. Also, he would like to solve these problems by |
Input |
The rst line of input will be a single integer 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 0≤ a,b≤1000 are integers. |
Output |
Output the result of each addition. For lines containing “P=NP", output ”skipped". |
Sample Input |
4 2+2 1+2 P=NP 0+0 |
Sample Output |
4 3 skipped 0 |
Source |
NCPC 2010 |
题的意思就是:输入一些东西,如果是整形的数+整形的数,就输出相加后的结果,如果是P=NP,则结果就是:skipped;
题目很简单吧!如果不懂一些库函数的用法的话,就不会accept!
先用sscanf函数:代码和注释如下:
#include<stdio.h>
int main()
{
char s[10];
int a,b,n;
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
if(s[0]=='P')
printf("skipped\n");
else
{
sscanf(s,"%d+%d",&a,&b);
printf("%d\n",a+b);
}
}
return 0;
}
也可以用atoi函数,不过有点麻烦!