今天,做了一道传说中的水题,但从做水题中突然发现了,以前很少使用但是用时却非常好用且方便的函数(sscanf);
函数在#include<stdio.h>中。
使用格式为sscanf(s,"HH:MM:SS",&HH,&MM,&SS);
可以根据你的需要更改“ ”中的格式,其中s为字符串。
ex:
12:02:12
如果需要得到其中的整数12,2,12可以这样
sscanf(s,"%d:%d:%d",&HH,&MM,&SS);
#include<stdio.h>
#include<string.h>
char str[12];
bool swap(char *str)
{
int HH,MM,SS,sum=0;
int len=strlen(str);
for(int i=0;i<len;i++)
if(str[i]=='?')
return false;
if(str[1]=='+'||str[2]=='+'){
sscanf(str,"%d+%d=%d",&HH,&MM,&SS);
sum=HH+MM;
if(sum==SS)return true;
}
else{
sscanf(str,"%d-%d=%d",&HH,&MM,&SS);
sum=HH-MM;
if(sum==SS)return true;
}
return false;
}
int main()
{
int cnt=0;
while(scanf("%s",str)!=EOF)
{
if(swap(str))
cnt++;
memset(str,'\0',sizeof(str));
}
printf("%d\n",cnt);
return 0;
}