1023 Have Fun with Numbers
来源:互联网 发布:mysql更新语句 编辑:程序博客网 时间:2024/05/17 08:37
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:
1234567899
Sample Output:
Yes
2469135798
解题思路:一开始记录各个数字出现的次数,然后double一下,看原先的数字是否够新的数字的组成。
#include<iostream>#include<string>#include<algorithm>#include<stdio.h>using namespace std;int main(){ for (string ss; cin >> ss;){ int flag[10] = { 0 }; for (int i = 0; i < ss.length(); i++){ flag[ss[i] - '0'] ++; } reverse(ss.begin(), ss.end()); string double_s = ""; int carry = 0; for (int i = 0; i < ss.length(); i++){ double_s = double_s + (char)(((ss[i] - '0') * 2 + carry) % 10 + '0'); carry = ((ss[i] - '0') * 2 + carry) / 10; } if (carry){ double_s += (char)(carry + '0'); } reverse(double_s.begin(), double_s.end()); int test = 1; for (int i = 0; i < double_s.length(); i++){ if (flag[double_s[i] - '0']){ flag[double_s[i] - '0']--; } else{ test = 0; break; } } if (test){ printf("Yes\n"); cout << double_s << endl; } else{ printf("No\n"); cout << double_s << endl; } } return 0;}
- 1023 Have Fun with Numbers
- Have Fun with Numbers
- Have Fun with Numbers
- Have Fun with Numbers
- PAT 甲级 1023 Have Fun with Numbers
- 1023 Have Fun with Numbers (20) --大数
- PAT-1023-Have Fun with Numbers
- pat a 1023 Have Fun with Numbers
- 1023. Have Fun with Numbers
- 1023. Have Fun with Numbers
- PAT_1023: Have Fun with Numbers
- 1023. Have Fun with Numbers
- 1023. Have Fun with Numbers
- 1023.Have Fun with Numbers
- pat1023 Have Fun with Numbers
- Have Fun with Numbers (20)
- pat Have Fun with Numbers
- PAT Have Fun with Numbers
- android启动模式笔记
- Java常用工具类(3)
- 自定义 Lint 规则简介
- Java并发编程:volatile关键字解析
- poi导出Excel 不会产生垃圾文件的导出方式
- 1023 Have Fun with Numbers
- hdu 3829 Cat VS Dog(最大独立集)
- Eclipse常用快捷键
- JavaScript动态加载ul标签
- 安装Zabbix过程中出现的问题集
- Android手动打包:jar某目录下存在两个同名(不区分大小写)class文件,解压后如何再打包回dex
- React 环境搭建
- C++中的4个与类型转换相关的关键字
- iOS小技巧