吉大秦少游
2018-11-18 17:34
采纳率: 60%
浏览 1.6k

PAT乙级1079测试点2/3/4提示答案错误,有哪些情况未考虑到?

 // 1079 延迟的回文数 v1
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string reverse(string str){
    string rev;
    rev=str;
    for(int i=0;i<str.length();i++)
        rev[i] = str[str.length()-1-i];
    return rev;
}

int IsPal(string str){
    int flag=1;
    for(int i=0;i<str.length()/2;i++){
        if(str[i]!=str[str.length()-i-1]){
            flag=0;
            break;
        }
    }
    return flag;
}

string Pal(string str1,string str2){
//  string pal="  ";
    string pal;
    pal=str1;   //vital;
    int c=0;
    for(int i=0;i<str1.length();i++){   
        c=c+str1[i]-'0'+str2[i]-'0';
        pal[i]=c%10+'0';
//      cout<<pal[i];
        c/=10;
    }
    if(c)   pal+=c+'0';
    reverse(pal.begin() ,pal.end() );
    return pal;
}

int main(){
    string str,rev="",pal="";   cin>>str;   //input is char type
    int cnt=0;
    rev=reverse(str);
//  cout<<rev<<endl;
    pal=Pal(str,rev);
//  cout<<pal<<endl;
//  cout<<IsPal(pal);
    while(cnt<10){
        rev=reverse(str);
        pal=Pal(str,rev);
        if(IsPal(pal)){ //pal
            cout<<str<<" + "<<rev<<" = "<<pal<<endl;
            cout<<pal<<" is a palindromic number.";
            break;
        }
        else{
            cout<<str<<" + "<<rev<<" = "<<pal<<endl;
        }
        str=pal;
        cnt++;
    }
    if(cnt==10) cout<<"Not found in 10 iterations.";
    return 0;
} 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • Italink 2018-11-20 11:00

    如果一开始输入就为回文,直接输出,你可以修改一下判断顺序,或者直接先判断一次

     #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    string reverse(string str) {
        string rev;
        rev = str;
        for (int i = 0; i < str.length(); i++)
            rev[i] = str[str.length() - 1 - i];
        return rev;
    }
    
    int IsPal(string str) {
        int flag = 1;
        for (int i = 0; i < str.length() / 2; i++) {
            if (str[i] != str[str.length() - i - 1]) {
                flag = 0;
                break;
            }
        }
        return flag;
    }
    
    string Pal(string str1, string str2) {
        //  string pal="  ";
        string pal;
        pal = str1;   //vital;
        int c = 0;
        for (int i = 0; i < str1.length(); i++) {
            c = c + str1[i] - '0' + str2[i] - '0';
            pal[i] = c % 10 + '0';
            //      cout<<pal[i];
            c /= 10;
        }
        if (c)   pal += c + '0';
        reverse(pal.begin(), pal.end());
        return pal;
    }
    
    int main() {
        string str, rev = "", pal = "";   
        cin >> str;   //input is char type
        int cnt = 0;
        if (IsPal(str)) { //pal
            cout << str << " is a palindromic number.";     
        }
        else {
            rev = reverse(str);
            //  cout<<rev<<endl;
            pal = Pal(str, rev);
            //  cout<<pal<<endl;
            //  cout<<IsPal(pal);
            while (cnt < 10) {
                rev = reverse(str);
                pal = Pal(str, rev);
                if (IsPal(pal)) { //pal
                    cout << str << " + " << rev << " = " << pal << endl;
                    cout << pal << " is a palindromic number.";
                    break;
                }
                else {
                    cout << str << " + " << rev << " = " << pal << endl;
                }
                str = pal;
                cnt++;
            }
            if (cnt == 10) cout << "Not found in 10 iterations.";
        }
        return 0;
    }
    
    点赞 评论

相关推荐 更多相似问题