UVa : 263


// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <sstream>
#include <set>
#include <math.h>
#define pi acos(-1.0)
#define N 1000000
using namespace std;


int main ()
{
    int n;

    while ( scanf ("%d", &n) && n ) {
        char num [20];
        int chainLength = 1;
        sprintf (num, "%d", n);

        printf ("Original number was %d\n", n);

        sort (num, num + strlen (num));

        char num2 [20];
        strcpy (num2, num);

        reverse (num2, num2 + strlen (num2));

        int first = atoi (num2);
        int second = atoi (num);
        int subs = first - second;
        printf ("%d - %d = %d\n", first, second, subs);
        map <int, bool> m;

        while ( m [subs] == false ) {
            m [subs] = true;

            sprintf (num, "%d", subs);
            sort (num, num + strlen (num));
            strcpy (num2, num);
            reverse (num2, num2 + strlen (num2));

            first = atoi (num2);
            second = atoi (num);
            subs = first - second;

            printf ("%d - %d = %d\n", first, second, subs);
            chainLength++;
        }

        printf ("Chain length %d\n\n", chainLength);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

One thought on “UVa : 263

  1. /*Not Use In Map*/

    #include
    #include
    #include
    #include
    using namespace std;
    int main()
    {
    char num1[20];
    char num2[20];
    char str[20];
    int n,first,second,res,x,chain_getlength,i,j;
    while(scanf(“%d”,&n))
    {
    if(n==0) break;
    sprintf(num1,”%d”,n);
    printf(“Original number was %d\n”,n);
    chain_getlength=1;
    x=strlen(num1);
    sort(num1,num1+x);
    strcpy(num2,num1);
    reverse(num2,num2+strlen(num2));
    first=atoi(num2);
    second=atoi(num1);
    res=first-second;
    printf(“%d – %d = %d\n”,first,second,res);
    int a[1000],k=0;
    bool flag=false;
    while(!flag)
    {
    a[k++]=res;
    sprintf(num1,”%d”,res);
    x=strlen(num1);
    sort(num1,num1+x);
    strcpy(num2,num1);
    reverse(num2,num2+strlen(num2));
    first=atoi(num2);
    second=atoi(num1);
    res=first-second;
    printf(“%d – %d = %d\n”,first,second,res);
    chain_getlength++;
    for(i=0;i<k;i++)
    {
    if(a[i]==res)
    {
    flag=true;
    break;
    }
    }
    }
    printf("Chain length %d\n\n",chain_getlength);
    }
    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