Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n— the number of cards with digits that you have (1≤n≤100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s1,s2,…,sn. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Sample Input
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
int main()
{
int n,i,ans=0,sum=0,len=0;
char s[110];
cin>>n;
ans=n/11;
cin>>s;
len=strlen(s);
for(i=0;i<len;i++)
{
if(s[i]=='8')
sum++;
}
ans=min(ans,sum);
cout<<ans<<endl;
return 0;
}
You are given a positive integer n.
Let S(x) be sum of digits in base 10 representation of x, for example, S(123)=1+2+3=6, S(0)=0.
Your task is to find two integers a,b, such that 0≤a,b≤n, a+b=n and S(a)+S(b) is the largest possible among all such pairs.
Input
The only line of input contains an integer n(1≤n≤1012).
Output
Print largest S(a)+S(b) among all pairs of integers a,b, such that 0≤a,b≤n and a+b=n.
Sample Input
Input
35
Output
17
Input
10000000000
Output
91
Hint
In the first example, you can choose, for example, a=17 and b=18, so that S(17)+S(18)=1+7+1+8=17. It can be shown that it is impossible to get a larger answer.
In the second test example, you can choose, for example, a=5000000001 and b=4999999999, with S(5000000001)+S(4999999999)=91. It can be shown that it is impossible to get a larger answer.
这是一个错误的代码 样例123
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
int main()
{
ll a,b,i,n,ans1=0,ans2=0,ans=0;
cin>>n;
if(n%2!=0)
{
a=n/2;
b=n/2+1;
}
else
{
a=n/2-1;
b=n/2+1;
}
while(a)
{
ans1+=a%10;
a/=10;
}
while(b)
{
ans2+=b%10;
b/=10;
}
ans=ans1+ans2;
cout<<ans<<endl;
return 0;
}
这是AC代码
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
int main()
{
ll n,k=0,kk=0,ans1=0,ans2=0,t;
cin>>n;
while(k<=n)
{
k=k*10+9;
t=k;
if(k>n)
{
t=(t-9)/10;
break;
}
}
k=t;
kk=n-k;
//cout<<k<<" "<<kk<<endl;
while(k)
{
ans1+=k%10;
k/=10;
}
while(kk)
{
ans2+=kk%10;
kk/=10;
}
cout<<ans1+ans2<<endl;
return 0;
}
use MathJax to parse formulas
Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion:
- An 'L' indicates he should move one unit left.
- An 'R' indicates he should move one unit right.
- A 'U' indicates he should move one unit up.
- A 'D' indicates he should move one unit down.
But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.
Input
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.
Output
If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.
Sample Input
Input
RRU
Output
-1
Input
UDUR
Output
1
Input
RUUR
Output
2
Hint
In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.
In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin.
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
char s[100100];
int main()
{
memset(s,0,sizeof(s));
ll n,ans=0,i,j,l=0,r=0,u=0,d=0;
cin>>s;
n=strlen(s);
if(n%2!=0)
cout<<"-1"<<endl;
else
{
for(i=0;i<n;i++)
{
if(s[i]=='L')
l++;
else
if(s[i]=='R')
r++;
else
if(s[i]=='U')
u++;
else
if(s[i]=='D')
d++;
}
ll ans1=0,ans2=0;
ans1=max(l,r)-min(l,r);
ans2=max(u,d)-min(u,d);
if((ans1+ans2)%2==1)
cout<<"-1"<<endl;
else
cout<<(ans1+ans2)/2<<endl;
}
return 0;
}