ACM (TJU) : 2577


// http://acm.tju.edu.cn/toj/showp2577.html

#include <stdio.h>

int main ()
{
    int n;
    scanf ("%i", &n);

    while ( n-- ) {
        int x;
        int temp;

        scanf ("%i", &x);

        if ( x > 10 ) {

            switch (true) {

                case 1:
                temp = x % 10;

                if ( 10 - temp <= temp )
                    x += (10 - temp);
                else
                    x -= temp;

                if ( x <= 100 ) {
                    printf ("%i\n", x);
                    break;
                }

                case 2:
                temp = x % 100;

                if ( 100 - temp <= temp )
                    x += (100 - temp);
                else
                    x -= temp;

                if ( x <= 1000 ) {
                    printf ("%i\n", x);
                    break;
                }

                case 3:
                temp = x % 1000;

                if ( 1000 - temp <= temp )
                    x += (1000 - temp);
                else
                    x -= temp;

                if ( x <= 10000 ) {
                    printf ("%i\n", x);
                    break;
                }

                case 4:
                temp = x % 10000;

                if ( 10000 - temp <= temp )
                    x += (10000 - temp);
                else
                    x -= temp;

                if ( x <= 100000 ) {
                    printf ("%i\n", x);
                    break;
                }

                case 5:
                temp = x % 100000;

                if ( 100000 - temp <= temp )
                    x += (100000 - temp);
                else
                    x -= temp;

                if ( x <= 1000000 ) {
                    printf ("%i\n", x);
                    break;
                }

                case 6:
                temp = x % 1000000;

                if ( 1000000 - temp <= temp )
                    x += (1000000 - temp);
                else
                    x -= temp;

                if ( x <= 10000000 ) {
                    printf ("%i\n", x);
                    break;
                }

                case 7:
                temp = x % 10000000;

                if ( 10000000 - temp <= temp )
                    x += (10000000 - temp);
                else
                    x -= temp;

                if ( x <= 100000000 ) {
                    printf ("%i\n", x);
                    break;
                }
            }
        }

        else
            printf ("%i\n", x);
    }

    return 0;
}

One thought on “ACM (TJU) : 2577

  1. // Same logic in different approach
    // Author : Admin
    
    #include <stdio.h>
    
    int main ()
    {
        int n;
        scanf ("%i", &n);
    
        while ( n-- ) {
            int x;
            int temp;
            int power = 10;
    
            scanf ("%i", &x);
    
            if ( x <= 10 )
                printf ("%i\n", x);
    
            while ( x > power ) {
                temp = x % power;
    
                if ( power - temp <= temp )
                    x += (power - temp);
                else
                    x -= temp;
    
                power *= 10;
    
                if ( x <= power )
                    printf ("%i\n", x);
            }
        }
    
        return 0;
    }
    

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