[编程题]ZOJ
  • 热度指数:6324 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出。

输入描述:
每组用例占一行,包含ZOJ三个字符。
1<=length<=100。


输出描述:
对于每组输入,请输出一行,表示按照要求处理后的字符串。
具体可见样例。
示例1

输入

ZZOOOJJJ

输出

ZOJZOJOJ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            //先统计每个字符的数量
            int[] count = new int[3];
            int len = s.length();
            for (int i = 0; i < len; i++) {
                if (s.charAt(i) == 'Z') count[0]++;
                if (s.charAt(i) == 'O') count[1]++;
                if (s.charAt(i) == 'J') count[2]++;
            }

            //整理
            for (int i = 0; i < len; i++) {
                if (count[0] != 0) {
                    System.out.print('Z');
                    count[0]--;
                }
                if (count[1] != 0) {
                    System.out.print('O');
                    count[1]--;
                }
                if (count[2] != 0) {
                    System.out.print('J');
                    count[2]--;
                }
            }

        }
    }

}


发表于 2021-04-05 11:51:19 回复(0)
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[] array = scanner.nextLine().toCharArray();
        int z=0;
        int o=0;
        int j= 0;
        for (char c : array) {
            switch (c) {
                case 'Z': z++;break;
                case 'O': o++;break;
                case 'J': j++;break;
            }
        }
        StringBuilder builder = new StringBuilder();
        while (z>0||o>0||j>0){
            if (z>0) {
                builder.append("Z");
                z--;
            }
            if (o>0) {
                builder.append("O");
                o--;
            }
            if (j>0) {
                builder.append("J");
                j--;
            }
        }
        System.out.println(builder.toString());
    }
}


编辑于 2020-03-06 22:17:10 回复(0)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.next();
            String z = s.replaceAll("[^Z]", "");
            String o = s.replaceAll("[^O]", "");
            String j = s.replaceAll("[^J]", "");
            int len = (z.length()>o.length()?z.length():o.length())>j.length()?(z.length()>o.length()?z.length():o.length()):j.length();
            int x = 0,y=0,w=0;
            for (int i = 0; i < len; i++) {
                if(x<z.length())
                    System.out.print(z.charAt(x));
                if(y<o.length())
                    System.out.print(o.charAt(x));
                if(w<j.length())
                    System.out.print(j.charAt(x));
                x++;
                y++;
                w++;
            }
        }
    }
}

发表于 2018-06-10 21:57:16 回复(0)
import java.util.Scanner;

/**
* @author Allen_Hua
* @create_time May 15, 2018 1:05:59 PM
*/
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            String str = scan.nextLine();
            // 分别记录Z O J出现次数
            int a = 0, b = 0, c = 0;
            for (int i = 0; i < str.length(); i++) {
                char temp = str.charAt(i);
                if (temp == 'Z') {
                    a++;
                }
                else if (temp == 'O') {
                    b++;
                }
                else if (temp == 'J') {
                    c++;
                }
            }
            // 控制格式输出结果
            while (a > 0 || b > 0 || c > 0) {
                if (a > 0) {
                    System.out.print('Z');
                }
                if (b > 0) {
                    System.out.print('O');
                }
                if (c > 0) {
                    System.out.print('J');
                }
                a--;
                b--;
                c--;
            }
        }
    }
}
发表于 2018-05-15 13:20:21 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
        String line=null;
        while((line=input.readLine())!=null) {
            int num[]=new int[128];
            for(int i=0;i<line.length();i++) {
                num[line.charAt(i)]++;
            }
            while(num['Z']>0||num['O']>0||num['J']>0){
                if(num['Z']>0) {
                    num['Z']--;
                    System.out.print("Z");
                }
                if(num['O']>0) {
                    num['O']--;
                    System.out.print("O");
                }
                if(num['J']>0) {
                    num['J']--;
                    System.out.print("J");
                }
            }
        }
    }
}
发表于 2018-05-05 21:05:21 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String zStr = in.next();
        int[] counts = new int[3];
        for (int i = 0; i < zStr.length(); i++) {
            char ch = zStr.charAt(i);
            if (ch == 'Z')
                counts[0]++;
            else if (ch == 'O')
                counts[1]++;
            else
                counts[2]++;
        }
        char[] zoj = { 'Z', 'O', 'J' };
        String outStr = "";
        while (counts[0]+counts[1]+counts[2] > 0) {
            int times = Integer.MAX_VALUE;
            String str = "";
            for (int i = 0; i < 3; i++) {
                if (counts[i] > 0) {
                    str += zoj[i];
                    if (counts[i] < times)
                        times = counts[i];
                }
            }
            for (int i = 0; i < times; i++) {
                outStr += str;
            }
            for (int i = 0; i < 3; i++)
                if (counts[i] > 0)
                    counts[i] -= times;
        }
        System.out.print(outStr);
    }
}

发表于 2018-03-18 14:07:01 回复(0)