1136. A Delayed Palindrome (20)

简介: #include #include #include using namespace std;string s, s0;void add(string &s, string &s0){ int h = 0; for (int i = (int)s.
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

string s, s0;

void add(string &s, string &s0){
    int h = 0;
    for (int i = (int)s.length() - 1; i >= 0; i--) {
        s[i] = s[i] + s0[i] + h - '0';
        if(s[i] > '9'){ s[i] = s[i] - 10; h = 1;}
        else h = 0;
    }
    if (h) s = "1" + s;
}

int main() {
    cin >> s;
    s0 = s;
    reverse(s0.begin(), s0.end());
    if (s == s0) {
        printf("%s is a palindromic number.\n", s.c_str()); return 0;
    }else{
        for (int i = 0; i < 10; i++) {
            s0 = s;
            reverse(s.begin(), s.end());
            if(s == s0) {printf("%s is a palindromic number.", s.c_str()); return 0;}
            printf("%s + %s = ", s0.c_str(), s.c_str());
            add(s, s0);
            printf("%s\n", s.c_str());
        }
    }
    printf("Not found in 10 iterations.\n");
    return 0;
}

目录
相关文章
LeetCode 131. Palindrome Partitioning
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。 返回 s 所有可能的分割方案。
67 0
LeetCode 131. Palindrome Partitioning
|
存储
LeetCode 132. Palindrome Partitioning II
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。 返回符合要求的最少分割次数。
70 0
LeetCode 132. Palindrome Partitioning II
|
索引
LeetCode 336. Palindrome Pairs
给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。
104 0
LeetCode 336. Palindrome Pairs
【1136】A Delayed Palindrome (20分)
【1136】A Delayed Palindrome (20分) 【1136】A Delayed Palindrome (20分)
86 0
【LeetCode】Palindrome Pairs(336)
  Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a   palindrome.
87 0
1024. Palindromic Number (25)
#include #include #include #include #include using namespace std; bool judge(string s){ string st = s; reverse(st.
773 0
[LeetCode] Palindrome Pairs
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. Example 1:
1577 0
|
C++
【LeetCode】9. Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. 思考过程: 题目很简单,要求是判断一个数是否是回文数,回文数的定义就是数字翻转之后与原先的数一样的话就是回文数,比如 101 , 22, 1 等,所以要处理这个问题的话,只需要将一个数的最高位换到最低位,次高位换到第二低位,依次
1047 0
LeetCode - 9. Palindrome Number
9. Palindrome Number  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个数,判断这个数是不是回文数.
784 0