当前位置:   article > 正文

codeforces 110A(Nearly Lucky Number) Java_codeforce 110a nearly lucky number

codeforce 110a nearly lucky number

加班回到寝室,再来刷一波水题!!!

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 题意:如果一个数中 4和7 的个数为lucky number(每位数字仅仅是 4或7),则该数是nearly lucky number,问该数是不是nearly lucky number?
 *
 * @author TinyDolphin
 *         2017/6/26 22:15.
 */
public class Main {

    /**
     * 判断该数是否是幸运数字(即仅仅包含4和7)
     */
    public static boolean isLuckNum(int num) {
        String str = num + "";
        for (int index = 0; index < str.length(); index++) {
            if (str.charAt(index) != '4' && str.charAt(index) != '7') {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) throws IOException {
        //StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        String inputNum;     // 输入的数
        int luckPosition;   // 记录幸运位
        while (in.hasNext()) {
            inputNum = in.next();
            luckPosition = 0;
            char temp;
            for (int index = 0; index < inputNum.length(); index++) {
                temp = inputNum.charAt(index);
                if (temp == '4' || temp == '7') {
                    luckPosition++;
                }
            }
            out.println(isLuckNum(luckPosition) ? "YES" : "NO");
        }
        out.flush();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/75800
推荐阅读
相关标签
  

闽ICP备14008679号