Friday, September 28, 2018

UVa problem soultion 725 - Division

problem link:

Discuss: this is just simple implementation problem. just simply check every digit using while loop you will get the output.

try yourself, before see the code



#include<bits/stdc++.h>
using namespace std;
int valid(int n,int m){
    int ara[10],i,r,j;
    memset(ara,0,sizeof ara);
    ara[0]=1;
    while(1){
        r=n%10;
        n/=10;
        if(ara[r]==1)
            return 0;
        else
            ara[r]=1;
            if(n==0)
                break;
    }
      while(1){
        r=m%10;
        m/=10;
        if(ara[r]==1)
            return 0;
        else
            ara[r]=1;
            if(m==0)
                break;
    }

    return 1;
}
int valid1(int n,int m){
    int ara[10],i,r,j;
    memset(ara,0,sizeof ara);
    while(1){
        r=n%10;
        n/=10;
        if(ara[r]==1)
            return 0;
        else
            ara[r]=1;
            if(n==0)
                break;
    }
      while(1){
        r=m%10;
        m/=10;
        if(ara[r]==1)
            return 0;
        else
            ara[r]=1;
            if(m==0)
                break;
    }

    return 1;
}
int main()
{
   // freopen("input.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    int n,f1=0;
    while(cin >>n&&n){
            int i,j,cnt=0,mn=1234,mx=99999,flag=0;
            if(f1&&n)
                   cout <<endl;
            else
                f1=1;
    while(1)
    {
         j=n*mn;
         if(j>mx)
         {
             break;
         }
         else
         {
             if(valid(j,mn)&&j>12345){
             flag=1;
             printf("%d / %05d = %d\n",j,mn,n);
             }
         }
         mn++;
        // cout <<j<<' ';
    }
    mn=10000;
    while(1)
    {
       // cout <<"Y ";
         j=n*mn;
         if(j>mx)
         {
             break;
         }
         else
         {
             if(valid1(j,mn)){
             flag=1;
             printf("%d / %5d = %d\n",j,mn,n);
             }
         }
         mn++;
        // cout <<j<<' ';
    }

    if(!flag)
        printf("There are no solutions for %d.\n",n);

    }
    return 0;
}

No comments:

Post a Comment