UVa 11879 – Multiple of 17


#include <cstdio>
using namespace std ;

int main(){
    int n[101],i,j,d,d1,d2;
    char temp ;
    while (true){
        i = 0;
        while (true){
            temp = getchar();

            if (temp == '\n')break;
            n[i++] = temp - '0';
        }
        if (i == 1 && n[0]==0)return 0;

        i--;

        for(j = i ; j>2 ; j--){

            d = n[j]*5; // d = d1+d2*10;
            d1 = d%10;
            d2 = d/10;

            if (n[j-1]>d1){
                n[j-2]--;
                n[j-1] = n[j-1]+10-d1;
            }else
                n[j-1] = n[j-1]-d1;

            if (n[j-2]>d2){
                n[j-3]--;
                n[j-2] = n[j-2]+10-d2;
            }else
                n[j-2] = n[j-2]-d2;

        }

        if (i == 1){
            temp = n[0];
            d = 5*n[1];
        }
        else {
            temp = n[1]+n[0]*10;
            d = 5*n[j];
        }

        if ((temp-d)%17==0)
            printf("1\n");
        else
            printf("0\n");

    }
    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s