日期:2014-05-20  浏览次数:20714 次

一道程序题!!
比如有一个数99,那么就打印连续数相加等于99的数
99=[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
99=[7, 8, 9, 10, 11, 12, 13, 14, 15]
99=[14, 15, 16, 17, 18, 19]
99=[32, 33, 34]
99=[49, 50]

写出实现代码!


------解决方案--------------------
不是算法 不知道是这样不
Java code

public class AddNum {
    public static void main(String args[]) {
        int num = 99;
        int mid = num / 2 + 1;
        int sum = 0;
        int start, end;
        for (int i = 0; i < mid + 1; i++) {
            start = i;
            for (int j = i + 1; j < mid + 1; j++) {
                sum = sum + j;
                if (sum == num) {
                    end = j;
                    System.out.println(start + 1 + "-----" + end);
                }
            }
            sum = 0;
        }

    }
}

------解决方案--------------------
探讨
Java code

public void printNums(int totalNum){
int a; //等差数列首项
for(n=2;;n++){
if( (totalNum - n*(n-1)/2)%n !=0 )
continue;
else{
a = (totalNum ……