简体   繁体   English

带前导零的数字回文 c++

[英]numeric palindrome with leading zeros c++

I wanted to solve a problem, yet my code passes only 8 out of 10 tests.我想解决一个问题,但我的代码只通过了 10 次测试中的 8 次。 The problem is to determine whether a number 1<=N<=10^9 can be a numeric polyndrome.问题是判断一个数字 1<=N<=10^9 是否可以是一个数字多项式。 The thing is, you may add as many leading zeros as it requires to make a non-polyndrome into a polyndrome.问题是,您可以根据需要添加尽可能多的前导零,从而将非多项式转化为多项式。 If it is possible, or a number is polyndrome, the result must be yes, otehrwise no.如果可能,或者一个数字是多项式,则结果必须是是,否则不是。 For example, 2020 is not a polyndrome, but If I add a leading zero, it becomes 02020, which is a polyndrome.例如,2020 不是一个多项式,但如果我添加一个前导零,它就会变成 02020,这是一个多项式。 One main problem of my code is that i don't know the number of leading zeros needed to make a number a polyndrome.我的代码的一个主要问题是我不知道使数字成为多项式所需的前导零的数量。 Here's my code:这是我的代码:

#include <cstdio>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    string N, N2;
    cin >> N;
    N2 = N;
    reverse(N.begin(), N.end());
    if (N2 == N) {
        cout << "Yes" << "\n";
        return 0;
    }
    else {
        N2 = "0" + N2;
        N = N + "0";
        if (N != N2) {
            cout << "No" << "\n";
            return 0;
        }
        else {
            cout << "Yes";
            return 0;
        }
    }
}

I would be grateful for any help to enhance my code: edit: I have to add leading zeros if it can turn a number into a polyndrome, that's the main thing如果能帮助我改进我的代码,我将不胜感激:编辑:如果它可以将数字变成多态数,我必须添加前导零,这是最主要的

I would do something like:我会做类似的事情:

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    string n;
    cin>>n;
    string m =n;
    reverse(n.begin(),n.end());
    if(n==m){
        cout<<"Yes"<<endl;
        return 0;
 }
  int count=0;
  int k=m.length();
 for(int i=1; i<=k;i++){
     if(m[i]=='0'){
         count+=1;
     }  
 }

 for(int i=1;i<=count;i++){
     m='0'+m;
 }
 string m2=m;
 reverse(m.begin(), m.end());
 if (m2 == m)
 {
     cout << "Yes" << endl;
     return 0;
}
else{
    cout << "No" << endl;
    return 0;
}
}

Just to be complete, I will show you the answer based on the comments.为了完整起见,我将根据评论向您展示答案。

It is the same, if you add 0es to the beginning or remove them from the end.如果您在开头添加 0 或从末尾删除它们,也是一样的。

If you have 2020, you can either add one leading 0 --> 02020 or remove a trailing 0 -> 202. Result is the same.如果您有 2020,您可以添加一个前导 0 --> 02020 或删除一个尾随 0 -> 202。结果是相同的。

So, the algorithm will be.所以,算法将是。

  1. Search from the right end for the first character that is not '0'从右端开始搜索第一个不是“0”的字符
  2. Build a pair forward and a pair reverse iterators, based on the iterator, found by `std::find基于由`std::find 找到的迭代器,构建一对正向和一对反向迭代器
  3. Compare temporary created strings, build from forward and reverse iterators比较临时创建的字符串,从正向和反向迭代器构建
  4. Output comparison result Output比较结果

Very simple example code:非常简单的示例代码:

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

int main() {
    if (std::string numberAsString{}; std::cin >> numberAsString) {

        std::string::reverse_iterator newEnd = std::find_if(numberAsString.rbegin(), numberAsString.rend(), [](const char c) {return c != '0'; });

        std::cout << (std::string(numberAsString.begin(), newEnd.base()) == std::string(newEnd, numberAsString.rend()) ? "Yes\n" : "No\n");
    }
}

I only changed in main else part of your code.我只更改了代码的主要else部分。

