how can i solve the time limit exceeded in my code like this : C. Sum of Range time limit per test 0.5 seconds memory limit per test 256 megabytes input standard input output standard output
Given 2 numbers A and B. Print three lines that contain the following respectively:
summation of all numbers between A and B (inclusive).
summation of even numbers between A and B (inclusive).
summation of odd numbers between A and B (inclusive).
Input
Only one line contains two numbers A and B (1 ≤ A, B ≤ 109). Output
Print the answer required above.
Input
4 6 output 15 10 5
this my code
#include <iostream>
using namespace std;
void range(long long a , long long b)
{
cin>>a>>b;
long long sum=0,s=0,ss=0;
for(int i=min(a,b);i<=max(a,b);i++)
{
sum=sum+i;
}
cout<<sum<<endl;
for(int i=min(a,b);i<=max(a,b);i++)
{
if(i%2==0)
{
s=s+i;
}
}
cout<<s<<endl;
for(int i=min(a,b);i<=max(a,b);i++)
{
if(i%2!=0)
{
ss=ss+i;
}
}
cout<<ss<<endl;
}
int main()
{
long long a,b;
range(a,b);
}
Aucun commentaire:
Enregistrer un commentaire