Wednesday 18 May 2011

256-Quirksome squares

#include<iostream>
#include<stdio.h>
using namespace std;
long square[10001];
int main()
{
    long d,i,k,lim,p1,p2,div;
    //freopen("in.txt","r",stdin);
    for(i=0;i<=10000;i++)
        square[i]=i*i;
    while(scanf("%ld",&d)==1)
    {
        switch(d)
        {
        case 2:
            lim=99;
            div=10;
            break;
        case 4:
            lim=9999;
            div=100;
            break;
        case 6:
            lim=999999;
            div=1000;
            break;
        case 8:
            lim=99999999;
            div=10000;
            break;
        }
        for(i=0;square[i]<=lim;i++)
        {
            p1=square[i]/div;
            p2=square[i]%div;
            k=p1+p2;
            cout.width(d);
            cout.fill('0');
            if(k*k==square[i])
                cout<<square[i]<<endl;
        }
    }
    return 0;
}

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I think, that the switch is unnecesary, yo can do something like this...

    lim=math.pow(10,d)-1;
    div=math.pow(10,d-1)

    ;)

    ReplyDelete