PTA乙级题 1079. 延迟的回文数 (20)

【题目链接】


因为其倒转后第一个数字若等于0时,需要输出,所以要用数组。

#include<iostream> 
#include<string> 
using namespace std;  
string str1,str2,result;  
void add(){  
    result.clear();  
    int len = str1.size();  
    int jin = 0;  
    for(int i = len-1;i >= 0;i--){ 
        jin += str1[i]-'0'+str2[i]-'0';  
        result.insert(result.begin(),(jin%10)+'0');  
        jin /= 10;  
    }  
    if(jin > 0){  
        result.insert(result.begin(),jin+'0');  
    }  
}  
bool judge(){  
    int len = result.size();  
    for(int i = 0;i < len/2;i++){  
        if(result[i] != result[len-1-i]){  
            return false;  
        }  
    }  
    return true;  
}  
int main()
{
    cin>>str1;  
    int len;  
    int cnt = 1;  
    result = str1;  
    if(judge()){  
        cout<<result;  
        printf(" is a palindromic number.\n");  
        return 0;  
    }  
    while(cnt <= 10){  

        cnt++;  
        str2.clear();  
        len = str1.size();  
        for(int i = 0;i < len;i++){  
            str2.insert(str2.begin(),str1[i]);  
        }  
        cout<<str1<<" + "<<str2<<" = ";  
        add();  
        cout<<result<<endl;  
        if(judge()&&cnt<11){  
            cout<<result;  
            printf(" is a palindromic number.\n");  
            break;  
        } else if(cnt>10){  
            printf("Not found in 10 iterations.\n");  
        }  
        str1.clear();  
        str1 = result;  
    }  

    return 0;  
}