Sunday, December 9, 2007

The Huge One

ACCEPTED !!!  really HUGE but straightforward ...
____________________________________________________________________
Question: The Huge One
____________________________________________________________________

#include "iostream.h"
#include "string.h"

int divisible(char *num, int k) {
int len = strlen(num), v, v0, v1, v2, last2, last3, sum, sum1, sum2, i;
char bkp[1002];
switch(k) {
case 1:
return 1;
case 2:
v0 = num[len - 1] - '0';
if(v0 % 2 == 0) return 1;
return 0;
case 3:
sum = 0;
for(int i = 0; i < len; i++) sum += num[i] - '0';
if(sum % 3 == 0) return 1;
return 0;
case 4:
v0 = num[len - 1] - '0';
v1 = num[len - 2] - '0';
last2 = v1 * 10 + v0;
if(last2 % 4 == 0) return 1;
return 0;
case 5:
if((num[len - 1] - '0') % 5 == 0) return 1;
return 0;
case 6:
v0 = num[len - 1] - '0';
sum = 0;
for(int i = 0; i < len; i++) sum += num[i] - '0';
if(v0 % 2 == 0 && sum % 3 == 0) return 1;
return 0;
case 7:
strcpy(bkp, num);
i = 0;
while(i < len - 1) {
v1 = bkp[i] - '0';
v0 = bkp[i + 1] - '0';
v = v1 * 3 + v0;
bkp[i] = v / 10 + '0';
bkp[i + 1] = v % 10 + '0';
if(bkp[i] == '0') i++;
}
if((bkp[i] - '0') % 7 == 0) return 1;
return 0;
case 8:
v0 = num[len - 1] - '0';
v1 = num[len - 2] - '0';
v2 = num[len - 3] - '0';
last3 = v2 * 100 + v1 * 10 + v0;
if(last3 % 8 == 0) return 1;
return 0;
case 9:
sum = 0;
for(int i = 0; i < len; i++) sum += num[i] - '0';
if(sum % 9 == 0) return 1;
return 0;
case 10:
if((num[len - 1] - '0') == 0) return 1;
return 0;
case 11:
sum1 = 0;
sum2 = 0;
for(int i = 0, j = 1; i < len || j < len; i += 2, j += 2) {
if(i < len) sum1 += num[i] - '0';
if(j < len) sum2 += num[j] - '0';
}
if((sum1 - sum2) % 11 == 0) return 1;
return 0;
case 12:
sum = 0;
for(int i = 0; i < len; i++) sum += num[i] - '0';
v0 = num[len - 1] - '0';
v1 = num[len - 2] - '0';
last2 = v1 * 10 + v0;
if(sum % 3 == 0 && last2 % 4 == 0) return 1;
return 0;
default:
return 1;
}
}

int main()
{
int n;
cin >> n;
for(int t = 1; t <= n; t++) {
char hugenum[1002];
cin >> hugenum;
int m;
cin >> m;
int j, k;
for(j = 0; j < m; j++) {
cin >> k;
int r = divisible(hugenum, k);
if(r == 0) break;
}
cout << hugenum;
if(j == m) cout << " - Wonderful." << endl;
else cout << " - Simple." << endl;
while(m >
++j) cin >> k;
}
return 0;
}
____________________________________________________________________

No comments:

Contributors