Created
November 8, 2015 06:58
-
-
Save renyuntao/88639e0e283f429fd401 to your computer and use it in GitHub Desktop.
Fancy number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cmath> | |
#include<string> | |
#include<set> | |
using std::endl; | |
using std::cout; | |
bool isFancyNum(int value) | |
{ | |
std::set<char> st = {'0','1','6','8','9'}; | |
std::string num = std::to_string(value); | |
unsigned int len = num.length(); | |
for(int i = 0;i < len;++i) | |
{ | |
if(!st.count(num[i])) | |
return false; | |
} | |
if(len == 1) | |
return true; | |
else if(len & 1) // len is odd number | |
{ | |
int middle = len / 2; | |
int begin = 0; | |
int end = len - 1; | |
for(;begin < middle;++begin) | |
{ | |
if(num[begin] != num[end-begin] && abs(num[begin]-num[end-begin]) != 3) | |
return false; | |
--end; | |
} | |
} | |
else // len is even number | |
{ | |
cout<<"len is even\n"; | |
int middle = len / 2; | |
int begin = 0; | |
int end = len - 1; | |
for(;begin < middle;++begin) | |
{ | |
if(num[begin] != num[end-begin] && abs(num[begin]-num[end-begin]) != 3) | |
return false; | |
--end; | |
} | |
} | |
return true; | |
} | |
int main() | |
{ | |
int value; | |
std::cin>>value; | |
if(isFancyNum(value)) | |
cout<<"Is Fancy number.\n"; | |
else | |
cout<<"Is not Fancy number.\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment