codeforces 845B Luba And The Ticket

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 she 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的字符串,可以使任意数变为另一个数,问最少需要几次变换使前三个数的和等于后三个数的和

变换的情况只有4种,简单模拟一下就好了,要使变换次数最少肯定是减少后三个中最大的,或是补上前三个中最小的到9(默认前三个的和小于后三个的),做几次变换就可以知道结果了


#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;

#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))

const int inf=0x3f3f3f3f;
char g[8];
int sum1,sum2,a[3],b[3];

void Swap(int a[3],int b[3])
{
    for0(i,3)swap(a[i],b[i]);
}

int main()
{
    while(gets(g)!=NULL)
    {
        for0(i,3)a[i]=g[i]-'0',sum1+=g[i]-'0';
        for(int i=3;i<6;i++)b[i-3]=g[i]-'0',sum2+=g[i]-'0';
        sort(a,a+3);
        sort(b,b+3);
        if(sum1>sum2)Swap(a,b),swap(sum1,sum2);//默认前面的三个小
        int p=sum2-sum1;
        if(!p)
            pf("0\n");
        else if(p<=9-a[0]||p<=b[2])//只有一个数字变换的最大情况
            pf("1\n");
        else if(p<=9-a[0]+9-a[1]||p<=b[2]+b[1]||p<=9-a[0]+b[2])//两个数字变换有三种情况
            pf("2\n");
        else
            pf("3\n");
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章