UVA Problem 11879 – Multiple of 17 Solution

UVA Problem 11879 – Multiple of 17 Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

Run time 0.009 s ( 9 ms ), Rank 868.

This problem requires us to take input as a string. We have to use a string since no built in data type can hold 10^100 digits. Although there maybe other ways to solve this ( JAVA BIG INTEGER ) but i will focus on C++.

We could solve this problem by dividing the input and in the end checking if there is no remainder. Here i take each digit of the input then check if it is divisible by 17. If it is not divisible then i keep multiplying by 10 each time and sum current digit to it. In the end if there is no remainder ( meaning remainder is ZERO ) then the input is divisible by 17.

Another way to write that one line of code below,

remainder = remainder * 10;           /* For the first iteration remainder is set to 0 */
remainder = remainder + s[i] - '0';   /* s[i] is a character, - 48 makes it an Integer */
remainder = remainder % 17;

The above piece of code is iterated for the whole input string.

Important:  Be sure to add or print a new line after each output unless otherwise specified. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Input:

34
201
2098765413
1717171717171717171717171717171717171717171717171718
0

Output:

1
0
1
0

Code:

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 * Problem: UVA 11879 ( Multiple of 17 )
 */

#include<stdio.h>

static char s[128];

int main(){
    register unsigned int i;
    unsigned remainder;

    while(gets(s)){
        if(s[0] == 48 && s[1] == 0) return 0;                   /* 48 is character '0' and 0 is '\0' NUL character */

        for(remainder = i = 0; s[i]; ++i)
            remainder = ( remainder * 10 + s[i] - 48 ) % 17;    /* Keep multiplying the remainder by 10 and add the current digit, then mod by 17 to get remainder */

        remainder ? printf("0\n") : printf("1\n");              /* If the number is divisible by 17 then remainder in the end becomes ZER0 */
    }
    return 0;
}

One thought on “UVA Problem 11879 – Multiple of 17 Solution

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s