UVA Problem 10929 – You can say 11 Solution

UVA Problem 10929 – You can say 11 Solution:


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

Solving Technique:

For this problem use a string to handle input, even unsigned long long can’t hold a large number containing 1000 digits. 

Found the logic for this code here on Algorithmist. Another way to solve this problems explained there.

We can check if a large number is divisible by another number by subtracting the sum of even index number from odd index numbers. For example,

if, ABCD, A-B+C-D % N == 0, then the number is divisible by N
if, 2937, 2-9+3-7 % 11 == -11 % 11 = 0, So, 2937 is divisible by 11

The input may have leading zeros.

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


Input:

112233
30800
2937
323455693
5038297
112234
0

Output:

112233 is a multiple of 11.
30800 is a multiple of 11.
2937 is a multiple of 11.
323455693 is a multiple of 11.
5038297 is a multiple of 11.
112234 is not a multiple of 11.

Code:

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 * Problem: UVA 10929 ( You can say 11 )
 */

#include<stdio.h>
#include<string.h>

static char s[1024];

int main(){
    unsigned i;

    while(gets(s)){
        unsigned len = strlen(s);
        if(s[0] == '0' && len == 1)
            return 0;

        int sum = 0;
        for(i = 0; i < len; i += 2)
            sum += s[i] - '0';

        for(i = 1; i < len; i += 2)
            sum -= s[i] - '0';

        printf(sum % 11 ? "%s is not a multiple of 11.\n" : "%s is a multiple of 11.\n", s);
    }
    return 0;
}

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