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. 深入了解JavaScript中基于原型(prototype)的继承机制

    原型 前言 继承是面向对象编程中相当重要的一个概念,它对帮助代码复用起到了很大的作用. 正文 Brendan Eich在创建JavaScript时,没有选择当时最流行的类继承机制,而是借鉴Self,用 ...

  2. Java中详述线程间协作

    线程协作 首先引入一段代码: package 线程间数据共享; import java.util.Date; public class Watch { private static String ti ...

  3. C++typename的由来和用法

  4. c++ stl nth_element 原理解析

    nth_element是stl中的一个库函数,它会使迭代器nth所指的元素与整个[first,last)完整排序后.同一个位置的元素同值.即找到完整排序时第n位的正确值. 但这个函数与完整排序的区别在 ...

  5. 使用scrapy爬取jian shu文章

    settings.py中一些东西的含义可以看一下这里 python的scrapy框架的使用 和xpath的使用 && scrapy中request和response的函数参数 & ...

  6. Go - 实现项目内链路追踪(二)

    上篇文章 Go - 实现项目内链路追踪 分享了,通过 链路 ID 可以将 请求信息.响应信息.调用第三方接口的信息.调试信息.执行的 SQL 信息.执行的 Redis 信息 串起来,记录的具体参数在文 ...

  7. leetcode5 最长回文字符串 动态规划 Manacher法

    dp 注意没有声明S不空,处理一下 o(n^2) class Solution { public: string longestPalindrome(string s) { if (s.empty() ...

  8. codeforces 1042C Array Product【构造】

    题目:戳这里 题意:n个数,两种操作,第一种是a[i]*a[j],删掉a[i],第一种是直接删除a[i](只能用一次)剩下的数序列号不变.操作n-1次,使最后剩下的那个数最大化. 解题思路: 正数之间 ...

  9. 网络流学习-Ford-Fulkerson

    首先我们先解决最大流问题 什么是最大流问题呢 根据我的理解,有一个源点s,汇点t,s可以通过一个网络(雾)流向汇点t 但是每一条边都有他的最大传输容量限制,那么我们的任务是,如何分配流量使得..从s流 ...

  10. Principle for iOS App Animation Design

    Principle for iOS App Animation Design Animate Your Ideas, Design Better Apps https://principleforma ...