求两个整数区间的累加和(递归方法)

int main()
{
    int n,m;
    int k=0;
    scanf("%d %d",&n,&m);
    k=sum(n,m);
    printf("%d\n",k);
    return 0;
}
int sum(int n,int m){
    if(n<m){
        return sum(n,m-1)+m;
    }else{
        return n;

    }
}
时间: 2024-08-27 06:10:00

求两个整数区间的累加和(递归方法)的相关文章

求两个整数的最大公约数

<C和指针>第7章第2道编程题: 两个整型值M和N(M.N均大于0)的最大公约数可以按照下面的方法计算: 请编写一个名叫gcd的函数,它接受两个整型参数,并返回这两个数的最大公约数.如果这两个参数中的任何一个不大于零,函数返回零. 1 /* 2 ** 求两个整数的最大公约数 3 */ 4 5 #include <stdio.h> 6 7 int gcd( int M, int N ); 8 9 int 10 main() 11 { 12 int m, n; 13 scanf( &q

欧几里得算法求两个整数的最大公因数

unsigned int Gcd (unsigned int m,unsigned int n){ unsigned int rem; while(n>0){ rem = m % n; m = n; n = rem; } return m; } 对于m<n的情况,第一次循环m,n会交换 算法的时间复杂度计算 时间复杂度 logn 若M > N,则第一次循环交换M和N. 若想分析其时间复杂度,则要求循环次数,即生成余数的次数. 可以证明: 当M > N, 则M % N < M

12_1求两个整数中的较小值,要求不能使用比较运算符, if-else, a&gt;b?a:b, while for

转载请注明出处:http://www.cnblogs.com/wuzetiandaren/p/4253932.html  声明:现大部分文章为寻找问题时在网上相互转载,此博是为自己做个记录记录,方便自己也方便有类似问题的朋友,本文的思想也许有所借鉴,但源码均为本人实现,如有侵权,请发邮件表明文章和原出处地址,我一定在文章中注明.谢谢. 题目:求两个整数中的较小值,要求不能使用比较运算符, if-else, a>b?a:b, while for, 内嵌汇编递归第三方函数. 在网上看到一些网友给出了

求两个整数的最大公约数和最小公倍数

//求最大公约数是用辗转相除法,最小公倍数是根据公式 m,n 的 最大公约数* m,n最小公倍数 = m*n 来计算 #include<stdio.h> //将两个整数升序排列 void ascNum(int *p1,int *p2) { int temp; if(*p1 > *p2) { temp = *p2; *p2 = *p1; *p1 = temp; } } //求两个整数的最大公约数 辗转相除法 int getAppr(int a, int b) { int c; ascNum

002:求两个整数的最大公约数和最小公倍数

求最大公约数可采用辗转相除法,其流程如图所示. 最小公倍数就是两个整数的乘积除以其最大公约数. 1 #include <stdio.h> 2 3 int main() 4 { 5 unsigned long a, b, c=0; //两个整数和临时变量 6 unsigned long lcm=0, gcd=0; //最小公倍数和最大公约数 7 8 while( 1 ) 9 { 10 printf("Please input two positive integers(spacebar

【c语言】求两个整数中的较大者

// 求两个整数中的较大者 #include <stdio.h> int max( int a, int b ) { int temp; if( a > b ) { temp = a; } else { temp = b; } return temp; } int main() { int a,b; printf("请输入要比较的两个数:\n"); scanf("%d %d",&a,&b); printf("大数是:%d\

求两个整数的乘积

// 求两个整数的乘积#include<stdio.h>int product (int,int);int main(void) { int x,y,s; scanf("%d %d",&x,&y); s = product(x,y); printf("The mul is:%d",s); return 0;}int product(int a,int b){ int mul; mul = a*b; return mul;} 原文地址:ht

C++求两个整数的最大公约数和最小公倍数

最小公倍数=两个整数的成绩 / 最大公约数 求最大公约数的方法: (1)辗转相除法 1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int a,b,tmp,m; 6 cin>>a>>b; 7 m=a*b; 8 if(a<b) 9 { 10 tmp=b; 11 b=a; 12 a=tmp; 13 } 14 while(b!=0) 15 { 16 tmp=a%b; 17 a=b; 18 b

C++程序设计实践指导1.5求两个整数集合并集改写要求实现

改写要求1:改写为单链表结构可以对任意长度整数集合求并集 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int data; LinkNode* next; }; class SET { public: struct LinkNode* creat(int x[],int len); struct LinkNode* copy(LinkNode* aHead); int no