一般的方法

#include<stdio.h>

int sum(int n)
{
if(n==1)
return 1;
else
return n+sum(n-1);
}

int main(void)
{
int n;
while(scanf("%d",&n)==1)
{
printf("%d\n",sum(n));
}
return 0;
}

如果加上这些条件设置

题目:求1+2+…+n,

要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
 

#include<stdio.h>

int digui(int sum,int n)
{
n&&digui(sum+n,n-1);
}

int main(void)
{
int n;
while(scanf("%d",&n)==1)
{
printf("%d\n",digui(0,n));
}
return 0;
}

很不错!哈哈哈!多多感悟!

递归求和1到n的更多相关文章

  1. Java之递归求和的两张方法

    方法一: package com.smbea.demo; public class Student { private int sum = 0; /** * 递归求和 * @param num */ ...

  2. 51Nod 1013 3的幂的和 快速幂 | 乘法逆元 | 递归求和公式

    1.乘法逆元 直接使用等比数列求和公式,注意使用乘法逆元 ---严谨,失细节毁所有 #include "bits/stdc++.h" using namespace std; #d ...

  3. python 递归求和

    例子: 1 mylist = [1,2,3,4,5] 2 def func(var): 3 if var == []: 4 print('列表为空') 5 return 0 6 return var[ ...

  4. python 列表的递归求和

    def list_sum(num_List): : ] else: ] + list_sum(num_List[:]) print(list_sum([, , , , ]))

  5. 基于visual Studio2013解决C语言竞赛题之0613递归求积

     题目

  6. 22_IO_第22天(File、递归)_讲义

    今日内容介绍 1.File 2.递归 xmind:下载地址: 链接:https://pan.baidu.com/s/1Eaj9yP5i0x4PiJsZA4StQg 密码:845a 01IO技术概述 * ...

  7. 用递归的方法求一个数组的前n项和

    用递归的方法求一个数组的前n项和 public class Demo1 { /* * 用递归的方法求一个数组的前n项和 */ public static void main(String[] args ...

  8. 13、IO (File、递归)

    File File类的概述和作用 * A:File类的概述和作用 * a: File的概念 * File类是文件和目录路径名的抽象表示形式 * Java中把文件或者目录(文件夹)都封装成File对象 ...

  9. 21_java之File对象和递归遍历

    01IO技术概述 * A:IO技术概述 * a: Output * 把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作 * b: Input * 把持久设备上的数据读取到内存中的这 ...

随机推荐

  1. adb not responding. if you'd like to

    在安装genymotion后启动工程报此错误. 解决方案:把其他虚拟机删掉,然后用genymotion新建一个,启动工程OK.

  2. PAT (Advanced Level) 1053. Path of Equal Weight (30)

    简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  3. thinkphp 5.0 模块设计

    模块设计 5.0版本对模块的功能做了灵活设计,默认采用多模块的架构,并且支持单一模块设计,所有模块的命名空间均以app作为根命名空间(可配置更改). 目录结构 标准的应用和模块目录结构如下: ├─ap ...

  4. Transport layer and Network layer

    http://stackoverflow.com/questions/13333794/networking-difference-between-transport-layer-and-networ ...

  5. SQLSERVER设置行号

    select row_number()over(order by columnname)as rownum,* from tablename 按照columnname列进行排列

  6. Codeforces#371 Div2

    这是一场非常需要总结的比赛,交了3题,最后终测的时候3题全部没过,一下掉到了绿名,2333 Problem A 题意:给定区间[l1,r1],[l2,r2],然后给定一个整数k,求区间当中相交的元素, ...

  7. STL优先队列的使用

    STL中有一个优先队列的容器可以使用. [头文件] queue 队列容器 vector 向量容器 [操作] 优先级队列支持的操作 q.empty()         如果队列为空,则返回true,否则 ...

  8. js MD5加密后的字符串

    js MD5加密后的字符串 <script language="JavaScript"> /************************************** ...

  9. vector与ArrayList、hashmap与hashtable区别

    一.vector与ArrayList区别     首先要说明的是vector和arraylist都是list的实现类,都是代表链表的数据结构.     java.util.Vector;  类中 pa ...

  10. libconfig第一篇———使用指南

    官网:http://www.hyperrealm.com/libconfig/ 1 libconfig是什么? Libconfig是一个结构化的配置文件库,它可以定义一些配置文件,例如test.cfg ...