從制造到創造
          軟件工程師成長之路
          posts - 292,  comments - 96,  trackbacks - 0


          import java.util.ArrayList;
          import java.util.List;

          public class Sum {
              
          public static void main(String[] args) {

                  
          int[] temp = new int[1000];
                  
          for (int i = 1; i < 1001; i++) {
                      temp[i 
          - 1= i;
                  }

                  
          int total = 0;// 存儲總數
                  int sum = 0// 和
                  int k = 0;
                  List list 
          = null// 存儲每次新循環時的數據
                  int size = 0;
                  
          int num = 0;
                  
          for (int j = 0; j < 1000; j++) {
                      k 
          = j;
                      sum 
          = 0// sum 每次循環都要重置
                      list = new ArrayList(); // list 每次循環都要清空
                      do {
                          sum 
          += temp[k];

                          list.add(temp[k]);
                          
          if (sum == 1000) {// 洽好等于1000就打印
                              total++;
                              System.out.println(
          "Group" + total + ":  ");
                              
          if (list != null && list.size() > 0) {
                                  size 
          = list.size();
                              }
                              
          for (int x = 0; x < size; x++) {
                                  num 
          = x + 1;
                                  System.out.println(
          "    " + num + " : " + list.get(x));
                              }
                          }

                          
          if (k < 999) {
                              k
          ++;
                          }
                      } 
          while (sum < 1001);// 超過1000就退出
                  }

                  System.out.println(
          "  total :  " + total);
              }
          }


          輸出結果:

          Group1:  
              
          1 : 28
              
          2 : 29
              
          3 : 30
              
          4 : 31
              
          5 : 32
              
          6 : 33
              
          7 : 34
              
          8 : 35
              
          9 : 36
              
          10 : 37
              
          11 : 38
              
          12 : 39
              
          13 : 40
              
          14 : 41
              
          15 : 42
              
          16 : 43
              
          17 : 44
              
          18 : 45
              
          19 : 46
              
          20 : 47
              
          21 : 48
              
          22 : 49
              
          23 : 50
              
          24 : 51
              
          25 : 52
          Group2:  
              
          1 : 55
              
          2 : 56
              
          3 : 57
              
          4 : 58
              
          5 : 59
              
          6 : 60
              
          7 : 61
              
          8 : 62
              
          9 : 63
              
          10 : 64
              
          11 : 65
              
          12 : 66
              
          13 : 67
              
          14 : 68
              
          15 : 69
              
          16 : 70
          Group3:  
              
          1 : 198
              
          2 : 199
              
          3 : 200
              
          4 : 201
              
          5 : 202
          Group4:  
              
          1 : 1000
            total :  
          4
          posted on 2007-07-20 14:49 CoderDream 閱讀(1495) 評論(0)  編輯  收藏 所屬分類: 經驗點滴

          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(9)

          我參與的團隊

          隨筆分類(245)

          隨筆檔案(239)

          文章分類(3)

          文章檔案(3)

          收藏夾(576)

          友情鏈接

          搜索

          •  

          積分與排名

          • 積分 - 458376
          • 排名 - 114

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 汶上县| 沂水县| 玉溪市| 紫云| 开江县| 河北区| 香港| 丹东市| 海盐县| 祁东县| 冷水江市| 通许县| 沂水县| 承德市| 晋州市| 冕宁县| 荔浦县| 德兴市| 汉中市| 巨野县| 壶关县| 廉江市| 大化| 临海市| 巴塘县| 洞头县| 禄丰县| 东乌珠穆沁旗| 台北市| 博罗县| 泸州市| 温宿县| 新宾| 乌兰察布市| 美姑县| 舞钢市| 稷山县| 凤翔县| 商城县| 东丰县| 罗甸县|