Skip to content

Instantly share code, notes, and snippets.

@migueldferreira
Created June 15, 2013 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save migueldferreira/e634a8c80bb4af5c258b to your computer and use it in GitHub Desktop.
Save migueldferreira/e634a8c80bb4af5c258b to your computer and use it in GitHub Desktop.
Simple C++ program that calculates the compatibility percentage between two people based on their first name! Note that the percentages may vary depending on the order of the names: one person may love the other more or less than that one loves the first one!
#include<iostream>
#include<vector>
int main() {
std::string nameConcat, name;
std::vector<int> counter;
std::cout << "First name of the 1st person (lowercase): ";
std::cin >> nameConcat;
nameConcat += " ama ";
std::cout << "First name of the 2nd person (lowercase): ";
std::cin >> name;
nameConcat += name;
std::cout << nameConcat << std::endl;
for(size_t i = 0; i < nameConcat.size(); i++) {
char current = nameConcat[i];
if(current == ' ') continue;
int currentCount = 0;
for(int pos = i; pos < nameConcat.size(); pos++) {
if(nameConcat[pos] != current) continue;
nameConcat[pos] = ' ';
currentCount++;
}
if(currentCount >= 10) counter.push_back(currentCount/10);
counter.push_back(currentCount % 10);
}
int size;
while((size = counter.size()) > 2) {
for(std::vector<int>::iterator it = counter.begin(); it < counter.end(); it++)
std::cout << *it << ' ';
std::cout <<std::endl;
std::vector<int> aux;
int i;
for(i = 0; i < size/2; i++) {
int sum = counter[i] + counter[size-1-i];
if(sum >= 10) aux.push_back(sum/10);
aux.push_back(sum % 10);
}
if(size % 2 == 1) aux.push_back(counter[i]);
counter = aux;
}
int result;
if(counter.size() == 1) result = counter[0];
else result = counter[0] * 10 + counter[1];
std::cout << result << "%" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment