martes, 10 de abril de 2012

Project Euler 42

#include <iostream>
#include <fstream>
using namespace std;
bool esT[200];
void generarTrian()
{
    for(int i = 0 ; i<200; i++)esT[i] = false;
    int s = 1;
    for(int c = 2; s<200; c++)
    {
        esT[s] = true;
        s += c;
    }
}
int main()
{
    generarTrian();
    ifstream fin("words.txt");
    string cad ;
    fin >> cad;
    cad += ",";
    int maxi = 0;
    int a,b;
    a = 0;
    b = -1;
    for(int i = 1 ; i<cad.size(); i++)
    {
        while(i<cad.size() and cad[i]!=',')
            i++;
        a = b+1;
        b = i;
        int tmp = 0 ;
        for(int j = a+1; j<b-1; j++)
            tmp += (cad[j]-'A'+1);

        if(esT[tmp])
            maxi++;
    }
    cout << maxi << endl;
    return 0;
}

No hay comentarios:

Publicar un comentario