Problem description
For 1 + 2 + 3 +... The value of the + n
Input format
Input includes an integer n,
The output format
Output one line, including an integer, said 1 + 2 + 3 +... The value of the + n
The sample input
4
Sample output
10
The sample input
100
Note: there are some questions will give more groups of sample input and output to help you better, to solve the problem.
General before submit all these samples need to be test pass, but that doesn't mean that the groups of sample data is correct for your application is completely correct, potential error may still leads to lower your score,
Sample output
5050
The data size and agreed
1 & lt;=n & lt;=1000000000,
Note: please note that the data size,
That directly bear on the idea is to direct the use of a loop to accumulate, however, when the data size is large, this method often lead to "violence" timeout, at this point you need to think of other ways, you can have a try, if you use 1000000000 as your program's input, your program is can run within the time limit stipulated by the prescribed above,
Subject another to note is the size of the answer is not in your language within the scope of the default integer (int), if you use the integer to save the results, will lead to the result error,
If you are using C + + or C and ready to use printf output, is your output format string should be written in % I64d to long long type integer,
This is my code, why give wrong results, with Visual c + + compiler running overtime with blue cup test systems show that the other right?
#include
Int main ()
{
Long long x, f=0, I;
The scanf (" % ld ", & amp; X);
for(i=1; i<=x; I++)
F=f + I;
Printf (" % I64d \ n ", f);
return 0;
}
CodePudding user response:
This is not to say that very clearly in aThe idea of ontology directly is directly using a loop to accumulate , however, when the data size is large, the this method often lead to "violence" timeout , at this point you need to think of other ways, you can give it a try, if you use 1000000000 as your program's input, your program is can run within the time limit stipulated by the prescribed above,
Using gauss formula f=(1 + n)/2 * n
CodePudding user response:
There should be a formula for calculating, temporarily didn't think of and violence can also
#include
Int main (void) {
Unsigned long long n;
The scanf (" % ull ", & amp; n);
Unsigned long long result=0;
While (n & gt;=1) {
The result +=n;
n--;
}
Printf (" % ull ", result);
return 0;
}
CodePudding user response:
Let's use the formula of upstairs, violence is too slow, and I looked at the answer is not quite rightCodePudding user response:
Compile time, error error C2632: 'long' followed by 'long' is illegalCodePudding user response:
Visual c + + too old ~ ~ ~, may not support long long, general ll certainly no problem, what do you think I this ullCodePudding user response:
Int main (){
Long long x, f=0, I;
The scanf (" % ld ", & amp; X);
f=n * (1 + n)/2;
printf (" % I64d \ n, f);
return 0;
}
Use unsigned, it does not matter with long long behind is illegal, is your printf function is given,
CodePudding user response:
Printf how change ah, long long is illegal, but in the blue cup measurement system is correctCodePudding user response:
You with your dev c + + a try, if not vs support for certain, the latest version of mingw GCC must be supported, it mostly compiler version problem, change the printf it happened during the change of library functions, or save it, it is better to write their own an output functionCodePudding user response:
Printf (" % I64d \ n ", f); In front of their own test code is not there?CodePudding user response:
Thank youCodePudding user response:
Printf (" % ull ", result); Ull here should use llu or LLD, ull is only applicable to digital literal suffixesCodePudding user response:
Direct writing on them will timeout, do you know the story, add 1 to 50 he isn't a a combined, but the number of the fore and aft add by half, so as not to exceed the baiCodePudding user response:
#includeInt main ()
{int I, n, s=0;
Printf (" enter n=? \n");
The scanf (" % d ", & amp; n);
for(i=1; i<=n; I++)
S +=I;
Printf (" s=% d ", s);
return 0;
}
CodePudding user response:
This subject take an examination of the trapezoidal area of base plus the base times height divided by two,
#include
Int main ()
{
Unsigned long long n=0, s=0;
Printf (" enter n=? \n");
The scanf (" % I64u ", & amp; n);
Printf (" s=% I64u ", * (1 + n) n/2);
return 0;
}
/*
Enter n=?
1000000000
S=500000000500000000
*/
CodePudding user response:
Can use arithmetic series summation methodCodePudding user response:
Can also use a recursive method, I often use, ha, ha, ha, give you write a exampleCodePudding user response:
Include "stdio.h"Void main () {
Long m=0;
The scanf (" % d ", & amp; M);
Long n=sum (m);
Printf (" and asked for: % d \ n ", m)
}
{int the sum (n)
If (n==0) {
return 0;
} else {
Return the sum (n - 1) + n.
}
}
CodePudding user response:
Almost more than a year didn't write c, general principle is as followsCodePudding user response: