题目描述:
如果一个数(可能包含前导0)乘以2,如142857*2=285714,
依然由原来的数字组成的话,这样的数(142857)就叫Magic Number
输入:
多组测试数据,每组一行,每行是一个不大于1e10000整数(可能有前导0),
遇到EOF标志结束程序
输出:
判断这个数是不是Magic Number,是的话输出“Magic Number”
否则输出“Not Magic Number”
样例输入:
142857
0142857
857142
0588235294117647
样例输出:
Magic Number
Magic Number
Not Magic Number
Magic Number
难度:Very Easy
CODE:
#include <stdio.h>
#include <string.h>
#define MAX 10001
typedef struct {
int date[MAX];
int len;
}BigInt;
int magic(BigInt a,int m);
int main() {
char str[MAX];
BigInt a;
int i,len;
while ( scanf("%s",str) != EOF ) {
len = strlen(str);
for ( i = 0; i < len; i++ ) {
a.date[len-i-1] = str[i] - '0';
}
a.len = len;
if ( magic(a,2) ) {
printf("Magic Number\n");
} else {
printf("Not Magic Number\n");
}
}
return 0;
}
int magic(BigInt a,int m) {
int i,temp;
int carry = 0;
int s[10],e[10];
for( i = 0; i < 10; i++ ) {
s[i] = e[i] = 0;
}
for ( i = 0; i < a.len; i++ ) {
s[a.date[i]] ++;
}
for ( i = 0; i < a.len; i ++ ) {
temp = a.date[i] * m + carry;
e[temp%10] ++;
carry = temp / 10;
}
if ( carry ) {
e[carry] ++;
}
for ( i = 0; i < 10; i++ ) {
if ( s[i] != e[i] ) {
return 0;
}
}
return 1;
}
CODE1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int a[10005],old[10],mulold[10];
memset(old,0,sizeof(old));
memset(mulold,0,sizeof(mulold));
char c;
int i=0,j,k,bit=0,number=0;
bool flag=true;
for (;scanf("%c",&c)!=EOF;i=0,bit=0,number=0,flag=true) {
//scanf("%c",&c);
while(c!='\n') {
/*if(i==0&&c=='0') {
while(c=='0')
scanf("%c",&c);
}
if(c=='\n')
break;*/
a[i]=c-'0';
old[c-'0']=1;
i++;
scanf("%c",&c);
}
//for(j=0;j<i;j++) printf("%d ",a[j]);
/*if(a[0]==0) {
printf("Magic Number \n");
flag=false;
}
else*/
for(j=0;j<i;j++) {
int tmp=a[i-1-j]*2+bit;
//printf("tmp:%d ",tmp);
if(j==i-1&&tmp>=10) {
if(old[tmp/10]==0)
{
printf("*Not Magic Number \n");
flag=false;
break;
}
mulold[tmp/10]=1;
}
if(old[tmp%10]==0)
{
printf("*Not Magic Number \n");
flag=false;
break;
}
mulold[tmp%10]=1;
bit=tmp/10;
}
//for(k=0;k<10;k++) {printf("%d ",old[k]);printf("%d\n",mulold[k]);}
if(flag) {
for(k=0;k<10;k++)
if(old[k]!=mulold[k]) {
printf("Not Magic Number \n");
break;
}
if(k==10)
printf("Magic Number \n");
}
memset(old,0,sizeof(old));
memset(mulold,0,sizeof(mulold));
}
return 0;
}
CODE2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int a[10005],old[10],mulold[10];
memset(old,0,sizeof(old));
memset(mulold,0,sizeof(mulold));
char c;
int i=0,j,k,bit=0,number=0;
bool flag=true;
for (;scanf("%c",&c)!=EOF;i=0,bit=0,number=0,flag=true) {
//scanf("%c",&c);
while(c!='\n') {
/*if(i==0&&c=='0') {
while(c=='0')
scanf("%c",&c);
}
if(c=='\n')
break;*/
a[i]=c-'0';
old[c-'0']=1;
i++;
scanf("%c",&c);
}
//for(j=0;j<i;j++) printf("%d ",a[j]);
/*if(a[0]==0) {
printf("Magic Number \n");
flag=false;
}
else*/
for(j=0;j<i;j++) {
int tmp=a[i-1-j]*2+bit;
//printf("tmp:%d ",tmp);
if(j==i-1&&tmp>=10) {
if(old[tmp/10]==0)
{
printf("Not Magic Number \n");
flag=false;
break;
}
mulold[tmp/10]=1;
}
if(old[tmp%10]==0)
{
printf("*Not Magic Number \n");
flag=false;
break;
}
mulold[tmp%10]=1;
bit=tmp/10;
}
//for(k=0;k<10;k++) {printf("%d ",old[k]);printf("%d\n",mulold[k]);}
if(flag) {
for(k=0;k<10;k++)
if(old[k]!=mulold[k]) {
printf("Not Magic Number \n");
break;
}
if(k==10)
printf("Magic Number \n");
}
memset(old,0,sizeof(old));
memset(mulold,0,sizeof(mulold));
}
return 0;
}