1024. Palindromic Number (25)

简介: #include #include #include #include #include using namespace std;bool judge(string s){ string st = s; reverse(st.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;

bool judge(string s){
    string st = s;
    reverse(st.begin(), st.end());
    if(st == s) return true;
    else return false;
}

void add(string &s){
    string st = s;
    reverse(st.begin(), st.end());
    int c = 0;
    for (int i = (int)st.size() - 1; i >= 0; i--) {
        s[i] = s[i] + st[i] + c - '0';
        if(s[i] > '9'){
            s[i] = s[i] - 10;
            c = 1;
        }else c = 0;
        if((i == 0) && c == 1){
            s = "1" + s;
        }
    }
}

int main(){
    int k;
    string s, st;
    cin >> s >> k;
    if(judge(s)){
        cout << s << 0 << endl;
        return 0;
    }
    for (int i = 1; i <= k; i++) {
        add(s);
        if (judge(s)) {
            cout << s << endl << i << endl;
            return 0;
        }
    }
    cout << s << endl << k << endl;

    return 0;
}

目录
相关文章
|
人工智能
Constant Palindrome Sum
Constant Palindrome Sum
成功解决ValueError: Number of passed names did not match number of header fields in the file
成功解决ValueError: Number of passed names did not match number of header fields in the file
【1019】General Palindromic Number (20 分)
【1019】General Palindromic Number (20 分) 【1019】General Palindromic Number (20 分)
73 0
|
人工智能 C++
Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
745 0
Maximum Subsequence Sum
最大连续子列和问题,在此给出题解 (浙大PTA https://pintia.cn/problem-sets/16/problems/665)
|
文件存储
1019. General Palindromic Number (20)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Number.
891 0
[LeetCode]--9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to s
964 0
|
C++
【LeetCode】9. Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. 思考过程: 题目很简单,要求是判断一个数是否是回文数,回文数的定义就是数字翻转之后与原先的数一样的话就是回文数,比如 101 , 22, 1 等,所以要处理这个问题的话,只需要将一个数的最高位换到最低位,次高位换到第二低位,依次
1042 0