1024. Palindromic Number (25) @ PAT (Advanced Level) Practise
来源:互联网 发布:阿里云代理商查询 编辑:程序博客网 时间:2024/06/05 19:57
Version 2: 模拟加法(http://blog.csdn.net/yhhhyfyy/article/details/8594868)
#include<iostream>#include<string>#include<algorithm> //reverse函数用到using namespace std; string add(strings1,string s2){stringresult=""; int carry=0;intlen=s1.length();intsum;while(len--){sum=s1[len]-'0'+ s2[len]-'0'+carry;result.insert(result.begin(),sum%10+'0');carry=sum/10;}if(carry) //最终和有进位{ result.insert(result.begin(),carry+'0');}returnresult;} int main(){ string s1,s2,result; //s1作为输入,s2是翻转后的字符串 int k,ans=0; //ans保存翻转求和次数 cin>>s1>>k; while (ans!=k) { s2=s1; reverse(s2.begin(),s2.end()); if (s1==s2) //若回文 { break; } else //否则继续翻转求和 { s1=add(s1,s2); } ans++; } cout<<s1<<endl<<ans<<endl; return 0;}
Version 1: case 6 & case 8 内存超限
#include<iostream>#include<sstream>#include<string>#include<algorithm>using namespace std;stringstream ss;int icount=0;boolisPalindromic(string & s){inti=0;intj=s.length()-1;for(;i<j;i++,j--){if(s[i]!=s[j])returnfalse;}returntrue;}voidtransform(string & s){longtmp1,tmp2;ss.clear();ss<<s;ss>>tmp1;ss.clear();reverse(s.begin(),s.end());ss<<s;ss>>tmp2;ss.clear();ss<<tmp1+tmp2;ss>>s;icount++;}int main(){stringnum;intk;cin>>num>>k;while(!isPalindromic(num)&& icount<k){transform(num);}cout<<num<<endl;cout<<icount;return0;}
- 1024. Palindromic Number (25) @ PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1024 Palindromic Number (25)
- PAT (Advanced Level) Practise 1024 Palindromic Number (25)
- 【PAT Advanced Level】1024. Palindromic Number (25)
- 【PAT】【Advanced Level】1024. Palindromic Number (25)
- 1024. Palindromic Number (25)【简易大数加法+回文】——PAT (Advanced Level) Practise
- 1019. General Palindromic Number (20) @ PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1019. General Palindromic Number (20)
- PAT (Advanced Level) Practise 1019 General Palindromic Number (20)
- PAT (Advanced Level) Practise 1019 General Palindromic Number (20)
- PAT (Advanced Level) 1024. Palindromic Number (25) 字符串反转相加
- 浙大 PAT Advanced level 1024. Palindromic Number (25)
- PAT (Advanced Level) Practise 1082 Read Number in Chinese (25)
- PAT (Advanced Level) Practise 1117Eddington Number(25)
- PAT (Advanced Level) Practise 1117 Eddington Number(25)
- PAT (Advanced Level) Practise 1082 Read Number in Chinese (25)
- 1019. General Palindromic Number (20)——PAT (Advanced Level) Practise
- Pat(Advanced Level)Practice--1024(Palindromic Number)
- 黑马程序员_封装、继承、多态
- ASP.NET导出Excel,打开Excel文件时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致”
- Android 亮度调节
- NGUI系列教程五(角色信息跟随)
- LINQ to SQL语句(1)之Where
- 1024. Palindromic Number (25) @ PAT (Advanced Level) Practise
- SAP 免费公开课即将开始 为期六周的企业移动应用开发
- 在 precise 发行版中 i386 硬件架构下的 bluez 软件包文件清单
- NGUI系列教程六(序列帧动画)
- Nagle 算法
- 杭电 acm 1075 What Are You Talking About
- TOMCAT 不用项目名/直接访问域名/80端口
- CentOs linux 修改默认使用的JDK版本
- 学习boost program_options(2--简单示例)