ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1068. Sum

C++ Answer
Posted by navi1893 21 Jun 2012 05:38
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int a, sum = 0;
    cin >> a;

    if( a > 0 )
    {
        for ( int i = 1; i <= a; i++)
        sum += i;
    }

    else
    {
        for (int i = 1; i <= abs(a); i++)
        sum += i;
        sum = 1 - sum;
    }

    cout << sum;
    return 0;
}
Re: C++ Answer
Posted by Muhammad Ayub khan 19 Aug 2012 08:02
hell whats problm with mine .

#include <iostream>


using namespace std;

int main()
{
    int A=0;
    double Ans=0;
    cin>>A;
    if(A<=10000)
    {
        if(A>=1)
        Ans=A*(A+1)/2;
        if(A<0)
        Ans=(A*-1)*(A-1)/2+1;
        cout<<Ans;
    }
    return 0;
}
Re: C++ Answer
Posted by Muhammad Ayub khan 19 Aug 2012 08:04
What about less then 10000?
Re: C++ Answer
Posted by ShT3ch 24 Aug 2012 00:46
WA 9?
Floating types can return answer like 123e+10. So you have to do "int Ans" before got answ.

In add:
Just use formula s(a1,an)=(a1,an)/2*n like that

       int(((1+n)/2)*(abs(1-n)+1)));

Edited by author 24.08.2012 00:47
Re: C++ Answer
Posted by yyjazsf 2 Oct 2012 20:06
hello, whats problm with mine .
#include <iostream>
bool flag=true;//the number more than 0
int num=0;
bool GetNum()
{
    scanf("%d",&num);
    return num>=0;
}
int GetResoults()
{
    //Sn=(a1+an)n/2
    if (flag)//>0
    {
        return (1+num)*num/2;
    }
    else//<0
    {
        return (num+1)*((-num)+2)/2;
    }
}

int main()
{
    flag=GetNum();
    printf("%d\n",GetResoults());
    return 0;
}
Re: C++ Answer
Posted by yyjazsf 2 Oct 2012 20:07
-10000-10000