#include <iostream>
#include <ctime>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int sum (string);
int main()
{
clock_t start = clock();
long int total = 0;
char ch;
ifstream fi;
vector<string> v;
bool table [100000];
for (unsigned int i = 0; i < 100000; i++)
table[i] = false;
for (unsigned int i = 0; ; i++)
if (static_cast<int>((1/static_cast<double> (2)) * static_cast<double> ((i + 1) * ((i + 1) + 1))) > 100000)
break;
else
table[static_cast<int>((1/static_cast<double> (2)) * static_cast<double> ((i + 1) * ((i + 1) + 1)))] = true;
fi.open ("words.txt");
while (!fi.eof ())
{
v.resize (v.size () + 1);
ch = fi.get ();
while (ch != ',' && !fi.eof ())
{
v[v.size () - 1] += ch;
ch = fi.get ();
}
}
for (unsigned int i = 0; i < v.size (); i++)
if (table[sum (v[i])] == true)
total++;
fi.close ();
cout << total << endl
<< "Process took " << (static_cast<double> (clock()) - start) / CLOCKS_PER_SEC << " seconds." << endl;
system("pause");
return 0;
}
int sum (string str)
{
int total = 0;
for (unsigned int i = 0; i < str.size (); i++)
if (isalpha (str[i]))
total += tolower (str[i]) - 96;
return total;
}
Tuesday, November 18, 2008
Problem #42
This one was just a modification of the source code for a previous version.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment