• CodeForces 146A Lucky Ticket


    Lucky Ticket
    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

    Petya loves tickets very much. As we know, each ticket has a number that is a positive integer. Its length equals n (n is always even). Petya calls a ticket lucky if the ticket's number is a lucky number and the sum of digits in the first half (the sum of the first n / 2 digits) equals the sum of digits in the second half (the sum of the last n / 2 digits). Check if the given ticket is lucky.

    Input

    The first line contains an even integer n(2 ≤ n ≤ 50) — the length of the ticket number that needs to be checked. The second line contains an integer whose length equals exactly n — the ticket number. The number may contain leading zeros.

    Output

    On the first line print "YES" if the given ticket number is lucky. Otherwise, print "NO" (without the quotes).

    Sample Input

    Input
    2
    47
    Output
    NO
    Input
    4
    4738
    Output
    NO
    Input
    4
    4774
    Output
    YES

    Hint

    In the first sample the sum of digits in the first half does not equal the sum of digits in the second half (4 ≠ 7).

    In the second sample the ticket number is not the lucky number.

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 int main()
     6 {
     7     char a[105];
     8     int i,j,n;
     9     while(scanf("%d",&n)!=EOF)
    10     {
    11         int flg=1,s1=0,s2=0;
    12         scanf("%s",a);
    13         for(i=0;i<n;i++)
    14         {
    15             if(i<n/2)
    16                 s1=s1+a[i]-'0';
    17             else
    18                 s2=s2+a[i]-'0';
    19             if(a[i]=='4' || a[i]=='7')
    20             {
    21                 flg=1;
    22             }
    23             else
    24             {
    25                 flg=0;
    26                 break;
    27             }
    28         }
    29         //printf("%d %d
    ",s1,s2);
    30         if(s1!=s2)
    31             flg=0;
    32         if(flg)
    33             printf("YES
    ");
    34         else
    35             printf("NO
    ");
    36     }
    37     return 0;
    38 }
    View Code
  • 相关阅读:
    再回首,Java温故知新(八):Java基础之字符串
    《Prism 5.0源码走读》 设计模式
    《Prism 5.0源码走读》Bootstrapper
    VS编译时自动下载NuGet管理的库
    《Prism 5.0源码走读》Prism 5.0简介
    代码阅读
    如何建设个人品牌
    Hexo建站教程
    Codeforce:131A. cAPS lOCK
    一文看懂《最大子序列和问题》
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771482.html
Copyright © 2020-2023  润新知