Answer the question
In order to leave comments, you need to log in
How to reduce program execution time?
Is there a code how to reduce the program execution time so that it is less than one second?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int S=0,Smax=0,n,i,k;
cin>>n;
for(int i=1;i <= n;i++)
{
S=0;
for(int j=1;j <= n;j++)
{
if(i%j == 0)
S=S+j;
if(S > Smax){Smax=S;k=i;}
}
}
cout<<k;
return 0;
}
Answer the question
In order to leave comments, you need to log in
Rewrite the algorithm. And to help you with this, first I would like to understand what this algorithm does. At first glance, this is a search for a number from 1 to n, the sum of divisors of which is maximum, right?
At first glance, why not take it out
if(S > Smax){Smax=S;k=i;}
of the second cycle? And that turns out at each pass there is a check.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question