代码之家  ›  专栏  ›  技术社区  ›  Amit Ray

回文问题给出答案但不被接受

  •  1
  • Amit Ray  · 技术社区  · 6 年前

    网站www.spoj.com中给出了Pallindrome的一个问题。问题陈述是- 输入

    第一行包含整数t,即测试用例的数量。整数K在接下来的t行中给出。

    我已经为此编写了一个CPP代码

    #include <iostream>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <sstream>
    #include <bits/stdc++.h>
    #include<limits>
    using namespace std;
    
    int main(){
        int b;
        cin>>b;
        int num[b];
        for(int i=0;i<b;i++){
            cin>>num[i];
            if(num[i]>1000000){
                //cout<<"Enter value less than 1000000"<<endl;
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                //cout<<"You have entered wrong input"<<endl;
                cin>>num[i];
            }
        }
        string a[b];
        for(int i=0;i<b;i++){
            long numb = num[i] + 1;
    
            if(!(numb>1000000 || num[i] <10 )){
                for(int k=numb; k<1000000; k++){
                    stringstream ss;
                    ss << k;
                    string str=ss.str();
                    string ostr = str;
                    reverse(str.begin(), str.end());
                    if(ostr == str){
                        a[i] = ostr;
                        //cout<<ostr<<endl;
                        break;
                    }
                }
                cout<<a[i]<<endl;
            }
    
        }
    
    }
    

    #include <iostream>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <sstream>
    //#include <bits/stdc++.h>
    #include<limits>
    using namespace std;
    
    int main(){
        int b;
        cin>>b;
        int num[b];
        for(int i=0;i<b;i++){
            cin>>num[i];
            if(num[i]>1000000){
                //cout<<"Enter value less than 1000000"<<endl;
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                //cout<<"You have entered wrong input"<<endl;
                cin>>num[i];
            }
        }
        string a[b];
        for(int i=0;i<b;i++){
            long numb = num[i] + 1;
    
            if(!(numb>1000000 || num[i] <10 )){
                for(int k=numb; k<1000000; k++){
                    stringstream ss;
                    ss << k;
                    string str=ss.str();
                    string ostr = str;
    
                    int len = str.length();
                    int n=len-1;
                    for(int m=0;m<(len/2);m++){
                        //Using the swap method to switch values at each index
                        swap(str[m],str[n]);
                        n = n-1;
                    }
                    //reverse(str.begin(), str.end());
                    if(ostr == str){
                        a[i] = ostr;
                        //cout<<ostr<<endl;
                        break;
                    }
                }
                cout<<a[i]<<endl;
            }
    
        }
    
    }
    
    1 回复  |  直到 6 年前