7-49 Have Fun with Numbers
Have Fun with Numbers
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
2469135798
题目注解
PTA的题目总是绕来绕去…被坑了好几次
简单翻译:
输入一个数位不超过20位的整数,
进行翻倍后输出,
如果翻倍后的数字是翻倍前的数字排列组合而成,则输出Yes,反之No。
tips:更简单的说,就是判断前后数字出现的次数是否相等!
tips:无论Yes or No,都要输出数字翻倍后的结果 !!!
无注释版代码
#include <stdio.h>
void Doub(char *,int);
void Count(char *,int*,int);
int judge(int *,int*);
int main () {
char arr[21]="0";
scanf("%20s",arr);
int before[10]= {0};
int after[10]= {0};
int len =sizeof(arr)/sizeof(arr[0]);
Count(arr,before,len);
Doub(arr,21);
Count(arr,after,len);
if(judge(before,after)) printf("Yes\n");
else printf("No\n");
for(int i=0; i<21; i++)
if(arr[i]!=0)printf("%c",arr[i]);
return 0;
}
void Count(char a[],int b[],int len) {
for(int i=0,w; a[i]!=0&&i<len; i++) {
w=a[i]-'0';
b[w]++;
}
}
int judge(int a[],int b[]) {
int temp=1;
for(int i=0; i<10; i++) {
if(a[i]!=b[i]) {
temp=0;
break;
}
}
return temp;
}
void Doub(char a[],int len) {
if(a[0]>'4') {
char te=a[0];
char mp;
a[0]='0';
for(int i=1; i<21; i++) {
mp=a[i];
a[i]=te;
te=mp;
}
}
int x=0;
for(int i=len-1; i>=0; i--) {
if(a[i]!=0) {
if(a[i]>'4') {
a[i]=a[i]*2-58;
if(x==1)a[i]++;
x=1;
} else {
a[i]=a[i]*2-'0';
if(x==1) {
a[i]++;
x=0;
}
}
}
}
}
注释版代码
#include <stdio.h>
//翻倍输入的数字
void Doub(char a[],int len);
//用于计入翻倍前后各个数字出现次数
void Count(char a[],int b[],int len);
//用于判断翻倍前后数字是否相等的值,返回1则相等,否则返回0;
int judge(int a[],int b[]);
int main () {
char arr[21]="0"; //20位超过unsignlong,所以使用字符串计算
scanf("%20s",arr); //限制输入的位数为20个
int before[10]= {0}; //翻倍前各个数字出现次数
int after[10]= {0}; //翻倍后各个数字出现次数
int len =sizeof(arr)/sizeof(arr[0]); //数组长度
Count(arr,before,len); //计入翻倍前各个数字出现个数
Doub(arr,21); //翻倍
Count(arr,after,len); //计入翻倍前各个数字出现个数
//判断
if(judge(before,after)) {
printf("Yes\n");
} else {
printf("No\n");
}
//输出 ps:!!!!!无论是否No,都要输出数字翻倍后的结果
for(int i=0; i<21; i++) {
if(arr[i]!=0)printf("%c",arr[i]);
}
return 0;
}
void Count(char a[],int b[],int len) {
for(int i=0,w; a[i]!=0&&i<len; i++) {
w=a[i]-'0';
b[w]++;
}
}
int judge(int a[],int b[]) {
int temp=1;
for(int i=0; i<10; i++) {
if(a[i]!=b[i]) {
temp=0;
break;
}
}
return temp;
}
void Doub(char a[],int len) {
/*判断第一位数是不是大于4,
是的话就要往前插一个'0',
这样后面的计算才能进位 ,
例如987654321,9位数,翻倍后是10位数,
所以翻倍前就要在前面插一个数字。
当然,这是我翻倍算法导致的,
如果你有更好的算法,
望告知,
*/
if(a[0]>'4') {
char te=a[0];
char mp;
a[0]='0';
for(int i=1; i<21; i++) {
mp=a[i];
a[i]=te;
te=mp;
}
}
int x=0;//用于判断是否进位
for(int i=len-1; i>=0; i--) {
if(a[i]!=0) {
if(a[i]>'4') {
a[i]=a[i]*2-58;
if(x==1)a[i]++;
x=1;
} else {
a[i]=a[i]*2-'0';
if(x==1) {
a[i]++;
x=0;
}
}
}
}
}