固定一个数组吧  A={2,2,2,3,3,3,4,5,5,5} 
将这个数组连续在一起的相同3个元素输出,,
结果是000,333,555。。、
把他们放到数组里也行 B={0,0,0,3,3,3,5,5,5};
要求 程序的运算次数要越少越好,,,,这是10个数字 一个for循环就10次运算,
大家加油

解决方案 »

  1.   


    public class Test {
    public static void main(String[] args){
    int[] a = {2,2,2,3,3,3,4,5,5,5};
    int[] b = new int[10];
    int temp;
    int k=0;

    for (int i = 0; i<a.length-2; i++){
    temp = a[i];
    if (temp == a[i+1] && temp == a[i+2]){
    b[k] = b[k+1] = b[k+2] = temp;
    k = k+3;
    }
    } for (int i=0; i<b.length; i++){
    System.out.println(b[i]);
    }
    }
    }
      

  2.   

    不是非常明白你的意思,这是根据我的理解写的:public class Test {   public static void main(String[] args){
            int[] a = {2,2,2,3,3,3,4,5,5,5};
            for(int i=0;i<10;i++){
             if(a[i]==a[i+1]){
             System.out.print(a[i]+""+a[i]+""+a[i]);
             i+=3;
             if(i<9)
             System.out.print(",");
             }
            }
      }
    }
      

  3.   

    一次循环加一个监听位,呵呵,1L想法正确
    没事写了个大家看下,对于上面给定的数组循环4次
    只针对你给定的数组 如果你这数组里要出现了比如22222这样的情况只输出一个222public class Test {
    public static void main(String[] args) {
    int[] a = { 2, 2, 2, 3, 3, 3, 4, 5, 5, 5 };
    StringBuffer sbuffer = new StringBuffer();
    int temp;
    int i = 0;
    while(i < a.length - 2) {
    temp = a[i];
    if (temp == a[i + 1] && temp == a[i + 2]) {
    String intToStr = new Integer(temp).toString(); 
    sbuffer.append(intToStr + intToStr + intToStr +",");
    i = i + 3;
    }else{
    i++;
    }
    }
    String str = sbuffer.toString();
    System.out.println(str.substring(0, str.length() -1));
    }
    }