首页 > 其他分享 >1136 A Delayed Palindrome

1136 A Delayed Palindrome

时间:2023-09-12 16:47:45浏览次数:47  
标签:10 Palindrome string palindromic s1 number 1136 Delayed carry

题目:

Consider a positive integer N written in standard notation with k+1 digits ai​ as ak​⋯a1​a0​ with 0≤ai​<10 for 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.

 

注意点:

1、string字符串反转

使用 reverse(s.begin(), s.end()); 

头文件为 #include<algorithm>

2、大整数运算时,要重新开一个数组来存储运算结果,否则在例如 A + reverse(A)的情况下会出错

 

 

 

 

代码:

#include <iostream>
#include <algorithm>
using namespace std;
string rev(string s) {
    reverse(s.begin(), s.end());
    return s;
}
string add(string s1, string s2) {
    string s = s1;
    int carry = 0;
    for (int i = s1.size() - 1; i >= 0; i--) {
        s[i] = (s1[i] - '0' + s2[i] - '0' + carry) % 10 + '0';
        carry = (s1[i] - '0' + s2[i] - '0' + carry) / 10;
    }
    if (carry > 0) s = "1" + s;
    return s;
}
int main() {
    string s, sum;
    int n = 10;
    cin >> s;
    if (s == rev(s)) {
        cout << s << " is a palindromic number.\n";
        return 0;
    }
    while (n--) {
        sum = add(s, rev(s));
        cout << s << " + " << rev(s) << " = " << sum << endl;
        if (sum == rev(sum)) {
            cout << sum << " is a palindromic number.\n";
            return 0;
        }
        s = sum;
    }
    cout << "Not found in 10 iterations.\n";
    return 0;
}

 

标签:10,Palindrome,string,palindromic,s1,number,1136,Delayed,carry
From: https://www.cnblogs.com/yccy/p/17697041.html

相关文章

  • CF1335E1 Three Blocks Palindrome (easy version)
    思路发现一个进阶回文序列仅包含三个部分:\(x\)个连续的\(a\),\(y\)个连续的\(b\),\(x\)个连续的\(a\)。对于一个\(a\),我们一定会取最外面的两个\(a\),如果不取,则答案一定不小或不变,所以我们枚举到\(a\)的时候,一定是确定了最外围的两个\(a\)的位置。接下来再枚举\(x\)......
  • P1217 [USACO1.5] 回文质数 Prime Palindromes
      打表先把一到一亿的质数兼回文数打出来。(用文件输入输出会方便复制一些)最后效果如下:太长故折叠 0,2,3,5,7,11,101,131,151,181,191,313,353,373,383,727,757,787,797,919,929,10301,10501,10601,11311,11411,12421,12721,12821,13331,13831,13931,14341,14741,15451,1......
  • 【刷题笔记】9. Palindrome Number
    题目Determinewhetheranintegerisapalindrome.Aninteger is a palindromewhenit readsthesamebackwardasforward.Example1:Input:121Output:trueExample2:Input:-121Output:falseExplanation:Fromlefttoright,itreads-121.Fromrightto......
  • ScheduledThreadPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(bo
    MethodSummary voidexecute(Runnable          Executecommandwithzerorequireddelay. booleangetContinueExistingPeriodicTasksAfterShutdownPolicy()          Getthepolicyonwhethertocontinueexecutingexistingperiodictaskseven......
  • 「解题报告」CF1738H Palindrome Addicts
    神秘回文串题。首先容易发现要求的是区间本质不同回文串个数,所以直接上论文做法即可。容易想到增量构建回文自动机,假如现在建出了\([1,r]\)的PAM,考虑有多少回文串出现在了\([l,r]\)内。考虑记录每个回文串的最后一次出现位置\(last_p\),那么这个串的左端点就是\(last_p......
  • Palindrome Number
    Givenanintegerx,returntrueifxisapalindrome,andfalseotherwise.Example1:Input:x=121Output:trueExplanation:121readsas121fromlefttorightandfromrighttoleft.Example2:Input:x=-121Output:falseExplanation:Fromleftto......
  • CF 570E - Pig and Palindromes
    https://codeforces.com/problemset/problem/570/E双向DP,类似于摘樱桃:https://leetcode.cn/problems/cherry-pickup/记忆化搜索,超内存#include<vector>#include<string>#include<functional>#include<iostream>usingnamespacestd;intmain(){ int......
  • leetcode 409. Longest Palindrome
    Givenastringwhichconsistsoflowercaseoruppercaseletters,findthelengthofthelongestpalindromesthatcanbebuiltwiththoseletters.Thisiscasesensitive,forexample"Aa"isnotconsideredapalindromehere.Note:Assumethelength......
  • CF1512C A-B Palindrome 题解
    CF1512CA-BPalindrome题解Link洛谷CodeforcesDescription给出\(T\)个只由0、1和?组成的字符串\(s\),将字符串中的?替换成0或1之后形成一个回文串并且恰好有\(a\)个0和\(b\)个1,无解输出-1。Solution首先,若不考虑?原串不为回文串一定无解,输出-1即......
  • Partitioning by Palindromes - UVa 11584
    例题9-7划分成回文串(PartitioningbyPalindromes,UVa11584)#dp#线性dp#字符串回文#T3输入一个由小写字母组成的字符串,要求把它划分成尽量少的回文串。输出最少的个数。如aaadbccb最少可以划分为3个:aaa,d,bccb输入:第一行输入一个n表示数据组数接下来n行每行输入一个......