google code jam 2013: qualification round:Problem C. Fair and Square

Problem can be found here:
http://code.google.com/codejam/contest/2270488/dashboard#s=p2

This code is just for the first two tests.

Code:

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <vector>

using namespace std;


bool check(long long a){
    stringstream ss;
    ss.clear();
    ss << a;
    string s;
    s = ss.str();
    int i=0;
    int j=s.size()-1;
    while (i<j){
        if (s[i]!=s[j]){
            return false;
        }
        i++;
        j--;
    }
    return true;
}

void findres(vector<long long > &restab){
    vector<string> tab;
    for (int i=1;i<10000;i++){
        stringstream ss;
        ss << i;
        string s =ss.str();
        string sr;
        sr.assign(s.rbegin(),s.rend());
        s = s.append(sr);
        tab.push_back(s);
        s.erase(sr.size(),1);
        tab.push_back(s);
    }
    for (int i=0;i<tab.size();i++){
        stringstream ss;
        ss <<tab[i];
        long long num;
        ss >> num;
        if (check(num*num)){
            restab.push_back(num*num);
        }
    }
}

int main()
{
    ifstream input("C-large-practice-1.in");
    ofstream output("output.txt");
    vector <long long> restab;
    findres(restab);
    int casenum;
    input >> casenum;
    for (int c=0; c<casenum;c++){
        long long a,b;
        input >> a;
        input >> b;
        int res = 0;
        for (int i=0;i<restab.size();i++){
            if ((restab[i]>=a) && (restab[i]<=b)){
                res++;
            }
        }

        output << "Case #" << c+1 <<": " << res << endl;
    }
    input.close();
    output.close();
}

No comments:

Post a Comment