java 简单的加法 递归 从A加到B

public class Main {//	设置保存和的变量static int sum = 0;public static void main(String[] args) {int begin = 1, end = 10;//		调用方法一	sum =0;sum = add(begin, end);System.out.println("===>>> 从 " + begin + " 开始加到 " + end + " 的结果是" + sum);System.out.println();//		调用方法二sum =0;addB(begin, end);System.out.println("===>>> 从 " + begin + " 开始加到 " + end + " 的结果是" + sum);System.out.println();}//	方法一(从尾处开始加)public static int add(int begin, int end) {if (begin <= end) {sum = sum + end;System.out.println("当前begin为 " + begin + " 当前end为 " + end + " 当前sum为 " + sum);add(begin, end-1);}return sum;}//	方法二(首尾同时加)public static void addB(int begin, int end) {if (begin <= end) {sum = sum + begin + end;System.out.println("当前begin为 " + begin + " 当前end为 " + end + " 当前sum为 " + sum);addB(begin + 1, end - 1);}}
}

结果:

当前begin为 1 当前end为 10 当前sum为 10
当前begin为 1 当前end为 9 当前sum为 19
当前begin为 1 当前end为 8 当前sum为 27
当前begin为 1 当前end为 7 当前sum为 34
当前begin为 1 当前end为 6 当前sum为 40
当前begin为 1 当前end为 5 当前sum为 45
当前begin为 1 当前end为 4 当前sum为 49
当前begin为 1 当前end为 3 当前sum为 52
当前begin为 1 当前end为 2 当前sum为 54
当前begin为 1 当前end为 1 当前sum为 55
===>>> 从 1 开始加到 10 的结果是55


当前begin为 1 当前end为 10 当前sum为 11
当前begin为 2 当前end为 9 当前sum为 22
当前begin为 3 当前end为 8 当前sum为 33
当前begin为 4 当前end为 7 当前sum为 44
当前begin为 5 当前end为 6 当前sum为 55
===>>> 从 1 开始加到 10 的结果是55

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545361.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java LocalDateTime类| 带示例的compareTo()方法

LocalDateTime类compareTo()方法 (LocalDateTime Class compareTo() method) compareTo() method is available in java.time package. compareTo()方法在java.time包中可用。 compareTo() method is used to compare this date-time object to the given date-time object. co…

附录:更多集合操作命令

移除并返回集合中的一个随机元素 语法:spop key [count] 示例: 127.0.0.1:6379> smembers myset 1) "v1" 2) "v2" 127.0.0.1:6379> spop myset 1 1) "v2" 127.0.0.1:6379> smembers myset 1) "v1"随机返回集合中指定数量…

第一二章(PTA复习)

第一二章因为3默认是整形&#xff0c;整形长度大于short型&#xff0c;如果让short型 short型 int型&#xff0c;可能会溢出&#xff0c;所以编译报错 例如&#xff1a; 答案&#xff1a;D switch 语句中的变量类型可以是&#xff1a; byte、short、int 或者 char。从 Java …

附录:更多列表操作命令

在某值之前/之后添加某个元素 语法&#xff1a;linsert key before|after pivot value 示例&#xff1a; 127.0.0.1:6379> linsert list3 before b A (integer) 4 127.0.0.1:6379> lrange list3 0 -1 "a" "A" "b" "c"根据下标…

code craft_Craft.io调度中使用的重要术语

code craft1) Arrival Time (AT) 1)到达时间(AT) The time when the process arrives into the running state is called as the Arrival time of the process. In simple words, the time at which any process enter the CPU is known as the arrival time. 流程到达运行状态…

英语笔记:写作:Free admissionsto museums

Free admissionsto museums 免费开放博物馆 Recently, more and more museums areopen to the public free of charge so that they can attract more visitors. However,it has also brought some unexpected problems. Should we carry on the practice? People’sviews va…

ExtJS和AngularJS比较

原文地址&#xff1a;http://www.techferry.com/articles/ExtJS-vs-AngularJS.html ExtJS和AngularJS比较.pdf转载于:https://www.cnblogs.com/animal/p/4267109.html

异常类型及处理

