Friday, July 28, 2017

UVa problem solution 294 - Divisors

problem link:


Discuss: find the divisor of all number then compare it with previous if it is greater then  previous one then replace with maximum and store the number


try yourself before see the code
#define  ll long long
#include<bits/stdc++.h>
using namespace std;
ll divisor(ll n)
{
  ll i,cnt=0;
    for(i=1;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            cnt++;
            if(n/i!=i)
                cnt++;
        }
    }
    return cnt;
}
int main()
{

    ll t,i,j,n,m,maxx,number;
    cin >>t;
    while(t--)
    {
        cin >>n>>m;
        maxx=0;
        if(n==1&&m==0)
     {
          printf("Between 1 and 1, 0 has a maximum of 2 divisors.\n");
          continue;
     }
        for(i=n;i<=m;i++)
        {
            j=divisor(i);
            if(j>maxx)
            {
                number=i;
                maxx=j;
            }
        }
        printf("Between %lld and %lld, %lld has a maximum of %lld divisors.\n",n,m,number,maxx);

    }
    return 0;
}

No comments:

Post a Comment