标签:out 题目 turn code color sys seq 打印 ++
题目:输入一个正数s,打印出所有和为s的连续正整数序列(至少含有两个数)。例如输入15,由于1+2+3+4+5=4=5+6=7+8=15,所以结果打印出3个连续序列1-5、4-6、7-8。
// import java.util.*; public class SumSequence{ public void FindContinuousSequence(int sum){ if(sum<3) return; int small=1; int big=2; int middle = (1+sum)/2; int temp = small+big; while(small<middle){ if(temp == sum){ PrintContinuousSequence(small,big); } while(small< middle&&temp>sum){ temp -= small; small++; if(temp == sum){ PrintContinuousSequence(small,big); } } big++; temp+=big; } } public void PrintContinuousSequence(int small,int big){ for(int i=small;i<=big;i++){ System.out.print(i); } System.out.println(); } public static void main(String[] args){ SumSequence s = new SumSequence(); s.FindContinuousSequence(15); } }
标签:out 题目 turn code color sys seq 打印 ++
原文地址:https://www.cnblogs.com/yingpu/p/9256568.html