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).
2 47
NO
4 4738
NO
4 4774
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构成,且字符串前半段和后半段数字之和相等。满足条件,输出“YES",不满足输出"NO"
思路:纯暴力……
#include <bits/stdc++.h> using namespace std; int main() { int n; while(cin>>n) { string s; cin>>s; int sum1=0,sum2=0; bool flag=true; for(int i=0;i<n/2;i++) { if(s[i]!='4'&&s[i]!='7') { flag=false; break; } sum1+=s[i]-'0'; } for(int i=n/2;i<n;i++) { if(s[i]!='4'&&s[i]!='7') { flag=false; break; } sum2+=s[i]-'0'; } if(flag&&sum1==sum2) { cout<<"YES"<<endl; } else cout<<"NO"<<endl; } return 0; }
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 calls a mask of a positive integer n the number that is obtained after successive writing of all lucky digits of number n from the left to the right. For example, the mask of number 72174994 is number 7744, the mask of 7 is 7, the mask of 9999047 is 47. Obviously, mask of any number is always a lucky number.
Petya has two numbers — an arbitrary integer a and a lucky number b. Help him find the minimum number c (c > a) such that the mask of number c equals b.
The only line contains two integers a and b (1 ≤ a, b ≤ 105). It is guaranteed that number b is lucky.
In the only line print a single number — the number c that is sought by Petya.
1 7
7
100 47
147
#include <bits/stdc++.h> using namespace std; int fun(int n) { vector<int>v; while(n) { int temp=n%10; if(temp==4||temp==7) { v.push_back(temp); } n/=10; } int ans=0; for(int i=v.size()-1;i>=0;i--) { ans=ans*10+v[i]; } return ans; } int main() { int a,b; while(cin>>a>>b) { while(1) { a++; if(fun(a)==b) break; } cout<<a<<endl; } return 0; }
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 has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
- replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
- swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Print on the single line the single number — the minimum number of operations needed to convert string a into string b.
47 74
1
774 744
1
777 444
3
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites.
题意:给出两个只含7和4的字符串a和b,有两种操作,第一种将a中的一个字符变成另一种(7变成4,4变成7),第二种任意交换a中两个字符的位置。问至少经过多少次操作可以把a变成b。
思路:多写几组数据,可以发现,设置两个计数器,分别统计a中为4,b中为7和b中为7,a中为4的字符个数,输出最大的那个值
感觉自己说的不是很清楚 直接上代码
#include <bits/stdc++.h> using namespace std; int main() { string a,b; while(cin>>a>>b) { int cnt1=0,cnt2=0; for(int i=0;i<a.length();i++) { if(a[i]=='4'&&b[i]=='7') cnt1++; if(a[i]=='7'&&b[i]=='4') cnt2++; } cout<<max(cnt1,cnt2)<<endl; } return 0; }
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 long lucky numbers very much. He is interested in the minimum lucky number d that meets some condition. Let cnt(x) be the number of occurrences of number x in number d as a substring. For example, if d = 747747, then cnt(4) = 2, cnt(7) = 4, cnt(47) = 2, cnt(74) = 2. Petya wants the following condition to fulfil simultaneously: cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. Petya is not interested in the occurrences of other numbers. Help him cope with this task.
The single line contains four integers a1, a2, a3 and a4 (1 ≤ a1, a2, a3, a4 ≤ 106).
On the single line print without leading zeroes the answer to the problem — the minimum lucky number d such, that cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. If such number does not exist, print the single number "-1" (without the quotes).
2 2 1 1
4774
4 7 3 1
-1
上图是规律
附上自己根据规律的代码:
#include <bits/stdc++.h> using namespace std; int main() { int cnt4,cnt7,cnt47,cnt74; while(cin>>cnt4>>cnt7>>cnt47>>cnt74) { if(abs(cnt47-cnt74)>1||cnt4+cnt7<=cnt47+cnt74||cnt4<cnt47||cnt7<cnt47||cnt4<cnt74||cnt7<cnt74) { cout<<-1<<endl; continue; } else if(cnt47>cnt74) { cnt4-=cnt74; cnt7-=cnt74; for(int i=0; i<cnt4; i++) { cout<<4; } for(int i=0; i<cnt74; i++) { cout<<74; } for(int i=0; i<cnt7; i++) { cout<<7; } cout<<endl; } else if(cnt47==cnt74) { if(cnt4>cnt47) { cnt4-=1+cnt47; cnt7-=cnt47; for(int i=0; i<cnt4; i++) { cout<<4; } for(int i=0; i<cnt47; i++) { cout<<47; } for(int i=0; i<cnt7; i++) { cout<<7; } cout<<4<<endl; } else { cnt7-=1+cnt47; cout<<7; for(int i=0; i<cnt47; i++) { cout<<47; } for(int i=0; i<cnt7; i++) { cout<<7; } cout<<endl; } } else { cnt4-=cnt47+1; cnt7-=cnt47+1; cout<<7; for(int i=0; i<cnt4; i++) { cout<<4; } for(int i=0; i<cnt47; i++) { cout<<47; } for(int i=0; i<cnt7; i++) { cout<<7; } cout<<4<<endl; } } return 0; }
就以上四题啦!E题有知识点还没学……不会写……还得加油啊