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

通过分析题目可知数的最大位为20位,就连最大的long long类型此时也会爆掉,因此要采用字符串来处理。
上代码:
#include<stdio.h>
#include<string.h> int main(void){
char num1[21],num2[21];
scanf("%s",num1);
char doublenum[21];
char tmp;
strcpy(num2,num1);
int len=strlen(num2);
//每位数字乘2后存储在数组num2中
int carrybit=0;//储存每位数运算后的进位

char midch;

    //字符串处理的关键
for(int i=len-1;i>=1;i--){
      //根据人工列式计算的步骤给出算法
midch=num2[i];
num2[i]=((num2[i]-'0')*2+carrybit)%10+'0'; //先乘2再加上次运算产生的进位
carrybit=((midch-'0')*2+carrybit)/10; //计算这一次的进位,注意不能再用num2[i],因为num2[i]已经改变 错误:carrybit=((num2[i]-'0')*2+carrybit)/10
  } 
  num2[0]=(num2[0]-'0')*2+carrybit+'0'; //最高位不用再进位,若最高位大于9,下面会给出处理

if(num2[0]>'9'){//如果乘2后的数和原来的数位数不匹配,直接判定No
printf("No\n");
printf("%d%d",(num2[0]-'0')/10,(num2[0]-'0')%10);
printf("%s",&num2[1]);
}else{
strcpy(doublenum,num2);

    //冒泡排序num1 num2
for(int i=0;i<len-1;i++){
for(int j=0;j<len-i-1;j++)
if(num2[j]>num2[j+1]){
tmp=num2[j];
num2[j]=num2[j+1];
num2[j+1]=tmp;
}
} for(int i=0;i<len-1;i++){
for(int j=0;j<len-i-1;j++)
if(num1[j]>num1[j+1]){
tmp=num1[j];
num1[j]=num1[j+1];
num1[j+1]=tmp;
}
}

if(strcmp(num1,num2)==0){
printf("Yes\n");
printf("%s",doublenum);
}else{
printf("No\n");
printf("%s",doublenum);
}
} return 0;
}

题目来源:

https://pintia.cn/problem-sets/17/problems/263

最新文章

  1. 使用 nvm 管理不同版本的 node 与 npm
  2. 在Ubuntu全局安装express报错:Error: EACCES, mkdir &#39;/usr/lib/node_modules/express&#39;的解决办法
  3. android network develop(2)----network status check
  4. 解决vs2013使用Git推送到远程仓库报错的问题
  5. 【BZOJ】1097: [POI2007]旅游景点atr(spfa+状压dp)
  6. Oracle数据库中的Function调用参数问题
  7. tabbar - 取消系统渲染
  8. 利用Keil u4调试,精确实现软件延时
  9. oschina服务器软件
  10. Android技术路线图
  11. Swift - 纯代码实现页面segue跳转,以及参数传递
  12. shell群发邮件脚本
  13. [编织消息框架][netty源码分析]13 ByteBuf 实现类CompositeByteBuf职责与实现
  14. java 打印近似圆
  15. [LeetCode] Rectangle Overlap 矩形重叠
  16. 写你的shell,其实很简单[架构篇]
  17. Java若不为空则取其值的lambda表达式
  18. Redis——基础数据结构
  19. PyQT5 No module named ‘PyQt5.QtWebEngineWidgets’
  20. C#.NET常见问题(FAQ)-如何让控件或者窗体本身全屏

热门文章

  1. 初窥 Python 的 import 机制
  2. 基于Qt的tcp客户端和服务器实现摄像头帧数据处理(客户端部分)
  3. 嵌入式设备上卷积神经网络推理时memory的优化
  4. hdu3341Lost's revenge (AC自动机+变进制dp)
  5. UVA442 矩阵链乘 Matrix Chain Multiplication
  6. python 实现AES加密和解密
  7. ASP.NET 部署IIS后如何访问共享目录文件
  8. VMware ESXi 开启嵌套虚拟化
  9. Leetcode(878)-第 N 个神奇数字
  10. codeforces 911D