ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)

ZOJ 2971 Give Me the Number 题目

ZOJ 2311 Inglish-Number Translator 题目

//两者题目差不多,细节有点点不一样,因为不是一起做的,所以处理方式有一点点不一样——还是前一个方式高端呀。

//ZOJ 2971 的AC代码(用了strstr函数):

#include<stdio.h>
#include<string.h>
char c[][] = {"zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine", "ten" ,
"eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen", "twenty" ,
"thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" , "hundred" , "thousand" , "million" , "and"};
char s[],sss[];
int main()
{
int ne , sum , flag , i,j , acc,len;
int tt;
scanf("%d",&tt);
getchar();
while(tt--)
{
gets(s);
ne = ;
sum = ;
flag = ;
acc = ;
len=strlen(s);
while (flag <len)
{
while (s[flag] == ' ') flag++;
// while (s[flag] == 'and') flag++;
for (i = ; i >= ; i--)
{
if (strstr(&s[flag] , c[i]) == &s[flag])//在s中找c[i]如果找到了,返回位置,没有找到返回-1,即false;&取位置
{
if (i <= ) acc += i;
else if (i <= ) acc += (i - ) * ;
else if (i == ) acc *= ;
else if (i == )
{
sum += acc * ;
acc = ;
}
else if (i == )
{
sum += acc * ;
acc = ;
}
else if (i == ) ne = -;
flag += strlen(c[i]);
break;
}
}
}
printf("%d\n" , (sum + acc)); }
return ;
}

字符串转化为数

//ZOJ 2311 的AC代码(这题要注意,防空行,就是一行什么都没输入,那么也什么都不要输出):

//模拟
//英文句子转阿拉伯数字。 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int main()
{
int len,i,j,k,num,sign,ans;
char str[],word[];
char w[][]={"negative", "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty",
"ninety", "hundred", "thousand", "million"};
while(gets(str))
{
if(strcmp(str,"")==)continue;//防空行
len=strlen(str);
str[len++]=' ';
num=;
j=;
sign=;
ans=;
for(i=;i<len;i++)
{
if(str[i]!=' ')
word[j++]=str[i];
else
{
word[j++]='\0';
for(k=;k<;k++)
{
if(strcmp(w[k],word)==)
{
if(k==)
sign=-;
else if(k>=&&k<=)
num+=k-;
else if(k>=&&k<=)
num+=(k-)*;
else if(k==)
num=num*;
else if(k==)//只要考虑1000
{
ans+=num*;
num=;
}
else if(k==)//和1000000就可以了
{
ans+=num*;
num=;
}
break;
}
}
j=;
}
}
ans+=num;
printf("%d\n",ans*sign);
}
return ;
}
上一篇:linux笔记:文件系统管理-fdisk分区


下一篇:Charles代理配置截取https iOS11