02 November 2015

uva 263


/***
Md. Namzul Hasan
Shahjalal University of Science & technology,sylhet.
hasan08sust@gmail.com
verdict: accepted
***/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cctype>
#include<sstream>
#include<stdlib.h>
#include<map>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))

string data ,niloy ;
queue <char> jak , nam;
char jaki[100000] ;
in t, m ,k, len, ln   ;
char ch , j ;
ll n, x1, x2, mx= -1 , cnt  ;
in cont1[11] , cont2[11] ;

void change()
{
    MEM(cont1, 0);
MEM(cont2, 0);
    while(n)
    {
        cont1[n%10]++ ;
        cont2[n%10]++ ;
        n = n/10 ;
    }
    x1 =x2 = 0 ;
    for(in i=0 ; i<10 ; i++)
    {
        while(cont1[i]--)
        {
            x1 = (x1*10) + i ;
        }
    }
    for(in i=9 ; i>=0 ; i--)
    {
        while(cont2[i]--)
        {
            x2 = (x2*10) + i ;
        }
    }
}

map<ll,in> ml ;
in main()
{
    while(cin>>n && n)
    {
        pf("Original number was %lld\n",n) ;
        ml.clear() ;
        while(1)
        {
            change() ;
            pf("%lld - %lld = %lld\n",x2,x1,x2-x1) ;
            n = x2 -x1 ;
        if (ml.count(n)) break;
ml[n] = 1;

        }
        printf("Chain length %d\n\n", ml.size() + 1);
    }
    return 0 ;
}

No comments:

Post a Comment

UVA 10679 - I Love Strings!!