Arithmetic progression problems

Source: Internet
Author: User

Title Description Description

Given n (n>=1), A recursive method is used to calculate 1+2+3+4+...+ (n-1) +n

input/output format input/output Input Format:
A row, a number n
output Format:row, the result input and Output sample sample Input/output sample Test point # # Input Sample:3 Sample output: 6 Ideas: Run the program, when T=5, the output: s=15, its recursive call execution process is: (set t=3) recursive call procedure, is essentially the process of constantly calling a procedure or function, due to recursive invocation, all the subroutine variables (local variables, variable parameters, etc.), the address within the computer has a special management method-Stack (advanced post-out) to manage, once the recursive call ends, The computer begins to return the values of each subroutine variable based on the address stored in the stack and operates accordingly. the code ① is as follows (conforms to test instructions, with recursion):
1#include <stdio.h>2 intFacintN)3 {4     if(n==0)return 0;5     Else 6     {7         return(FAC (n1) +n);//Recursive8     }9 }Ten intMain () One { A     intN; -scanf"%d",&n); -printf"%d\n", FAC (n));//calling Functions the     return 0; -}

The code ② as follows (self-written, useless recursion):

1#include <stdio.h>2 intMain ()3 {4     intN;5scanf"%d",&n);6printf"%d\n",((1+N) *n)/2);7     return 0;8}

Arithmetic progression problems

Related Article

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.