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. HDU-Digital Roots(思维+大数字符串模拟)

    The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...

  2. 剑指offer编程题Java实现——面试题12相关题大数的加法、减法、乘法问题的实现

    用字符串或者数组表示大数是一种很简单有效的表示方式.在打印1到最大的n为数的问题上采用的是使用数组表示大数的方式.在相关题实现任意两个整数的加法.减法.乘法的实现中,采用字符串对大数进行表示,不过在具 ...

  3. 代码题(59)— 字符串相加、字符串相乘、打印最大n位数

    1.415. 字符串相加 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 思路:和链表相加类似,求进位. class Solution { public: string addS ...

  4. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

  5. hdu1316(大数的斐波那契数)

    题目信息:求两个大数之间的斐波那契数的个数(C++/JAVA) pid=1316">http://acm.hdu.edu.cn/showproblem.php? pid=1316 这里 ...

  6. 1002 A + B Problem II [ACM刷题]

    这一段时间一直都在刷OJ,这里建一个博客合集,用以记录和分享算法学习的进程. github传送门:https://github.com/haoyuanliu/Online_Judge/tree/mas ...

  7. go函数练习

    1.编写程序,在终端输出九九乘法表. package main import ( "fmt" ) func main() { for i := 1; i <= 9; i++ ...

  8. 大整数相加 a+b 的c语言实现

    终于来到我所期盼的高精度整数相加的题目了.这个题很经典,也算是一个很好的算法入门题吧. 如果是java的话,系统类库已经内置了BigInteger类,直接调用就可以很轻易地解决了.但是学习c的编写也是 ...

  9. PAT 1074 宇宙无敌加法器(20)(代码+思路+测试点分析)

    1074 宇宙无敌加法器(20 分)提问 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为"P ...

随机推荐

  1. 昨晚12点,女朋友突然问我:你会RabbitMQ吗?我竟然愣住了。

    01为什么要用消息队列? 1.1 同步调用和异步调用 在说起消息队列之前,必须要先说一下同步调用和异步调用. 同步调用:A服务去调用B服务,需要一直等着B服务,直到B服务执行完毕并把执行结果返回给A之 ...

  2. promise封装微信小程序的request

    1.在utils下创建一个 request.js文件,然后将方法导出 const app = getApp(); //使用promise封装request请求 const POST = (url, p ...

  3. AtCoder Beginner Contest 169

    比赛链接:https://atcoder.jp/contests/abc169/tasks A - Multiplication 1 #include <bits/stdc++.h> us ...

  4. Windows系统自带的DOS窗口

    写在前面: 整理自网络 记录学习过程,方便复习 说明 DOS全称为Disk Operating System,意思是"磁盘操作系统" DOS是个人计算机上的一类操作系统,windo ...

  5. Educational Codeforces Round 95 (Rated for Div. 2) C. Mortal Kombat Tower (DP)

    题意:你和基友两人从左往右轮流打怪兽,强怪用\(1\)表示,垃圾用\(0\)表示,但基友比较弱,打不过强怪,碰到强怪需要用一次魔法,而你很强,无论什么怪都能乱杀,基友先打,每人每次至少杀一个怪兽,最多 ...

  6. git忽略规则以及.gitignore文件不生效解决办法

    正文 Git忽略规则: #此为注释 – 内容被 Git 忽略 .sample # 忽略所有 .sample 结尾的文件 !lib.sample # 但 lib.sample 除外 /TODO # 仅仅 ...

  7. 【非原创】codeforces 1070C Cloud Computing 【线段树&树状数组】

    题目:戳这里 学习博客:戳这里 题意:有很多个活动,每个活动有持续天数,每个活动会在每天提供C个CPU每个CPU价格为P,问需要工作N天,每天需要K个CPU的最少花费. 解题思路:遍历每一天,维护当前 ...

  8. 51nod 1384 可重集的全排列

    对于1231,121,111等有重复的数据,我们怎么做到生成全排列呢 实际上,对于打标记再释放标记的这种方法,如果一开始第一层递归访问过1那么你再访问 就会完全重复上一次1开头的情况,那么递归地考虑这 ...

  9. Hacker101 CTF-Micro-CMS v2

    一.打开网站是这个样子 找到一个登录框,存在注入漏洞 3.我们可以这样更改用户名中的输入: admin' or 1=1 -- 4.错误消息显示Invalid Password,因此我们也应该尝试构造一 ...

  10. μC/OS-III---I笔记11---就绪任务列表管理

    就绪优先级为映像响表 在UCOSIII内,任务调度是要先找到优先级最高的任务,然后执行.理论上对于UCOSIII可以有无数个优先级,每个优先级又可以有无数个任务但是对于这么多的任务如何快速查到到当先就 ...