PAT 1136 A Delayed Palindrome[简单]
1136 A Delayed Palindrome (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 with 0≤ai<10for all i and ak>0. Then N is palindromic if and only if ai=ak−i for all i. Zero is written 0 and is also palindromic by definition.
Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )
Given any positive integer, you are supposed to find its paired palindromic number.
Input Specification:
Each input file contains one test case which gives a positive integer no more than 1000 digits.
Output Specification:
For each test case, print line by line the process of finding the palindromic number. The format of each line is the following:
A + B = C
where A is the original number, B is the reversed A, and C is their sum. A starts being the input number, and this process ends until C becomes a palindromic number -- in this case we print in the last line C is a palindromic number.; or if a palindromic number cannot be found in 10 iterations, print Not found in 10 iterations. instead.
Sample Input 1:
97152
Sample Output 1:
97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.
Sample Input 2:
196
Sample Output 2:
196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.
题目大意:给出一个数,是否回文?否则将其反转然后和原数相加,直到加到第10次未出现或者出现。
#include <iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std; //但是属于哪一条地铁,怎么去表示呢?
bool pd(string s){
int sz=s.size();
for(int i=;i<sz/;i++){
if(s[i]!=s[sz-i-])
return false;
}
return true;
}
int toNum(string s){
int a=;
for(int i=;i<s.size();i++){
a=a*+(s[i]-'');
}
return a;
}
string toStr(int a){
string s;
while(a!=){
s+=(a%+'');
a/=;
}
reverse(s.begin(),s.end());
return s;
}
int main(){
string s;
cin>>s;
string t=s;
//reverse(s[0],s[0]+s.size());//那个反转函数是啥来着???
//s.reverse();//这个调用不正确
reverse(s.begin(),s.end());//t是第一个数,s是第二个数.
bool flag=false;
int a,b,c;
for(int i=;i<;i++){
if(pd(s)){//如果这个数本来就是答案。
flag=true;break;
}
a=toNum(t);
b=toNum(s);
c=a+b;
cout<<t<<" + "<<s<<" = "<<c<<'\n';
s=toStr(c);
t=s;
reverse(s.begin(),s.end());
}
if(flag){
cout<<t<<" is a palindromic number.";
}else{
cout<<"Not found in 10 iterations.";
}
return ;
}
//本来是这么写的,但是提交只有18分,最后一个测试点过不去:答案错误。
//我明白了,这个的考点是大数相加(1K位),而我却将其转换成int,实在是太不好了。
#include <iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std; string add(string a){
string b=a;
reverse(b.begin(),b.end());
int jin=,s;
for(int i=a.size()-;i>=;i--){
s=(a[i]-'')+(b[i]-'')+jin;
if(s>=){
s%=;
jin=;
}else jin=;
a[i]=s+'';
}
if(jin==){
a=""+a;
}
return a;
} int main(){
string s;
cin>>s;
string t=s;
//reverse(s[0],s[0]+s.size());//那个反转函数是啥来着???
//s.reverse();//这个调用不正确
reverse(s.begin(),s.end());//t是第一个数,s是第二个数.
bool flag=false;
for(int i=;i<;i++){
if(t==s){//如果这个数本来就是答案。
flag=true;break;
}
cout<<t<<" + "<<s<<" = "<<add(t)<<'\n';
t=add(t);
s=t;
reverse(s.begin(),s.end());
}
if(flag){
cout<<t<<" is a palindromic number.";
}else{
cout<<"Not found in 10 iterations.";
}
return ;
}
//终于正确了,这水题花了我好长时间,哭唧唧!你看清题好吗?1000位的数字就是模拟大数相加啊!!!
PAT 1136 A Delayed Palindrome[简单]的更多相关文章
- PAT 1136 A Delayed Palindrome
1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k ...
- pat 1136 A Delayed Palindrome(20 分)
1136 A Delayed Palindrome(20 分) Consider a positive integer N written in standard notation with k+1 ...
- PAT甲级:1136 A Delayed Palindrome (20分)
PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...
- PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- 1136 A Delayed Palindrome (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- 1136 A Delayed Palindrome (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- 1136 A Delayed Palindrome
题意:略. 思路:大整数相加,回文数判断.对首次输入的数也要判断其是否是回文数,故这里用do...while,而不用while. 代码: #include <iostream> #incl ...
- PAT1136:A Delayed Palindrome
1136. A Delayed Palindrome (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- A1136. Delayed Palindrome
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
随机推荐
- COUNT() 函数返回匹配指定条件的行数。
SQL COUNT() 语法 SQL COUNT(column_name) 语法 COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入): SELECT COUNT(col ...
- 【BZOJ】1029: [JSOI2007]建筑抢修(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1029 按右端点排序后依次加入,并且每一次看是否能被修筑,如果能就修:否则查找原来修过的,如果原来修过 ...
- hdu 1233 还是畅通project
本题链接:点击打开链接 本题大意: 有n个村庄,n*(n-1)/2条路,输入每条路所连接是哪两个村庄及长度,求使全部村庄均连通(并不是都两两连通) 所铺公路的最短路程. 解题思路: 使用并查集基础及K ...
- bootstrap基础学习八篇
bootstrap辅助类 a.对于文本颜色 以下不同的类展示了不同的文本颜色.如果文本是个链接鼠标移动到文本上会变暗: 类 描述 .text-muted "text-muted" ...
- jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法
本文实例讲述了jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法.分享给大家供大家参考.具体实现方法如下: 这里主要通过val方法设置按钮的文字,并用attr方法修改disabled属性实 ...
- php 将一个字符串分割为组成它的字符
问: php里如何将一个字符串分割为组成它的字符? 比如hello -> [h, e, l, l, o] 以下有三种方法: 这是需要被分割的字符串: $str = 'Hello小样'; ...
- Java Print 打印
Java 原生的API中有Print,使用Print可以操作打印机进行打印操作,获取打印机属性,下面是代码 打印程序(静默打印) package com.boci.PrintPDF; import j ...
- ArcGIS Android SDK 中文标注乱码
Android使用如下代码添加标注: TextSymbol ts = new TextSymbol(12, "名称", Color.RED);Graphic gp = new Gr ...
- js解析json字符
这是真真遇到的问题,后台给我返回的是json的字符串: {"status":410,"data":"","message" ...
- python里的生成器
author:headsen chen date:2018-03-22 10:59:46 notice:This article created by headsen chen himself and ...