Translate

Views

Saturday, June 17, 2023

Solution UVA: 725 - Division

 

 Problem  VerdictLangTimeBestRankSubmit Time
 | discuss725 - Division AcceptedC++110.0800.00036587 mins ago

Suggest:
- I iterate 5 nested for loops to generate numbers (It seems that iterating a single loop from 1234 to 98765 is simpler. :))
- I will skip and move on to the next number if any of the following conditions are true:
+ The generated number x is not divisible by n
+ x <= 999
+ x/n <= 999
+ The digits of x and x/n are the same (the function f will perform the check and don't forget to add 0 in front if the number has less than 5 digits!)


#include<bits/stdc++.h>
using namespace std;

int n;

bool f(string z){
    for(int i=0; z[i+1]; i++)
        for(int j=i+1; z[j]; j++)
            if (z[i]==z[j]) return true;
    return false;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
   
    bool first= true;
    while(cin >> n && n){
        bool have_solution= false;
        if (!first){
            cout<<"\n";
        }
        for(int i=0; i<=9; i++)
        for(int j=0; j<=9; j++)
        if (i!=j)
        for(int k=0; k<=9; k++)
        if (k!=j && k!=i)
        for(int l=0; l<=9; l++)
        if (l!=k && l!=j && l!=i)
        for(int m=0; m<=9; m++)
        if (m!=k && m!=j && m!=i && m!=l){
           
            int x= i*10000+j*1000+k*100+l*10+m;
           
            if (x%n) continue;
           
            if (x<=999) continue;
           
            if (x/n<=999) continue;
       
           
            string z= to_string(x/n);  
            string s= to_string(x);
            while(z.size()<5) z='0'+z;
            while(s.size()<5) s='0'+s;
           
           
            if (f(z+s)) continue;      
   
            have_solution= true;
            cout<<x<<" / "<<z<<" = "<<n<<"\n";
        }
       
        if (!have_solution){
            cout<<"There are no solutions for "<<n<<".\n";
        }
        first= false;
    }
}

No comments: