PAT甲题题解-1023. Have Fun with Numbers (20)-大数加法
和1024一样都是大数据的题,因为位数最多要20位,long long最多19位
给一个num,求sum=num+num
问sum包含的数字,是否是num的一个排列,即数字都一样,只是顺序不同罢了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <string>
using namespace std;
const int maxn=; struct Bign{
int len,num[maxn]; // 这里num[0]是最低位,num[len-1]是最高位
Bign(){
memset(num,,sizeof(num));
len=;
}
Bign(int val){
*this=val;
}
Bign(const char*val){
*this=val;
}
//以int型赋值
Bign operator=(int val){
char s[maxn];
sprintf(s,"%d",val);
*this=s; //字符串赋值
return *this;
}
//以字符串赋值
Bign operator=(const char*val){
len=strlen(val);
for(int i=;i<len;i++){
num[i]=val[len--i]-'';
}
return *this;
}
//去掉前导0
void clean(){
while(len>&&!num[len-]){
len--;
}
}
//转化为字符串
string tostr(){
string res="";
clean();
for(int i=;i<len;i++){
res=(char)(num[i]+'')+res;
}
if(res=="")
return "";
return res;
}
//重载+
Bign operator+(const Bign& b)const{
Bign c;
c.len=;
for(int i=,g=;g||i<max(len,b.len);i++){
int x=g;
if(i<len)
x+=num[i];
if(i<b.len)
x+=b.num[i];
c.num[c.len++]=x%;
g=x/;
}
return c;
}
//取翻转后的数
Bign toReverse(){
clean();
Bign c;
c.len=len;
for(int i=;i<len;i++){
c.num[i]=num[len--i];
}
return c;
} };
int main()
{
char str[];
scanf("%s",str);
Bign num=str;
Bign sum=num+num;
int vis1[],vis2[];
memset(vis1,,sizeof(vis1));
memset(vis2,,sizeof(vis2));
for(int i=;i<num.len;i++){
vis1[num.num[i]]=;
}
for(int i=;i<sum.len;i++){
vis2[sum.num[i]]=;
}
bool flag=true;
for(int i=;i<;i++){
if(vis1[i]!=vis2[i]){
flag=false;
}
}
if(flag){
printf("Yes\n");
}
else{
printf("No\n");
}
string s=sum.tostr();
cout<<s<<endl;
return ;
}
PAT甲题题解-1023. Have Fun with Numbers (20)-大数加法的更多相关文章
- PAT甲题题解-1058. A+B in Hogwarts (20)-大水题
无语,这种水题还出,浪费时间,但又不得不A... #include <iostream> #include <cstdio> #include <algorithm> ...
- PAT甲题题解-1031. Hello World for U (20)-字符串处理,水
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- PAT甲题题解-1104. Sum of Number Segments (20)-(水题)
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642
PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...
- PAT甲题题解-1068. Find More Coins (30)-dp,01背包
一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...
- PAT甲题题解-1008. Elevator (20)-大么个大水题,这也太小瞧我们做题者的智商了
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <cstr ...
- PAT甲题题解-1010. Radix (25)-二分搜索
题意:给出n1和n2,以及其中一个数的进制,问另一个数是多少进制的情况下,才会是两个数相等.不存在的话,则输出Impossible 这题思路很简单,但是要考虑的比较多,在简单题里面算是比较好的. 有两 ...
- PAT甲题题解-1011. World Cup Betting (20)-误导人的水题。。。
题目不严谨啊啊啊啊式子算出来结果是37.975样例输出的是37.98我以为是四舍五入的啊啊啊,所以最后输出的是sum+0.005结果告诉我全部错误啊结果直接保留两位小数就可以了啊啊啊啊 水题也不要这么 ...
- PAT甲题题解-1012. The Best Rank (25)-排序水题
排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询 ...
随机推荐
- Azure IoT 预配置解决方案
Azure IoT 预配置解决方案 Sangyu Li © 2018 一.什么是Azure IoT 预配置解决方案? 如图,这就是Azure IoT Suite中 Provision solutio ...
- Windows Phone 8 获取与监听网络连接状态(转)
原文地址:http://www.cnblogs.com/sonic1abc/archive/2013/04/02/2995196.html 现在的只能手机对网络的依赖程度都很高,尤其是新闻.微博.音乐 ...
- October 31st, 2017 Week 44th Tuesday
No matter how hard the past is, you can always begin again. 不管过去有多么困难,你都可以重新开始. Honestly, I don't ag ...
- mac系统默认python3.6
1. 终端打开.bash_profile文件 终端输入:open ~/.bash_profile 2. 打开.bash_profile文件后在内容最后添加 alias python=" ...
- 团队作业——Beta冲刺4
团队作业--Beta冲刺 冲刺任务安排 杨光海天 今日任务:在同队成员帮助下,完成了浏览详情界面的跳转,以及图片的嵌入 明日任务:继续完成浏览详情界面 吴松青 今日任务:研究图片详情界面后端函数,遇到 ...
- C 语言之预处理 ---------文件包括
文件包括是C预处理程序的还有一个重要功能. 文件包括命令行的一般形式为: #include"文件名称" 在前面我们已多次用此命令包括过库函数的头文件. 比如: #include&q ...
- Spring framework3.2整合hibernate4.1报错:No Session found for current thread
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransact ...
- ICC Stage Flow
initial: reference library(mw), link library(db), create_mw_lib, read_verilog, link create_floorplan ...
- Linux SSH免密登录
SSH无密码登录要使用公钥与私钥.Linux可以用ssh-keygen生成公钥/私钥对,下面以Ubuntu为例说明配置过程. 有两个节点:node01(172.17.0.14)和node02(172. ...
- Android Edittext聚焦时输入法挡住了EditText输入框的两种解决方案
方案一.把整个布局文件用ScrollView套住.这样当你聚焦时虽然输入法也能够挡住一些输入框,但是你可以通过手动滑动看被挡住的内容. 方案二.在Activity中设置android:windowSo ...