Unlucky Number I

来源:互联网 发布:淘宝羊毛衫 编辑:程序博客网 时间:2024/06/13 13:57
Unlucky Number I
Description

Some positive integers’ representationsonly consist of the unlucky digits 4 and 7. We call them Unlucky Numbers. Forexample, numbers 74, 774, 7 are unlucky while 2, 12, 352 are not.

Please determine whether a given n is an unlucky number.

Input

There are multiple test cases. Thefirst line of input is an integerT indicating the number of test cases. ThenT test cases follow.

For each test case:

Line 1. This line contains an integer n (1 ≤n ≤ 109).

Output

For each test case:

Line 1. Output "YES" if thegiven n is an unlucky number,otherwise output "NO" instead.

Sample Input

2

47747

12347

Sample Output

YES

NO

题意:仅有4或者7就输出YES,否则输出NO。
思路1:用字符串,如果4或者7出现的次数等于字符串的长度,就是YES!
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            int flag=0;
            char a[20];
            scanf("%s",a);
            int l=strlen(a);
            for(int i=0;i<l;i++)
            {
                if(a[i]=='4'||a[i]=='7')
                {
                      flag++;
                }
            }
            if(flag==l)
            {
                printf("YES\n");
            }
            else printf("NO\n");
        }
    }
    return 0;
}
思路2:如果哪一位数字既不是4也不是7就输出NO,否则·输出YES。
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            int flag=0;
            char a[20];
            scanf("%s",a);
            int l=strlen(a);
            for(int i=0;i<l;i++)
            {
                if(a[i]!='4'&&a[i]!='7')  //这里我刚开始用了或,导致出错@~@,尴尬了,哈哈。。
                {
                      flag=1;
                      break;
                }
            }
            if(flag==1)
            {
                printf("NO\n");
            }
            else printf("YES\n");
        }
    }
    return 0;
}


0 0
原创粉丝点击