题目

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 diferent 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

题目分析

已知一个最多20位的数字N,判断N乘2后的数字M是否只是组成N的各位数字的重新排列

解题思路

  1. 用一个数组记录N中各位数字出现的次数,乘2后的数字M中出现相同数字则出现次数减一(该判断隐含M和N的长度不相等的情况,长度不相等则出现次数一定不相等,数组中出现次数不相等时元素值不为0)
  2. 大整数相乘算法

易错点

  1. 字符和数字的转换(字符-'0'=数字)

知识点

  1. 字符串转换
#include <algorithm>
reverse(ds.begin(),ds.end());

Code

Code 01

#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc,char * argv[]) {
string s,ds;
cin>>s;
int sf[10]= {0},carry=0;
for(int i=s.length()-1; i>=0; i--) {
sf[s[i]-'0']++;
int temp=(s[i]-'0')*2+carry;
ds.push_back(temp%10+'0');
carry=temp/10;
sf[temp%10]--;
}
while(carry!=0) {
ds.push_back(carry%10+'0');
sf[carry%10]--;
carry/=10;
} bool flag = true; // strrev(s.c_str());
// if(ds.length()!=s.length())flag=false;
// else {}
for(int i=0; i<10; i++) {
if(sf[i]!=0) {
flag = false;
break;
}
}
printf("%s", flag? "Yes\n" : "No\n");
reverse(ds.begin(),ds.end());
printf("%s",ds.c_str());
return 0;
}

Code 02

#include <cstdio>
#include <string.h>
using namespace std;
int book[10];
int main() {
char num[22];
scanf("%s", num);
int flag = 0, len = strlen(num);
for(int i = len - 1; i >= 0; i--) {
int temp = num[i] - '0';
book[temp]++;
temp = temp * 2 + flag;
flag = 0;
if(temp >= 10) {
temp = temp - 10;
flag = 1;
}
num[i] = (temp + '0');
book[temp]--;
}
int flag1 = 0;
for(int i = 0; i < 10; i++) {
if(book[i] != 0)
flag1 = 1;
}
printf("%s", (flag == 1 || flag1 == 1) ? "No\n" : "Yes\n");
if(flag == 1) printf("1");
printf("%s", num);
return 0;
}

最新文章

  1. 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL
  2. css3之多列
  3. Port Hacking
  4. jquery 获得星期几-根据当前日期判断是星期几
  5. oracle 字符集导入、导出 、转换
  6. (转载)linux那点事儿(中)
  7. json小结和fastjson包的实际json操作
  8. Centos运行Mysql因为内存不足进程被杀
  9. leetcode — path-sum-ii
  10. 工具篇-Java中一些utils
  11. 钉钉调试应用Inspect不显示或显示空白的解决方法
  12. 翻译1-在SQL Server 2016中介绍微软R服务
  13. hashCode方法的作用?
  14. pycharm如何在debug的时候动态执行python语句
  15. 5.Mysql常用函数
  16. windows server2012部署Cognos问题小结
  17. Java多例模式
  18. CentOS下的一些基础问题解答
  19. MyBatis别名
  20. 4.4.3 Java中的指针:Unsafe类

热门文章

  1. 第二阶段scrum-8
  2. HDU 4819 二维线段树
  3. LeetCode#7 整数反转(数学)
  4. HDFS 中文件操作的错误集锦
  5. UVA - 1149 Bin Packing(装箱)(贪心)
  6. 201909-2 小明种苹果(续) Java
  7. BUUCTF-[HCTF 2018]WarmUp
  8. App开发(连接外部服务器)
  9. redis(三)----连接池配置
  10. Tensorflow学习教程------变量