24h購物| | PChome| 登入
2014-02-17 08:34:39| 人氣1,495| 回應0 | 上一篇 | 下一篇

[UVA][字串分析] 10442 - Basic

推薦 0 收藏 0 轉貼0 訂閱站台

Problem D: Basic

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.

The first line of input contains a positive integer n. n lines follow. 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". Input lines contain no spaces and are between 1 and 80 characters in length.

Sample Input

5
2#101#
2#101##123#
17#abc#
16#123456789abcdef#
16#123456789abcdef#123456789abcdef#

Output for Sample Input

yes
yes
no
yes
no


題目描述:

給定數字的輸入格式

"數字" <- "進制" + # + "表示法" + #

"數字" <- "數字" + # + "表示法" + #
進制數最多為 2 - 16 之間。

檢查是否為合法表示。

#include <stdio.h>
#include <string.h>
int isValid(char s[]) {
char sdigit[] = "0123456789abcdef";
int mdigit[256];
memset(mdigit, -1, sizeof(mdigit));
for(int i = 0; sdigit[i]; i++)
mdigit[sdigit[i]] = i;
int base = 10, fbase = 1;
char *p = s;
while(*p) {
int number = 0, excess = 0;
for(; *p != '\0' && *p != '#'; p++) {
if(mdigit[*p] == -1) return 0;
if(mdigit[*p] >= base) return 0;
number = number * base + mdigit[*p];
if(number > 16) excess = 1;
}
//printf("%d %d\n", number, fbase);
if(*p == '#') {
p++;
//printf("xxx %c\n", *p);
if(*p == '#') {
if(excess || number < 2)
return 0;
base = number;
p++;
if(*p == '\0')
return 0;
} else if(*p == '\0') {
return 1;
} else {
if(fbase) {
if(excess || number < 2)
return 0;
base = number;
fbase = 0;
} else {
return 0;
}
}
} else {
if(*p == '\0' && fbase)
return 1;
return 0;
}
}
return 1;
}
int main() {
int testcase;
char s[128];
scanf("%d", &testcase);
while(testcase--) {
scanf("%s", &s);
puts(isValid(s) ? "yes" : "no");
}
return 0;
}
/*
10
2#10##
2##
#2#
*/

台長: Morris
人氣(1,495) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][floyd] 10246 - Asterix and Obelix
此分類上一篇:[UVA] 10426 - Knights' Nightmare

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文