Home > Back-end >  Blue cup of C/C
Blue cup of C/C

Time:10-19

Consult bosses, this topic
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 a
The 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 right

CodePudding user response:

Compile time, error error C2632: 'long' followed by 'long' is illegal

CodePudding user response:

Visual c + + too old ~ ~ ~, may not support long long, general ll certainly no problem, what do you think I this ull

CodePudding 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 correct

CodePudding 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 function

CodePudding user response:

Printf (" % I64d \ n ", f); In front of their own test code is not there?

CodePudding user response:

Thank you

CodePudding user response:

Printf (" % ull ", result); Ull here should use llu or LLD, ull is only applicable to digital literal suffixes

CodePudding 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 bai

CodePudding user response:

#include

Int 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 method

CodePudding user response:

Can also use a recursive method, I often use, ha, ha, ha, give you write a example

CodePudding 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 follows

CodePudding user response:

refer to the 18th floor heavy black prince replies:
almost didn't write c more than a year, the general principle is that


Iteration timeout, recursive absolute timeout, and length of type int is insufficient, it is best to use the formula to calculate directly

CodePudding user response:

By the way, is not enough long, long long, unsigned long long is enough

CodePudding user response:

#include
using namespace std;
Int main () {
int a;
cin> a;
cout<(a + 1) * a/2 & lt; }

CodePudding user response:

With long long have a try, if not try the other
  • Related