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. 2019牛客暑期多校训练营(第十场)F.Popping Balloons(线段树)

    题意:现在给你n个点 现在让你横着划三条线间距为r 然后竖着划三条线间距同样为r 现在让你求经过最多的点数 思路:我们首先建一棵关于y区间的线段树 然后枚举x轴 每次更新重叠的点 然后再更新回去 找一 ...

  2. Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)【ABCD】

    比赛链接:https://codeforces.com/contest/1445 A. Array Rearrangment 题意 给定两个大小均为 \(n\) 的升序数组 \(a\) 和 \(b\) ...

  3. P4587 [FJOI2016]神秘数(主席树)

    题意:给出1e5个数 查询l,r区间内第一个不能被表示的数 比如1,2,4可以用子集的和表示出[1,7] 所以第一个不能被表示的是8 题解:先考虑暴力的做法 把这个区间内的数字按从小到大排序后 从前往 ...

  4. ACM-ICPC 2018 徐州赛区网络预赛(8/11)

    ACM-ICPC 2018 徐州赛区网络预赛 A.Hard to prepare 枚举第一个选的,接下来的那个不能取前一个的取反 \(DP[i][0]\)表示选和第一个相同的 \(DP[i][1]\) ...

  5. AC自动机——看似KMP在跑,其实fail在跳

    先存代码 AC自动机(简单版) #include<bits/stdc++.h> #define maxn 1000007 using namespace std; int n,ans; i ...

  6. Codeforces Round #652 (Div. 2) C. RationalLee 贪心

    题意: t组输入,你有n个数,还有k个朋友,每一个朋友需要wi个数.意思就是你要给第i个朋友分配wi个数,输入保证w1+w2+...+wk=n 一个朋友的兴奋值是你分配给他的数中最大值加上最小值的和( ...

  7. Atcoder Beginner Contest 168 D - .. (Double Dots) (BFS)

    题意:有\(n\)个房间,在这些房间中两两连\(m\)次条边,问除了第一个房间,其他房间走到第一个房间的最短路径,输出这个房间所连的上一个房间,如果走不到,输出\(no\). 题解:刚开始我写了一个d ...

  8. linux repo init 遇到的问题

    问题描述: 利用repo从远程服务器上取代码时候,出现错误  fatal: cannot make .repo directory:Permission denied, 加了sudo 之后,还是不行, ...

  9. 容器之List接口下各实现类(Vector,ArrayList 和LinkedList)的线程安全问题

    Vector .ArrayList 和LinkedList都是List接口下的实现类,但是他们之间的区别和联系是什么呢? 首先: 然后: 如果您仅仅想知道结论,那么可以关闭了. 下面我讨论讨论为什么. ...

  10. DOM型XSS

    打开漏洞页面,随便输入点东西,发现没有啥东西. 但是我们发现我们输入的11,在面的herf 中 看到这儿就很简单了,我们只需要闭合一下,就可以构造出我们的payload了. '><img ...