1. Now understand if some numerical string is not palindrome and you are going to make it palindrome by adding leading 0's then it must end with 0's. 1.现在了解如果某个数字字符串不是回文,并且您将通过添加前导 0 使其成为回文,那么它必须以 0 结尾。

For Ex.对于前。 100 is not palindrome but you can make it by 00100 it possible because two 0's present at end. 100 不是回文,但你可以通过 00100 使它成为可能,因为末尾有两个 0。

same with 10 -> 010与 10 -> 010 相同

2. some numbers which are ends with 0's and not palindromes but we can make them by adding leading 0's 2.一些以 0 结尾而不是回文的数字,但我们可以通过添加前导 0 来生成它们

10, 20, 30.....90 -> (respective palindrome) 010,020,.....090 10, 20, 30.....90 -> (各自的回文) 010,020,.....090

100, 110, 200, 220,.....900, 990 -> (respective palindrome) 00100, 0110, 00200, 0220.....00900, 0990 100, 110, 200, 220,.....900, 990 -> (各自的回文) 00100, 0110, 00200, 0220.....00900, 0990

3. some numbers which are ends with 0's and not palindromes and we can't make them by adding leading 0's 3.一些以 0 结尾而不是回文的数字,我们不能通过添加前导 0 来生成它们

120, 130...190 120, 130...190

210, 230, 240,....290. 210、230、240、....290。

. .

. .

11010 11010

4. Now if you see carefully the numbers which we can make palindrome you get that to be able to do so we have to add exact number of 0's at starting as present at the end. 4.现在,如果您仔细查看我们可以制作回文的数字,您会发现能够做到这一点,因此我们必须在开头添加确切数量的 0,就像在结尾一样。 if you pause for minute and think then you get logical sense too.如果你停下来思考一下,那么你也会有逻辑感。

5. Now I am creating condition to right code on above analysis. 5.现在我正在为上述分析的正确代码创造条件。 I will talk about just else part我将谈论else部分

i> So I will check first that how many 0's present at end of number(while loop). i>所以我将首先检查数字末尾有多少个 0(while 循环)。 at least one 0's must be present.必须至少存在一个 0。

ii> Then I am adding as many leading 0's present at end.( for loop). ii>然后我在末尾添加尽可能多的前导 0。(for 循环)。

iii> Then storing N2 in N3 so I can reverse and check palindrome become or not because all numbers end with 0's do not become palindromes. iii>然后将N2存储在N3中,这样我就可以反转并检查回文是否变成了,因为所有以 0 结尾的数字都不会变成回文。

Code代码

#include <iostream>
#include <algorithm>
#include<string>
using namespace std;


int main()
{
    string N, N2;
    cin >> N;
    N2 = N;
    reverse(N.begin(), N.end());
    cout<<N<<" "<<N2<<"\n";

    if (N2 == N)
    {
        cout << "Yes" << "\n";
        return 0;
    }
    else
    {

        int k=N2.length(), count=0, i=1;

        while(N2[k-i]=='0')
        {
            count++;
            i++;
        }
        if(count==0)
        {
            cout<<"No";
            return 0;
        }
        for(int i=1; i<=count; i++)
        {
            N2="0" + N2;
        }
        string N3=N2;
        reverse(N2.begin(), N2.end());
        cout<<N3<<" "<<N2<<"\n";

        if (N3 != N2)
        {
            cout << "No" << "\n";
            return 0;
        }
        else
        {
            cout << "Yes";
            return 0;
        }

    }
}

Also just note that you added <cstdio> two times even it didn't required one time also.另请注意,您添加了<cstdio>两次,即使它也不需要一次。 Sometime if you include same header file twice it might cause error.有时,如果您包含相同的 header 文件两次,可能会导致错误。 Also there is no requirement of <cmath> library file.也不需要<cmath>库文件。 By the way you didn't include <string> library file might be in some cases your program run quite well without including it but it is not standard practise.顺便说一句,您没有包含<string>库文件,在某些情况下,您的程序在不包含它的情况下运行得很好,但这不是标准做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM