1023 Have Fun with Numbers (20 分)
1023 Have Fun with Numbers (20 分)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 happen.
1023 Have Fun with Numbers (20 分)
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
AC代码
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
char A[30];
char B[30];
int Digit[11] = { 0 };
for (int i = 0; i < 22; i++) B[i] = ' '; //初始化为空格作为判断符
cin >> A; //数字以字符形式存储到数组里
int ALen = strlen(A); //记录数字位数
int Y, J, Add = 0; //J代表进位数 Y代表原位数
for (int i = ALen - 1; i >= 0; i--) {
Digit[A[i] - '0']++;
J = (A[i] - '0') * 2 / 10; //得到进位
Y = (A[i] - '0') * 2 % 10; //得到原位
B[i + 1] = (Y + Add) + '0'; //B[i + 1]代表整体向后存储一位将首位空出用来存放最后一次计算可能大于十的值
if (J > 0) Add = J; //Add作为进位参与计算
else Add = 0;
if (!i) B[i] = Add + '0'; //i为零时 首位放置Add的值
}
if (B[0] == '0') Digit[0]++; //首位放置Add的值若为零
for (int i = 0; B[i] != ' '; i++) Digit[B[i] - '0']--; //在原数存储基础上减数
bool Flag = true;
for (int i = 0; i < 10; i++) {
if (Digit[i] != 0) { //各数字出现次数与原数不一致时
cout << "No" << endl;
if (B[0] != '0') cout << B[0]; //判断第一位 不为零则输出
for (int i = 1; B[i] != ' '; i++) cout << B[i]; //顺次输出所得数
Flag = false;
break;
}
}
if (Flag) { //各数字出现次数与原数一致
cout << "Yes" << endl;
if (B[0] != '0') cout << B[0];
for (int i = 1; B[i] != ' '; i++) cout << B[i];
}
return 0;
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)