Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏
2015-08-03 15:49
369 查看
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,新算出的值可以作为下一个数的基底(如果后面还有数的话)。
只是奇怪为什么放在排序专题里????
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; }
相关文章推荐
- CentOS6.5中桌面上的面板被误删后,如何正常恢复?
- Ruby中的字符串编写示例
- HDU-2544-最短路-模板
- LeetCode_3 Longest Substring Without Repeating Characters
- 【Android应用开发技术:媒体开发】拍照
- Algorithms—83.Remove Duplicates from Sorted List
- C# 获取URL参数 NameValueCollection
- JavaScript 与 Objective-C 通信
- ViewPager,java.lang.UnsupportedOperationException: Required method destroyItem was not overridden
- xpath解析含有命名空间别名的xml文件的示例代码
- ORACLE 实用语句大集合
- Groovy操作POI
- AndroidTV/机顶盒 ListView获取焦点与点击事件问题处理方案
- angularjs calling order
- php基于session实现数据库交互的类实例
- 函数window.open实现关闭所有的子窗口
- 9.2链表(七)——检查链表是否为回文
- struts2整合json
- hdu 1969 pie
- LeetcCode #220 Contains Duplicate III