进位&&大数字符串处理
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
进位&&大数字符串处理的更多相关文章
- HDU-Digital Roots(思维+大数字符串模拟)
The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...
- 剑指offer编程题Java实现——面试题12相关题大数的加法、减法、乘法问题的实现
用字符串或者数组表示大数是一种很简单有效的表示方式.在打印1到最大的n为数的问题上采用的是使用数组表示大数的方式.在相关题实现任意两个整数的加法.减法.乘法的实现中,采用字符串对大数进行表示,不过在具 ...
- 代码题(59)— 字符串相加、字符串相乘、打印最大n位数
1.415. 字符串相加 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 思路:和链表相加类似,求进位. class Solution { public: string addS ...
- 杭电acm 1002 大数模板(一)
从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...
- hdu1316(大数的斐波那契数)
题目信息:求两个大数之间的斐波那契数的个数(C++/JAVA) pid=1316">http://acm.hdu.edu.cn/showproblem.php? pid=1316 这里 ...
- 1002 A + B Problem II [ACM刷题]
这一段时间一直都在刷OJ,这里建一个博客合集,用以记录和分享算法学习的进程. github传送门:https://github.com/haoyuanliu/Online_Judge/tree/mas ...
- go函数练习
1.编写程序,在终端输出九九乘法表. package main import ( "fmt" ) func main() { for i := 1; i <= 9; i++ ...
- 大整数相加 a+b 的c语言实现
终于来到我所期盼的高精度整数相加的题目了.这个题很经典,也算是一个很好的算法入门题吧. 如果是java的话,系统类库已经内置了BigInteger类,直接调用就可以很轻易地解决了.但是学习c的编写也是 ...
- PAT 1074 宇宙无敌加法器(20)(代码+思路+测试点分析)
1074 宇宙无敌加法器(20 分)提问 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为"P ...
随机推荐
- NodeRED - 全局变量的使用笔记
NodeRED - 全局变量的使用笔记 global global.get(..) :获取全局范围的上下文属性 global.set(..) :设置全局范围的上下文属性 global.keys(..) ...
- CentOS 7 部署redis
1.下载redis: 地址:http://download.redis.io/releases/: 选择需要下载的版本,然后通过ssh工具导入到centos中,这里放到了/usr/local; 解压文 ...
- 【C#】对两张图片进行矩阵运算会怎么样?
对两张图片进行矩阵运算会怎么样? 在学习<线性代数>的矩阵运算时,突然想到图片也可以算是一种矩阵,那么对图片进行矩阵的运算会出现什么样的效果呢?为了满足好奇,便用C#写了个对图片进行矩阵运 ...
- HDU-4773 Problem of Apollonius (圆的反演)
参考: https://oi-wiki.org/geometry/inverse/ https://blog.csdn.net/acdreamers/article/details/16966369 ...
- C# 异常重试策略
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- .NET Core项目自动化测试和代码覆盖率审查
这篇文章给大家分享一下,如何配置.NET Core项目自动化测试和代码覆盖率审查. 基本知识,请参考这里: https://docs.microsoft.com/en-us/dotnet/core/t ...
- 爬虫入门四 re
title: 爬虫入门四 re date: 2020-03-14 16:49:00 categories: python tags: crawler 正则表达式与re库 1 正则表达式简介 编译原理学 ...
- ES2015 (ES6) 新特性: 20 个
ES2015 (ES6) 新特性 http://babeljs.io/docs/learn-es2015/ Learn ES2015 A detailed overview of ECMAScript ...
- Chrome 黑科技
Chrome 黑科技 秒变记事本 data:text/html, <htmlcontenteditable> OK <a href="data:text/html, &qu ...
- WebVR & CSS 3D & WebGL
WebVR & CSS 3D & WebGL VR https://developer.mozilla.org/en-US/docs/Web/API/WebVR_API https:/ ...