日期:2014-05-17  浏览次数:20772 次

大家近来看看这个怎么写
String a[]={"sam","sam","liang","zhuo","sam","liang","sam","zhuo","zhuo","liang"};


我要输出来的时候是

sam,sam1,liang,zhuo,sam2,liang1,sam3,zhuo1,zhuo2,liang2

------解决方案--------------------
Java code

public class Test {

    public static void main(String[] args) {
        String a[]={"sam","sam","liang","zhuo","sam","liang","sam","zhuo","zhuo","liang"};
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < i; j++) {
                if(a[j] == a[i]){
                    a[i] = a[j]+(b[j]+1);
                    b[j] = b[j]+1;
                }
            }
        }
        for(int k = 0;k<a.length;k++){
            System.out.print(a[k] );
            if(k != (a.length-1)){
                System.out.print(",");
            }
        }
        
    }
    
}

------解决方案--------------------
想了好久才想出来
Java code

public class Test 
{
    public static void main(String[] args) 
    {
        String[] s={"sam","sam","liang","zhuo","sam","liang","sam","zhuo","zhuo","liang"};
        String[] str = new String[s.length];
        str[0] = "sam";        
        for(int i=1;i<s.length;i++){
            int count = 0;
            String ss = s[i];
            for(int x=0;x<i;x++){                
                if(ss.equals(s[x])){//出现了几次
                    count++;                    
                }else{
                    str[i] = ss;
                }
            }
            str[i] = ss+(count==0?"":count);
        }
        for(String x: str){
            System.out.println(x);
        }
        
    }
}