华为机试题:统计一共有多少套五福

题目大概意思是:一个团队集五福,每个人集完五福后,用一串长度为5的字符串表示集到的结果,比如“10011”为该人集到了第一张、第四张和第五张五福。问这个团队一共能凑齐完整的多少套五福

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        getWFCount();
    }
    //获取五福套数
    public static void getWFCount() {
        System.out.println("请输入一组由0和1组成的5位字符串,中间用,号分割:");
        System.out.println("如:10110,11001");
        System.out.println("请输入:");
        while(true){
            Scanner sc = new Scanner(System.in);
            String s = sc.next();
            if(null == s || "" == s ){
                System.out.println("输入有误,请重新输入:");
                continue;
            }else{
                String[] sq = s.split(",");
                int[] list = {0,0,0,0,0};
                for(String sPart:sq){
                    if(sPart.length() != 5){
                         System.out.println("输入有误,请重新输入:");
                         continue;
                    }
                    if(!sPart.matches("[01]{5}")){
                        System.out.println("输入有误,请重新输入:");
                         continue;
                    }
                    for(int i=0;iif(sPart.charAt(i) == '1'){
                            int n= list[i];
                            n++;
                            list[i] = n;
                        }
                    }
                }
                for(int i =0;i < list.length - 1;i++)
                {
                    for(int j = 0;j <  list.length - 1-i;j++)// j开始等于0,
                    {
                        if(list[j] < list[j+1])
                        {
                            int temp = list[j];
                            list[j] = list[j+1];
                            list[j+1] = temp;
                        }
                    }
                }
//              System.out.println(Arrays.toString(list));
                System.out.println("该团队能凑齐"+list[4]+"套五福");
                sc.close();
                break;
            }
        }
    }
}

你可能感兴趣的:(程序员,招聘)