您的位置:首页 > 其它

Educational Codeforces Round 27 B. Luba And The Ticket(模拟)

2017-08-22 00:12 471 查看
B. Luba And The Ticket

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Luba has a ticket consisting of 6 digits. In one move s
4000he can choose digit in any position and replace it with arbitrary digit. She
wants to know the minimum number of digits she needs to replace in order to make the ticket lucky.

The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits.

Input

You are given a string consisting of 6 characters (all characters are digits from 0 to 9)
— this string denotes Luba's ticket. The ticket can start with the digit 0.

Output

Print one number — the minimum possible number of digits Luba needs to replace to make the ticket lucky.

Examples

input
000000


output
0


input
123456


output
2


input
111000


output
1


Note

In the first example the ticket is already lucky, so the answer is 0.

In the second example Luba can replace 4 and 5 with
zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required.

In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.

题意:

给你6位数,问你至少改变多少位才能使得前3位的和等于后三位,直接模拟,我这里t表示每次改变的值,把t排一下序两边差值每次减去t[i]就好了

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
using namespace std;
#define lson k*2#define rson k*2+1#define M (t[k].l+t[k].r)/2#define INF 1008611111#define ll long long
#define eps 1e-15
int a[10];
int t[10];
int cmp(int x,int y)//从大到小排序
{
return x>y;
}
int main()
{
int i,j,n,num1,num2;
for(i=0;i<6;i++)
{
scanf("%1d",&a[i]);
}
num1=a[0]+a[1]+a[2];
num2=a[3]+a[4]+a[5];
if(num1==num2)
printf("0\n");
else
{
int ans=0;
sort(a,a+3);//对前三位排序
sort(a+3,a+6);//后三位排序
int d=abs(num1-num2);
if(num1>num2)
{
t[0]=a[0];
t[1]=a[1];
t[2]=a[2];
t[3]=9-a[3];
t[4]=9-a[4];
t[5]=9-a[5];
sort(t,t+6,cmp);//对t从大到小排序,表示每次可以改变的差值
for(i=0;i<6;i++)
{
if(d<=t[i])
{
ans=i+1;
break;
}
else
d-=t[i];
}
}
else
{
t[0]=a[3];
t[1]=a[4];
t[2]=a[5];
t[3]=9-a[0];
t[4]=9-a[1];
t[5]=9-a[2];
sort(t,t+6,cmp);
for(i=0;i<6;i++)
{
if(d<=t[i])
{
ans=i+1;
break;
}
else
d-=t[i];
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: