TJU : 3040 (Hide That Number)



// http://acm.tju.edu.cn/toj/showp3040.html
// Runtime: 0'00.08"
// Memory: 3156K

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>

#define INT_MAX 2147483647
#define INT_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL long long

#define For(i, a, b) for ( int i = (a); i < (b); i++ )
#define Forv(i, sz) for ( size_t i = 0; i < sz.size (); i++ )
#define Set(a, s) memset (a, s, sizeof (a))

using namespace std;

char a [N + 10];
char res [N + 10];

int main ()
{
    int cases = 0;

    while ( scanf ("%s", a) && strcmp (a, "0") ) {
        int len = strlen (a);

        reverse (a, a + len);

        res [0] = a [0];
        int tmp = res [0] - '0';

        for ( int i = 1; i < len; i++ ) {
            int diff = (a [i] - '0') - tmp;
            if ( diff < 0 ) {
                res [i] = (10 + diff) + '0';
                tmp = (res [i] - '0') + 1;

            }
            else {
                res [i] = diff + '0';
				tmp = res [i] - '0';
            }
        }

        res [len] = 0;

        reverse (res, res + len);

        if ( res [0] != '0' ) printf ("%d. %s\n", ++cases, res);
		else printf ("%d. IMPOSSIBLE\n", ++cases);
    }

	return 0;
}

// @END_OF_SOURCE_CODE
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