Beg 1+2+3+...+n

Source: Internet
Author: User

Title Description:

Ask for 1+2+3+...+n, the request cannot use multiplication method, for, while, if, else, switch, case and other keywords and conditional judgment statement (A? B:C).

Input:

The input may contain multiple test samples.
For each test case, the input is an integer n (1<= n<=100000).

Output:

corresponding to each test case,
The value of the output 1+2+3+...+n.

Sample Input:

3
5
Sample output:
6
15
AC Code:


#include <stdio.h>
 
int Add (int n, int *p) {
    n && Add (n-1, p);
 
    return (*p + = n);
}
 
int main () {
    int n;
    while (scanf ("%d", &n)! = EOF) {
        int sum = 0;
        Add (n, &sum);
        printf ("%d\n", sum);
    }
 
    return 0;
}
 
/**************************************************************
    problem:1506
    user:wusuopubupt
    Language:c
    result:accepted
    time:630 Ms
    memory:1212 KB
************************************** **************************/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.