Codeforces Round #104 (Div. 2)---A. Lucky Ticket

来源:互联网 发布:c语言课程设计 编辑:程序博客网 时间:2024/05/16 09:27

Lucky Ticket
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 477444 are lucky and 517467 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 test(s)
input
247
output
NO
input
44738
output
NO
input
44774
output
YES
Note

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.






解题思路:暴力。直接扫描一遍,若数中出现了不是4和7的数字,则直接输出“NO”;否则,求出前一半数字之和,后一半数字之和,看是否相等,相等则输出“YES”,否则输出“NO”。





AC代码:

#include <iostream>#include <cstdio>#include <string>using namespace std;int main(){//freopen("in.txt", "r", stdin);int n;string s;while(scanf("%d", &n)==1){cin>>s;int flag = 1;for(int i=0; i<n; i++){if(s[i] != '4' && s[i] != '7')flag = 0;}int sum1 = 0, sum2 = 0;for(int i=0; i<n/2; i++)sum1 += (s[i] - '0');for(int i=n-1; i>=n/2; i--){sum2 += (s[i] - '0');}if(sum1 != sum2)  flag = 0;if(!flag)  printf("NO\n");else  printf("YES\n");}return 0;}



0 0