PAT 甲级 1023 Have Fun with Numbers
https://pintia.cn/problem-sets/994805342720868352/problems/994805478658260992
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 file 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
题解: long long 会爆掉 所以字符串模拟加法
代码:
#include <cstdio>
#include <string>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; char s[2222], num[2222], sum[2222], c, summ[2222]; int main() {
scanf("%s", s);
int dot = 0;
bool flag = true;
int len = strlen(s);
int k = 0, ans = 0;
for(int i = len - 1; i >= 0; i--) {
sum[ans++] = '0' + (s[i] - '0' + s[i] - '0' + k) % 10;
k = (s[i] - '0' + s[i] - '0' + k) / 10;
}
if(k) {
sum[ans++] = '1';
flag = 0;
}
for(int i = 0; i <= ans / 2 - 1; i++)
swap(sum[i], sum[ans - i - 1]);
strcpy(summ, sum);
sort(s, s + len);
sort(summ, summ + ans);
if(strcmp(s, summ) != 0) flag = 0;
if(flag) printf("Yes\n");
else printf("No\n");
printf("%s\n", sum);
return 0;
}
最新文章
- MySql.Data.Entity 在EF中解析uint的枚举时有BUG
- 如何判断Javascript对象是否存在
- 在jQuery代码中,实现转跳
- Python基础之【第二篇】
- CPU工作状态的知识介绍
- Sqlserver中存储过程,触发器,自定义函数
- Eclipse for PHP Developers + xamp +xdebug
- 任意轴算法 ( Arbitrary Axis Algorithm )
- C#多态及接口
- H264-YUV通过RTP接收视频流ffmpeg解码SDL实时播放
- 题解-SDOI2015 约数个数和
- Js -----后台json数据,前端生成下载text文件
- JS_高程5.引用类型(3)Array类型-检测数组
- AltiumDesigner PCB中栅格与格点的切换
- leetcode — merge-k-sorted-lists
- wireshark显示过滤器的几种用法(转自他人博客)
- 学习笔记之MobaXterm
- js 验证input 输入框
- hdu 5248 贪心
- js 小数取整,js 小数向上取整,js小数向下取整