Codeforces Round #104 (Div. 2)---A. Lucky Ticket
来源:互联网 发布:c语言课程设计 编辑:程序博客网 时间:2024/05/16 09:27
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.
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.
On the first line print "YES" if the given ticket number is lucky. Otherwise, print "NO" (without the quotes).
247
NO
44738
NO
44774
YES
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;}
- Codeforces Round #104 (Div. 2)---A. Lucky Ticket
- Codeforces Round #160 (Div. 2)---A. Roma and Lucky Numbers
- Codeforces Round #104 (Div. 2) E - Lucky Subsequence
- Codeforces Round #104 (Div. 2) E Lucky Subsequence
- Codeforces Round #111 (Div. 2) B Unlucky Ticket (贪心)
- Codeforces Beta Round #91 (Div. 2) / 122A Lucky Division (数论)
- A. Lucky Ticket
- Codeforces Round #104 (Div. 1) E Lucky Queries
- Codeforces Round #150 (Div. 2) B. Undoubtedly Lucky Numbers
- Codeforces Beta Round #91 (Div. 2 Only) C. Lucky Sum
- Codeforces Beta Round #91 (Div. 2 Only) D. Lucky Transformation
- Codeforces Beta Round #91 (Div. 2 Only)-E. Lucky Permutation
- Codeforces Beta Round #42 (Div. 2)-C. Lucky Tickets
- Codeforces Round #183 (Div. 2)-C. Lucky Permutation Triple
- Codeforces Round #104 (Div. 2) D. Lucky Number 2 模拟策略
- CodeForces Round 176Div1 A Lucky permutation
- Educational Codeforces Round 21 A. Lucky Year
- Educational Codeforces Round 21 A. Lucky Year
- PLSQL Developer 连接本地数据库
- 【安装】libjpeg for python
- BP神经网络在双色球彩票上的预测实验及实现
- class高级用法
- 第[3天]程序的控制结构——循环嵌套例题02
- Codeforces Round #104 (Div. 2)---A. Lucky Ticket
- class基础用法
- USACO 2.1 Ordered Fractions
- NYOJ 10 skiing (深搜和动归)
- Android GridView的使用方法
- JavaScript:undefined And null区别
- ios:KVC的概述与使用
- ffmpeg源码分析之avformat_alloc_context
- Zend Framework 2 入门-数据分页-pagination