标签:HERE cat not bsp ++ strlen contain only case
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
大整数与int型整数的乘法;简单哈希。
#include <cstdio> #include <cstring> struct bign{ int digit[30]; int len; bign(){ memset(digit,0,sizeof(digit)); len=0; } }; bign change(char str[]) { bign c; c.len=strlen(str); int n=c.len; for(int i=0;i<n;i++){ c.digit[i]=str[n-1-i]-‘0‘; } return c; } bign multiply(bign a,int b) { bign c; int carry=0; for(int i=0;i<a.len;i++){ int temp=a.digit[i]*b+carry; c.digit[c.len++]=temp%10; carry=temp/10; } while(carry>0){ c.digit[c.len++]=carry%10; carry/=10; } return c; } void print(bign a) { for(int i=a.len-1;i>=0;i--) printf("%d",a.digit[i]); } int main() { char str[30]; scanf("%s",str); int hashtable[10]; memset(hashtable,0,sizeof(hashtable)); int len=strlen(str); for(int i=0;i<len;i++) hashtable[str[i]-‘0‘]++; bign a=change(str); bign double_a=multiply(a,2); for(int i=0;i<double_a.len;i++) hashtable[double_a.digit[i]]--; bool flag=true; for(int i=0;i<10;i++){ if(hashtable[i]!=0) { flag=false; break; } } if(flag==true) printf("Yes\n"); else printf("No\n"); print(double_a); return 0; }
标签:HERE cat not bsp ++ strlen contain only case
原文地址:https://www.cnblogs.com/kkmjy/p/9532480.html