异常类型及处理检查型异常与非检查型异常&#xff1a; 非检查型异常是指机器运行之前永远都不能自动检测出来的异常&#xff0c;例如idea不会报红的但有可能发生异常的代码。error类和RunTime类及其子类 异常类型及处理 finally使用

c ++查找字符串_C ++朋友功能| 查找输出程序| 套装2

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>using namespace std;class Sample1 {int A, B;friend class Sample2;};class Sample2 {int X, Y;public:Sample2(){X 5;Y 5;}void fun(){Sample1 S;S.A 10 * X;S.B 20 * Y;cout << S.A <<…

英语笔记:词组句子:0806

Aim at doing Aim to 动词原形 Vary from … to … 因……而已 Range from … to … 在……范围内波动、变化 In prospect 即将可能发生的 In place 在正确位置&#xff1b;准备妥当 In favor of 支持&#xff0c;赞同 People in control 学校的主管人员 Keep in to…

附录:更多有序集合操作命令

查询有序集合的总个数 语法:zcard key 示例: 127.0.0.1:6379> zcard zset1 (integer) 4查询 score 区间内的元素个数 语法:zcount key min max 示例: 127.0.0.1:6379> zcount zset1 0 10 (integer) 4累加元素的 score 值 语法:zincrby key increment member 示…

侧边菜单栏 android-menudrawer

这是github上的一款开源项目&#xff0c;类似于人人网可滑动的侧边菜单栏-----android-menudrawer。使用方法也很简单。1.将下载的包解压放入你的工作目录下2.利用Eclipse通过new-->project...->Android Project from Existing Code&#xff0c;找到项目目录&#xff0c;…

软件工程质量管理体系要求_软件质量管理| 软件工程

软件工程质量管理体系要求软件质量管理体系 (Software quality management system) A quality management system frequently mentioned as a standard system is a principal scheme used by organizations to ensure that the products they develop have the desired qualit…

英语笔记:写作:Recreational activities

Recreational activities 娱乐活动 Nowadays, in an era of information and technology, abundant recreational activities areavailable for people to release their stain and stress, ranging from sports tosurfing the internet. There is a hot debate going on argu…

Redis 如何实现限流功能?

“限流”这种事在生活中很常见,比如逢年过节时景点的限流,还有工作日的车辆单双号限流等,有人可能会问为什么要限流?我既然买了车子你还不让我上路开?还有我倒景点买了门票,景点不是能赚更多的钱吗?为什么要限流呢? 其实限流的主要目的就是为了保证整个系统的正常运行…

第三四五章(PTA复习)

第三四五章类的构造与继承异常线程类的构造与继承 加 final 唯一可以说明的是该类不可被继承&#xff0c;另外和其它类是一样的 abstract类是抽象类&#xff0c;必须要被继承才能实现&#xff0c;而final修饰的类不能被继承 异常 由于未捕获到异常&#xff0c;所以执行完final…

switch(封装)

#迭代器class switch(object):def __init__(self,value):self.valuevalueself.fallFalse#迭代器方法def __iter__(self):yield self.matchraise StopIterationdef match(self,*args):if self.fall or not args:return Trueelif self.value in args:self.fallTruereturn Trueels…

stl max函数_C ++ STL中带有示例的array :: max_size()函数

stl max函数C STL array :: max_size()函数 (C STL array::max_size() function) max_size() function is a library function of array and it is used to get maximum size of an array. It returns the total number of elements that an array can hold. max_size()函数是…

Redis 属于单线程还是多线程?不同的版本有什么区别?

Redis 是普及率最高的技术之一&#xff0c;同时也是面试中必问的一个技术模块&#xff0c;所以从今天开始我们将从最热门的 Redis 面试题入手&#xff0c;更加深入的学习和了解一下 Redis。 我们本文的面试题是 Redis 属于单线程还是多线程&#xff1f; 典型回答 本文的问题…

英语笔记:词组句子:0906

income 收入 mostly 大部门 particularly 尤其、特别 larger drops 更大幅度的下降 thanks to 由于 liable to 易于、可能 in terms of 在……方面 in view of 鉴于、考虑到 over time 随着时间过去 against time 争分夺秒 on time 准时 behind time 误期 widening…