有如下号码:002 006 012 016 022 025 028 029 056 058
059 066 069 089 113 114 116 117 123 124
127 129 133 135 137 138 144 145 148 149
156 157 166 168 169 177 178 223 224 226
227 233 235 238 244 245 248 249 256 257
259 266 267 268 269 277 278 334 336 337
339 344 345 347 356 357 359 366 367 368
369 377 378 389 399 446 447 456 457 458
459 466 467 468 469 477 478 479 567 568
579 667 677 678 679 689 779 789 799001 002 005 008 011 012 013 014 015 018
019 023 025 027 028 035 037 038 045 048
055 058 088 123 124 125 126 128 134 135
136 137 138 139 145 146 148 156 157 158
159 167 168 169 179 234 236 237 238 245
246 248 256 258 268 279 289 335 345 347
348 355 357 367 368 378 379 448 456 458
468 478 479 488 568 578 668 688
001 005 008 011 013 014 018 019 023 026
027 035 038 039 055 056 057 059 078 088
112 115 116 117 118 119 122 123 127 128
134 135 136 137 138 139 145 146 147 148
149 155 156 157 166 177 188 199 224 225
228 229 236 237 238 244 245 246 249 255
256 257 267 269 288 299 345 348 356 357
358 359 368 456 457 467 468 478 489 558
559 567 569 578 588 599 678 689 889 899
将以上号码出现次数统计出来,
显示效果与下面一致:
共有3个号码群进行统计,总注数为267出现1次的号码(94注):
006 015 016 022 026 029 037 039 045 048
057 066 069 078 089 112 113 114 115 118
119 122 125 126 129 133 144 147 155 158
159 167 178 179 188 199 223 225 226 227
228 229 233 234 235 255 258 259 266 277
278 279 288 289 299 334 335 336 337 339
344 355 358 366 369 377 379 389 399 446
447 448 459 466 469 477 488 489 558 559
569 579 588 599 667 668 677 679 688 779
789 799 889 899出现2次的号码(61注):
001 002 005 008 011 012 013 014 018 019
023 025 027 028 035 038 055 056 058 059
088 116 117 124 127 128 134 136 139 146
149 166 168 169 177 224 236 237 244 246
248 249 257 267 268 269 347 348 356 359
367 378 457 458 467 479 567 568 578 678
689出现3次的号码(17注):
123 135 137 138 145 148 156 157 238 245
256 345 357 368 456 468 478

