Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏

时间:2023-01-09 22:35:23

Basic

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 905 Accepted: 228

Description

The programming language Ada has integer constants that look like this: 123, 8#123#, 16#abc#. These constants represent the integers 123, 83 (123 base 8) and 2739 (abc base 16). More precisely, an integer may be a decimal integer given as a sequence of one or more digits less than 10, or it may be an integer to some specific base, given as the base followed by a sequence of one or more digits less than the base enclosed by # symbols. Lower case letters from a through f are used as the digits representing 10 through 15. In Ada, the base, if specified, must be a sequence of decimal digits. For this problem, however, the base may be of any form described above so long as it represents an integer between 2 and 16 inclusive.

Input

The first line of input contains a positive integer n. n lines follow.Input lines contain no spaces and are between 1 and 80 characters in length.

Output

For each line of input, output a line “yes” if it is a valid integer constant according to the above rules; otherwise output a line containing “no”.

Sample Input

5

2#101#

2#101##123#

17#abc#

16#123456789abcdef#

16#123456789abcdef#123456789abcdef#

Sample Output

yes

yes

no

yes

no

Source

Waterloo local 2003.01.25

奇葩题,看了好几遍,把百度,谷歌都用上只能说,不懂

题意就是:

给一串字符串,判断是否合法。合法情况为:第一个数字在2至16间,表示进制的基底,然后是一个用两个#包含在内的数字,表示该进制下的数字,用a到f表示10到15,新算出的值可以作为下一个数的基底(如果后面还有数的话)。

只是奇怪为什么放在排序专题里????

#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define WW freopen("output.txt","w",stdout)
#define RR freopen("input.txt","r",stdin) using namespace std; const int MAX=100100; char s[110]; int Oper(int sum,int i)
{
if(sum<2||sum>16)
{
return 0;
}
if(s[i]=='#')
{
return 0;
}
double ans=0;//这里要用double,用long long 都过不了,可能有的字符串太长会损失精度
int j;
for(j=i;s[j];j++)
{
if(s[j]=='#')
{
break;
}
if(sum>10)
{
if(s[j]>='0'&&s[j]<='9')
{
ans=ans*sum+s[j]-'0';
}
else if(s[j]>='a'&&s[j]<=sum-11+'a')
{
ans=ans *sum+10+s[j]-'a';
}
else
{
return 0;
}
}
else if(sum<=10)
{
if(s[j]>='0'&&s[j]<=sum-1+'0')
{
ans=ans*sum+s[j]-'0';
}
else
{
return 0;
}
}
}
if((s[j]=='#'&&s[j+1]=='\0')||(s[j]=='#'&&s[j+1]=='#'&&Oper(ans,j+2)))
{
return 1;
}
else
{
return 0;
}
} int main()
{
int n,i;
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
int ans=0;
for(i=0;;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
ans=ans*10+s[i]-'0';
}
else
{
break;
}
}
if((s[i]=='#'&&Oper(ans,i+1))||(s[i]=='\0'&&ans==0))
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。