吉大秦少游 2018-11-18 17:34 采纳率: 0%
浏览 1624

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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