1023. Have Fun with Numbers (20)
来源:互联网 发布:mysql scope identity 编辑:程序博客网 时间:2024/06/01 22:13
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 file 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:1234567899Sample Output:
Yes2469135798
#include<iostream>#include<string>#include<string.h>using namespace std;string add(string, string);int main(){int ori_num[10];int ans_num[10];memset(ori_num, 0, sizeof(int)* 10);memset(ans_num, 0, sizeof(int)* 10);string ori, ans;cin >> ori;ans = add(ori, ori);int length[2] = { 0, 0 };length[0] = ori.size();length[1] = ans.size();for (int i = 0; i < length[0]; i++){//统计数中0~9分别出现过多少次ori_num[ori[i] - '0'] ++;}for (int i = 0; i < length[1]; i++){ans_num[ans[i] - '0'] ++;}for (int i = 0; i < 10; i++){if (ans_num[i] != ori_num[i]){cout << "No" << ans << endl;break;}if (i == 9){cout << "Yes\n" << ans <<endl;}}return 0;}string add(string a, string b){char ans[30];int length = a.size();//同一个数,所以位数相同int carry = 0;for (int i = length-1; i >=0; i--){ans[i] = a[i] + b[i] - '0' + carry;if (ans[i] - '0' > 9){ans[i] -= 10;carry = 1;}elsecarry = 0;}ans[length] = 0;string ret = ans;if (carry == 1)//最高位有可能有进位ret = "1"+ret;return ret;}
- 1023. Have Fun with Numbers (20)
- 【PAT】1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- PAT: 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- PAT 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- PAT 1023. Have Fun with Numbers (20)
- PAT 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- Java的反射机制
- C++学习笔记(一)
- Codevs 1035 火车停留 [费用流] [拆点]
- Android view背景动态改变带简单淡入淡出动画
- poj3260&&hdu3591 多重背包+完全背包
- 1023. Have Fun with Numbers (20)
- 动态库的编写和使用
- POJ 3601 Tower of Hanoi
- Effecitive C++_Item20笔记
- 多线程——继承Thread 类和实现Runnable 接口的区别
- 公告
- JAVA ASCII码 读入字符串测试
- C与C++中的enum类型使用
- 在Ubuntu中安装Visual Studio Code