大位数乘法乘法
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!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
Output Specification:
For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.
Sample Input: 1234567899
Sample Output: Yes\n2469135798
#include< stdio.h> #include< stdbool.h> #include< string.h> char doubleit(char str[], int x); bool ist(char str[], int length); char ch = 32;//创建一个可能多出一位的最大的位数的数 bool ist(char str[],int last)//是否两者的数都互有涵盖 { int arry[10] = { 0 }, arrys[10] = { 0 };//记得桶排 for (int x = 0; x <= last; x++) arry[str[x] - 48] = 1; ch=doubleit(str, last); for (int x = 0; x <= last; x++) arrys[str[x] - 48] = 1; if(ch!=32) arrys[ch - 48] = 1; int flag=1; for (int x = 0; x <= 9; x++)//比较同样记得可以用strcamp(字符串比较时) if (arry[x] != arrys[x])flag = 0; if (flag) return true;//0是false,非0都是true else return false; } char doubleit(char str[], int last) { int x = 0,temp,lastone=0;//lastone用来记录进位,temp作为一个中间值 for ( x = last; x>=0 ; x--)//开始乘法 { temp = (str[x] - 48) * 2 ;//取模 str[x] = temp%10 + 48+lastone; lastone = temp / 10; if (x == 0 && lastone != 0) ch = lastone + 48; } return ch; }
int main() { char str[22]; int lastnumber; scanf("%s", str);//scanf会自动补'\0' lastnumber = strlen(str)-1;//strlen不包括'\0' if (ist(str, lastnumber))printf("Yes\n"); else printf("No\n"); if (ch != 32) printf("%c%s", ch, str); else printf("%s", str); return 0; }
总结
大数据做运算用字符串来存放,注意的点
1.数据是“倒序存放”,这时就一定注意可能多出来一位,需要再构建一位
2.进位问题,有可能是1也有可能是2,所以用取模的方法就较为简单
3.若有对比问题,flag或者memset都要想到
4.同样可以建多数组,将计算结果直接存放在新数列