2010年9月18日 星期六

ACM 333 - Recognizing Good ISBNs

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[82];
int i, numCount, s, e;
while (gets(str))
{
if (strlen(str) == 0) { printf(" is incorrect.\n"); continue; }
numCount = 0;
int per = 0;
int tsum = 0;
int sum[9] = {0}, wrong = 0, d = 0, isX = 0, g = 0;
for (s = 0; str[s] == ' ' || str[s] == '\t' || isalpha(str[s]) ; s ++)
if (str[s] == 'X') break;
for (e = strlen(str) - 1; str[e] == ' ' || str[e] == '\t' || isalpha(str[e]); e --)
if (str[e] == 'X') break;
for (i = s; i <= e; i ++)
{
if (str[i] == '-') continue;
else if (isdigit(str[i]))
{
numCount ++;
per += str[i] - '0';
tsum += per;
}
else if (str[i] == 'X')
{
if (numCount != 9) { wrong = 1; break;}
else
{
per += 10;
tsum += per;
numCount ++;
}
}
else { wrong = 1; break;}
}
for (i = 0; str[i] == ' ' || str[i] == '\t'; i ++) ;
s = i;
for (i = strlen(str) - 1; str[i] == ' ' || str[i] == '\t'; i --) ;
e = i;
for (i = s; i <= e; i ++)
putchar(str[i]);
if (wrong) printf(" is incorrect.\n");
else if (numCount >= 10 && tsum % 11 == 0) printf(" is correct.\n");
else printf(" is incorrect.\n");

}
return 0;
}

回目錄
回首頁

沒有留言 :

張貼留言

Related Posts Plugin for WordPress, Blogger...