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;
}



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