Reputation: 11
I wanted to solve a problem, yet my code passes only 8 out of 10 tests. The problem is to determine whether a number 1<=N<=10^9 can be a numeric polyndrome. 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. 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
Upvotes: 1
Views: 1385
Reputation: 11
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;
}
}
Upvotes: 1
Reputation: 648
I only changed in main else
part of your code.
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.
For Ex. 100 is not palindrome but you can make it by 00100 it possible because two 0's present at end.
same with 10 -> 010
2. some numbers which are ends with 0's and not palindromes but we can make them by adding leading 0's
10, 20, 30.....90 -> (respective palindrome) 010,020,.....090
100, 110, 200, 220,.....900, 990 -> (respective palindrome) 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
120, 130...190
210, 230, 240,....290 .
.
.
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. 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. I will talk about just else
part
i> So I will check first that how many 0's present at end of number(while loop). at least one 0's must be present.
ii> Then I am adding as many leading 0's present at end.( for loop).
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.
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. Sometime if you include same header file twice it might cause error. Also there is no requirement of <cmath>
library file. 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.
Upvotes: 0
Reputation: 15265
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.
If you have 2020, you can either add one leading 0 --> 02020 or remove a trailing 0 -> 202. Result is the same.
So, the algorithm will be.
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");
}
}
Upvotes: 0