请选择 进入手机版 | 继续访问电脑版

资源299论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

日本代购,韩国代购,专业海外代购服务
查看: 62|回复: 0

nearly lucky number java 幸运数字水题

[复制链接]

621

主题

679

帖子

2245

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2245
发表于 2019-11-12 11:16:21 | 显示全部楼层 |阅读模式
先领券再购物更优惠
本帖最后由 开心就好 于 2019-11-12 11:20 编辑

nearly lucky number java Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Unfortunately, not all numbers are lucky. Petya calls a number nearly lucky if the number of lucky digits in it is a lucky number. He wonders whether number n is a nearly lucky number.

Input
The only line contains an integer n (1 ≤ n ≤ 1018).

Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

Output
Print on the single line "YES" if n is a nearly lucky number. Otherwise, print "NO" (without the quotes).

Examples
input
40047
output
NO
input
7747774
output
YES
input
1000000000000000000
output
NO
Note
In the first sample there are 3 lucky digits (first one and last two), so the answer is "NO".

In the second sample there are 7 lucky digits, 7 is lucky number, so the answer is "YES".

In the third sample there are no lucky digits, so the answer is "NO".

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.io.PrintWriter;
  7. import java.util.Scanner;

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

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

  27.     public static void main(String[] args) throws IOException {
  28.         //StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
  29.         Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
  30.         PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  31.         String inputNum;     // 输入的数
  32.         int luckPosition;   // 记录幸运位
  33.         while (in.hasNext()) {
  34.             inputNum = in.next();
  35.             luckPosition = 0;
  36.             char temp;
  37.             for (int index = 0; index < inputNum.length(); index++) {
  38.                 temp = inputNum.charAt(index);
  39.                 if (temp == '4' || temp == '7') {
  40.                     luckPosition++;
  41.                 }
  42.             }
  43.             out.println(isLuckNum(luckPosition) ? "YES" : "NO");
  44.         }
  45.         out.flush();
  46.     }
  47. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /3 下一条

QQ|Archiver|手机版|小黑屋|资源299论坛

GMT+8, 2019-12-11 20:02 , Processed in 0.020938 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表