解决方案 »

  1.   

    用HashMap,号码为键,次数为值
    一个循环走完,次数全统计出来了再遍历该HashMap,把次数相同的丢到同一个数组去
      

  2.   


    import java.io.*;
    import java.util.*;public class Count 
    {
    public static void main(String[] args) throws IOException
    {
    ArrayList<Integer> objectList = new ArrayList<Integer>();
    ArrayList<Integer> numList = new ArrayList<Integer>();
    for (int i = 0; i < args.length; i++)
    {
    Scanner in = new Scanner(new FileInputStream(args[i]));
    while (in.hasNextInt())
    {
    Integer t = new Integer(in.nextInt());
    int p = objectList.indexOf(t);
    if (p == -1)
    {
    objectList.add(t);
    numList.add(new Integer(1));
    }
    else
    {
    numList.set(p, new Integer(numList.get(p).intValue() + 1));
    }
    }
    in.close();
    }

    int num = 0;
    for (int i = 0; i < numList.size(); i++)
    {
    num += numList.get(i).intValue();
    }
    System.out.println("共有" + args.length + "个号码群进行统计,总注数为" + num);
    System.out.println();

    int max = numList.get(0).intValue();
    for (int i = 0; i < numList.size(); i++)
    {
    if (numList.get(i).intValue() > max)
    {
    max = numList.get(i).intValue();
    }
    }

    int[] nums = new int[max];
    for (int i = 0; i < numList.size(); i++)
    {
    int t = numList.get(i).intValue();
    nums[t - 1]++;
    }

    for (int i = 0; i < max; i++)
    {
    System.out.println("出现" + (i + 1) + "次的号码(" + nums[i] + "注):");
    int t = 0;
    ArrayList<Integer> tmp = new ArrayList<Integer>();
    for (int j = 0; j < numList.size(); j++)
    {
    if (numList.get(j).intValue() == (i + 1))
    {
    tmp.add(objectList.get(j));
    }
    }
    Collections.sort(tmp);

    for (int j = 0; j < tmp.size(); j++)
    {
    System.out.printf("%03d ", tmp.get(j));
    t++;
    if (t % 10 == 0)
    {
    System.out.println();
    }
    }
    System.out.println();
    System.out.println();
    }
    }
    }
      

  3.   

    自动罗列出所有的次数和对应的数字。(不限于只出现1或者2或者3次)/*有如下号码:002 006 012 016 022 025 028 029 056 058
    059 066 069 089 113 114 116 117 123 124
    127 129 133 135 137 138 144 145 148 149
    156 157 166 168 169 177 178 223 224 226
    227 233 235 238 244 245 248 249 256 257
    259 266 267 268 269 277 278 334 336 337
    339 344 345 347 356 357 359 366 367 368
    369 377 378 389 399 446 447 456 457 458
    459 466 467 468 469 477 478 479 567 568
    579 667 677 678 679 689 779 789 799001 002 005 008 011 012 013 014 015 018
    019 023 025 027 028 035 037 038 045 048
    055 058 088 123 124 125 126 128 134 135
    136 137 138 139 145 146 148 156 157 158
    159 167 168 169 179 234 236 237 238 245
    246 248 256 258 268 279 289 335 345 347
    348 355 357 367 368 378 379 448 456 458
    468 478 479 488 568 578 668 688
    001 005 008 011 013 014 018 019 023 026
    027 035 038 039 055 056 057 059 078 088
    112 115 116 117 118 119 122 123 127 128
    134 135 136 137 138 139 145 146 147 148
    149 155 156 157 166 177 188 199 224 225
    228 229 236 237 238 244 245 246 249 255
    256 257 267 269 288 299 345 348 356 357
    358 359 368 456 457 467 468 478 489 558
    559 567 569 578 588 599 678 689 889 899
    将以上号码出现次数统计出来,
    显示效果与下面一致:
    共有3个号码群进行统计,总注数为267出现1次的号码(94注):
    006 015 016 022 026 029 037 039 045 048
    057 066 069 078 089 112 113 114 115 118
    119 122 125 126 129 133 144 147 155 158
    159 167 178 179 188 199 223 225 226 227
    228 229 233 234 235 255 258 259 266 277
    278 279 288 289 299 334 335 336 337 339
    344 355 358 366 369 377 379 389 399 446
    447 448 459 466 469 477 488 489 558 559
    569 579 588 599 667 668 677 679 688 779
    789 799 889 899出现2次的号码(61注):
    001 002 005 008 011 012 013 014 018 019
    023 025 027 028 035 038 055 056 058 059
    088 116 117 124 127 128 134 136 139 146
    149 166 168 169 177 224 236 237 244 246
    248 249 257 267 268 269 347 348 356 359
    367 378 457 458 467 479 567 568 578 678
    689出现3次的号码(17注):
    123 135 137 138 145 148 156 157 238 245
    256 345 357 368 456 468 478*/import java.util.Arrays;
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test{
    public static void main(String[] args){
    String content = "002 006 012 016 022 025 028 029 056 058 " +
    "059 066 069 089 113 114 116 117 123 124 "+
    "127 129 133 135 137 138 144 145 148 149 "+
    "156 157 166 168 169 177 178 223 224 226 "+
    "227 233 235 238 244 245 248 249 256 257 "+
    "259 266 267 268 269 277 278 334 336 337 "+
    "339 344 345 347 356 357 359 366 367 368 "+
    "369 377 378 389 399 446 447 456 457 458 "+
    "459 466 467 468 469 477 478 479 567 568 "+
    "579 667 677 678 679 689 779 789 799 "+
    "001 002 005 008 011 012 013 014 015 018 "+
    "019 023 025 027 028 035 037 038 045 048 "+
    "055 058 088 123 124 125 126 128 134 135 "+
    "136 137 138 139 145 146 148 156 157 158 "+
    "159 167 168 169 179 234 236 237 238 245 "+
    "246 248 256 258 268 279 289 335 345 347 "+
    "348 355 357 367 368 378 379 448 456 458 "+
    "468 478 479 488 568 578 668 688 "+
    "001 005 008 011 013 014 018 019 023 026 "+
    "027 035 038 039 055 056 057 059 078 088 "+
    "112 115 116 117 118 119 122 123 127 128 "+
    "134 135 136 137 138 139 145 146 147 148 "+
    "149 155 156 157 166 177 188 199 224 225 "+
    "228 229 236 237 238 244 245 246 249 255 "+
    "256 257 267 269 288 299 345 348 356 357 "+
    "358 359 368 456 457 467 468 478 489 558 "+
    "559 567 569 578 588 599 678 689 889 899 123";
    String[] nums = content.split(" ");
    int total = nums.length;//得到总注数
    Arrays.sort(nums);
    ArrayList<String>[] list = getResult(nums); System.out.println(total);

    int time = 0;
    for(ArrayList<String> l : list){
    time ++;
    if(l != null){
    System.out.println("出现" + time + "次的号码(" + l.size() +"注)");
    int j = 0;
    for(String str : l){
    if( j % 10 == 0){
    System.out.println();
    }
    j ++;
    System.out.printf("%s ",str);
    } System.out.println();
    }
    }
    } private static ArrayList<String>[] getResult(String[] nums){
    ArrayList<String>[] list = new ArrayList[nums.length];
    String content = Arrays.toString(nums);
    content=content.replaceAll("[ \\[\\],]","");
    String regex = "^(\\d{3})((\\1)*)";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(content);
    int group = 0;
    int time = 0;
    while(content.length() > 0){
    //System.out.println(content + "aaaaaaa");
    if(matcher.find()){
    time += matcher.group(2).length() / 3;
    if(list[time] == null){
    //如果对应次数的list为null,初始化
    list[time] = new ArrayList<String>();
    group ++;//统计一共多少组
    }
    //添加到对应的list中
    list[time].add(matcher.group(1));
    //下面重新初始化变量.
    time = 0;
    content = content.replaceFirst(matcher.group(0),"");
    matcher = pattern.matcher(content);
    }else{
    //一定满足条件
    throw new IllegalArgumentException("Illegal Argument");
    }
    } System.out.print("共有" + group + "个号码群进行统计,总注数为:");
    return list;
    }
    }
      

  4.   

    micsolaris 大侠
    代码试了,结果也正确。
    能不能把String content = "002 006 012 016 022 025 028 029 056 058 " +
                "059 066 069 089 113 114 116 117 123 124 "+
                "127 129 133 135 137 138 144 145 148 149 "+
                "156 157 166 168 169 177 178 223 224 226 "+
                "227 233 235 238 244 245 248 249 256 257 "+
                "259 266 267 268 269 277 278 334 336 337 "+
                "339 344 345 347 356 357 359 366 367 368 "+
                "369 377 378 389 399 446 447 456 457 458 "+
                "459 466 467 468 469 477 478 479 567 568 "+
                "579 667 677 678 679 689 779 789 799 "+
                "001 002 005 008 011 012 013 014 015 018 "+
                "019 023 025 027 028 035 037 038 045 048 "+
                "055 058 088 123 124 125 126 128 134 135 "+
                "136 137 138 139 145 146 148 156 157 158 "+
                "159 167 168 169 179 234 236 237 238 245 "+
                "246 248 256 258 268 279 289 335 345 347 "+
                "348 355 357 367 368 378 379 448 456 458 "+
                "468 478 479 488 568 578 668 688 "+
                "001 005 008 011 013 014 018 019 023 026 "+
                "027 035 038 039 055 056 057 059 078 088 "+
                "112 115 116 117 118 119 122 123 127 128 "+
                "134 135 136 137 138 139 145 146 147 148 "+
                "149 155 156 157 166 177 188 199 224 225 "+
                "228 229 236 237 238 244 245 246 249 255 "+
                "256 257 267 269 288 299 345 348 356 357 "+
                "358 359 368 456 457 467 468 478 489 558 "+
                "559 567 569 578 588 599 678 689 889 899 123";
    代码,字符串改成文件。
    如:out.txt
    内容:002 006 012 016 022 025 028 029 056 058
    059 066 069 089 113 114 116 117 123 124
    127 129 133 135 137 138 144 145 148 149
    156 157 166 168 169 177 178 223 224 226
    227 233 235 238 244 245 248 249 256 257
    259 266 267 268 269 277 278 334 336 337
    339 344 345 347 356 357 359 366 367 368
    369 377 378 389 399 446 447 456 457 458
    459 466 467 468 469 477 478 479 567 568
    579 667 677 678 679 689 779 789 799001 002 005 008 011 012 013 014 015 018
    019 023 025 027 028 035 037 038 045 048
    055 058 088 123 124 125 126 128 134 135
    136 137 138 139 145 146 148 156 157 158
    159 167 168 169 179 234 236 237 238 245
    246 248 256 258 268 279 289 335 345 347
    348 355 357 367 368 378 379 448 456 458
    468 478 479 488 568 578 668 688
    001 005 008 011 013 014 018 019 023 026
    027 035 038 039 055 056 057 059 078 088
    112 115 116 117 118 119 122 123 127 128
    134 135 136 137 138 139 145 146 147 148
    149 155 156 157 166 177 188 199 224 225
    228 229 236 237 238 244 245 246 249 255
    256 257 267 269 288 299 345 348 356 357
    358 359 368 456 457 467 468 478 489 558
    559 567 569 578 588 599 678 689 889 899
      

  5.   

    package ioed;import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.TreeMap;public class CSDNIO { public static int getTime(Map<String, Integer> map, Integer i) {
    int j = 0;
    for (Object obj : map.values()) {
    if (i.equals(obj)) {
    j++;
    }
    }
    return j;
    } public static void main(String[] args) { BufferedReader buffReader = null;
    try {
    buffReader = new BufferedReader(new InputStreamReader(
    new FileInputStream("d:\\out.txt")));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } Map<String, Integer> map = new TreeMap<String, Integer>();
    String s = null;
    String ss[] = null; while (true) { try {
    s = buffReader.readLine();
    } catch (IOException e) {
    e.printStackTrace();
    }
    if (s != null && s.length() > 0) {
    ss = s.split(" ");
    } else if (s == null) {
    break;
    } else if (s.length() == 0) {
    continue;
    }
    for (String str : ss) {
    if (map.containsKey(str)) {
    map.put(str, map.get(str) + 1);
    } else {
    map.put(str, 1);
    }
    }
    }
    for (int i = 0; i < map.size(); i++) {
    if (getTime(map, i) > 0) {
    System.out.println();
    System.out.println("" + i + "(" + getTime(map, i) + "):"); int k = 0;
    for (Entry<String, Integer> entry : map.entrySet()) {
    if (entry.getValue().equals(i)) {
    System.out.print(entry.getKey() + " ");
    k++;
    if (k % 10 == 0) {
    System.out.println();
    System.out.flush();
    }
    }
    }
    }
    }
    }
    }这个不行吗???