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

最近做了几套华为的机试题,今天有时间,把之前写的几套代码全都贴出来。题目都只记得个大概,将就着看吧,不过代码都是完整的,自认为写的还行。

题目描述

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

Java代码

import java.util.Scanner;

public class JudgePostOrder {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] count = new int[5];
        int times = 0;
        while(sc.hasNext()) {
            String str = sc.nextLine();
            for(int i=0; i<str.length();i++) {
                if(times >= 5) {
                    times = 0;
                }
                char c = str.charAt(i);
                if(c == '1') {
                    count[times++]++;
                }
            }
        }
        int min = count[0];
        for(int num:count) {
            if(num < min) {
                min = num;
            }
        }
        System.out.println(min);
    }
}

猜你喜欢

转载自blog.csdn.net/u010005281/article/details/80412927