如果不是回文则进行逆序相加操作,打印出最后回文和操作次数
题目中说数字可达到10的10次方 迭代可100次
则每次相加次数可能因进位多一位,longlong型只能19位可能不够
实现重点,字符串相加
注意求进位时也要带上carry
以下代码数值型会溢出
#include<iostream>
using namespace std;
long long reverse(long long in)
{
long long tmp = in;
long long re = 0;
while(tmp)
{
re *= 10;
re += tmp % 10;
tmp /= 10;
}
return re;
}
int main()
{
long long N;
int K;
int k = 0;
long long rN;
cin>>N;
cin>>K;
while(k < K)
{
rN = reverse(N);
if(rN == N)
break;
else
N += rN;
k++;
}
cout<<N<<endl;
cout<<k<<endl;
}
正确代码
#include<iostream>
using namespace std;
#include<string>
#include<algorithm>
string add(string s1,string s2)
{
int carry = 0;
string result = "";
int len1 = s1.size();
int len2 = s2.size();
int sum;
int i,j;
for(i = len1-1,j = len2-1;i>=0 || j>=0;i--,j--)
{
if(i<0)
sum = s2[j]-'0';
else if(j<0)
sum = s1[i]-'0';
else
sum = s1[i]-'0'+s2[j]-'0';
sum += carry;
result.insert(result.begin(),sum%10+'0');
carry = sum/10;
}
if(carry)
result.insert(result.begin(),carry+'0');
return result;
}
int main()
{
string str;
int K;
int k = 0;
string rstr;
cin>>str;
cin>>K;
while(k<K)
{
rstr = str;
reverse(str.begin(),str.end());
if(rstr == str)
break;
else
str = add(str,rstr);
k++;
}
cout<<str<<endl;
cout<<k<<endl;
}
分享到:
相关推荐
PAT甲级 1024 Palindromic Number A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit...
原创:PAT 1019 General Palindromic Number (20 分)A number that will be the same when
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. ...
Longest-Palindromic-Substring(最长回文子串) 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 Sample 1 输入: "babad" 输出: "bab" 注意: "aba" 也是一个有效答案。 Sample 2 输入...
最大公共字符串leetcode 最长回文子串 给定一个字符串 s,找出 s 中最长的回文子串。 您可以假设 s 的最大长度为 1000。 Example 1: Input: "babad" Output: "bab" Note: "aba" is also ...substri
眼镜创建一个名为palindromeNumberGenerator的函数,该函数采用一个Number值。 检查数字是否为回文数,如果不是,则将值和取反的值相加,并检查总和是否为回文数,重复直到达到回文数值。 每次对这些值求和以获取新...
最大公共字符串leetcode 最长回文子串 给定一个字符串 s,找出 s 中最长的回文子串。 您可以假设 s 的最大长度为 1000。 示例 1: Input: "babad" Output: "bab" Note: "aba" is also a valid ...re
最长回文子序列 动态规划 | 第 12 组(最长回文子序列)( )
./0005-longest-palindromic-substring.cpp ./0006-zigzag-conversion.cpp ./0007-reverse-integer.cpp ./0008-string-to-integer-atoi.cpp ./0009-palindrome-number.cpp ./0010-regular-expression-matching.cpp ....
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Java AC 版本
最长回文子串最长回文子串 | 设置 1 ( )
5-longest-palindromic-substring.c。 (完毕) 使用优化算法更新 214-shortest-palindrome.c。 使用二分搜索更新 287-find-the-duplicate-number.c 和弗洛伊德的龟兔赛跑(循环检测)算法。 (完毕) 使用哈希表...
USACO题目Palindromic Squares(回文平方数)及代码解析
Palindromic Substring.cpp │ └── python │ ├── 001 - Two Sum.py │ ├── 002 - Add Two Numbers.py │ ├── 003 - Longest Substring Without Repeating Characters.py │ ├── 004 - ...
zoj 3661 Palindromic Substring.md
The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...
回文数要确定是否需要取消费用,必须先确定是否需要取消费用。 Elnúmerodebe ser市长de 2dígitos。 Desarrollar unaaplicaciónque ingrese dosnúmerosy se cambien las posiciones pares del底漆númerocon el ...
Longest Palindromic Substring Regular Expression Matching Wildcard Matching Longest Common Prefix Valid Number Integer to Roman Roman to Integer Count and Say Anagrams Valid Anagram Simplify Path ...
https://leetcode.com/problems/longest-palindromic-substring/solution/) 解决方案 日期 最长公共子串 6/24 蛮力 6/24 动态规划 8/14 围绕中心展开 8/12 92.反向链表II 解决方案 日期 Head->Rev->End 7/5 46.排列 ...
Palindromic Substring 8 String to Integer 11 Container with Most Water 14 Longest Common Prefix 15 Three Sum 16 Three Sum Closest 20 Valid Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate ...