Created
December 22, 2010 15:59
-
-
Save gennad/751675 to your computer and use it in GitHub Desktop.
GoldenNumber.cpp
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 <sstream> | |
#include <stdlib.h> | |
#include <string> | |
#include <map> | |
int getIntVal(std::string strConvert) { | |
int intReturn; | |
// NOTE: You should probably do some checks to ensure that | |
// this string contains only numbers. If the string is not | |
// a valid integer, zero will be returned. | |
intReturn = atoi(strConvert.c_str()); | |
return(intReturn); | |
} | |
std::map<std::string, bool> isGolden(int number) { | |
std::string str; | |
std::stringstream out; | |
std::map<std::string, bool> result; | |
out << number; | |
str = out.str(); | |
int num = str.size(); | |
bool isGold = false; | |
const double MIN_PERCENTAGE = 0.7; | |
const double MIN_END_PERCENTAGE = 0.5; | |
const double MIN_MIRROR_PERCENTAGE = 0.4; | |
//same number | |
for (int i = 0; i < str.size(); i++) { | |
char digit = str[i]; | |
int numOfComparings = 0; | |
for (int j = 0; j < str.size(); j++) { | |
if (i == j) continue; | |
if (digit == str[j]) numOfComparings++; | |
} | |
double perc = numOfComparings / num; | |
if (perc > MIN_PERCENTAGE) { | |
isGold = true; | |
} | |
} | |
if (isGold == true) { | |
result["isGold"] = true; | |
result["desc"] = "by number of digits"; | |
return result; | |
} | |
//end of digit | |
int minCount; | |
if (num == 2 || num == 3) { | |
minCount = 2; | |
} else { | |
minCount = num * MIN_MIRROR_PERCENTAGE; | |
} | |
char endChar = str[str.size() - 1]; | |
for (int i = 2; i < minCount; i++ ) { | |
char curChar = str[str.size() - i]; | |
if (endChar != curChar) { | |
isGold = false; | |
} | |
isGold = true; | |
} | |
if (isGold == true) { | |
result["isGold"] = true; | |
result["desc"] = "by tail"; | |
return result; | |
} | |
//mirror | |
if (num == 2 || num == 3) { | |
minCount = 2; | |
} else { | |
minCount = num * MIN_END_PERCENTAGE; | |
} | |
for (int i = 0; i < minCount; i++) { | |
if (str[i] == str[str.size() - 1 -i]) | |
isGold = true; | |
else | |
isGold == false; | |
} | |
if (isGold == true) { | |
result["isGold"] = true; | |
result["desc"] = "by mirror"; | |
return result; | |
} | |
result["isGold"] = false; | |
result["desc"] = "not gold"; | |
return result; | |
} | |
int main(int argc, char**argv) { | |
// Prints welcome message... | |
std::cout << "Welcome ..." << std::endl; | |
//int number = 4321111; | |
int number = 12341; | |
std::map<std::string, bool> r = isGolden(number); | |
std::cout << r.find("isGold")->second; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment