sscanf函数具体用法举例,与其他函数(例如atoi)的比较

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函数,不过有点麻烦!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值