Description:
Evaluate 1 + 2 + 3 +... + N. It is required that the keyword and condition judgment statement (? B: C ).
Input:
The input may contain multiple test examples.
For each test case, the input is an integer N (1 <=n <= 100000 ).
Output:
Corresponding to each test case,
Output 1 + 2 + 3 +... + N value.
Sample input:
3
5
Sample output:
6
15
Recommendation index :※※
Source: http://ac.jobdu.com/problem.php? PID = 1, 1506
# Include <iostream> # include <stdlib. h> # include <stdio. h> using namespace STD; int F (const int N); int f_end (const int N); int (* choose []) (INT) = {f_end, f }; int f_end (const int N) {return 0;} int F (const int N) {return choose [n> 1] (n-1) + N;} int main () {int N; while (scanf ("% d", & N )! = EOF) {printf ("% d \ n", F (N) ;}return 0 ;}
Introduction to function pointer array: http://blog.csdn.net/zhu_liangwei/article/details/8194812
@ Wqooops http://blog.csdn.net/shiren_bod/article/details/6703467 avoids the function pointer operation, using N itself as the end indicator.
# Include <iostream> # include <stdlib. h> # include <stdio. h> using namespace STD; int F (const int N, Int & sum) {N & F (n-1, sum); Return sum + = N;} int main () {int N; while (scanf ("% d", & N )! = EOF) {int sum = 0; printf ("% d \ n", F (n, sum);} return 0 ;}