• CodeForces


    Luba And The Ticket

    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.

    Example
    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.

    题意:六个数字改变最少的数字使得前三位的和等于后三位的和。

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    int n,n2;
    string s,ss;
    int ans,ans1;
    void dfs(int y,int k)
    {
        if((ss[3]-'0')+(ss[4]-'0')+(ss[5]-'0')==(ss[0]-'0')+(ss[1]-'0')+(ss[2]-'0'))
         {
              ans=min(y,ans);
         }
        if(y>6||k>=6)
            return;
        for(int i=0; i<=9; i++)
        {
            if(i!=s[k]-'0')
            {
                ss[k]=i+'0';
                dfs(y+1,k+1);
                ss[k]=s[k];
            }
            else
            {
                dfs(y,k+1);
            }
        }
        return ;
    }
    int main()
    {
    
        cin>>s;
        ss=s;
        for(int i=0; i<3; i++)
        {
            n+=(s[i]-'0');
            n2+=(s[i+3]-'0');
        }
        ans=100;
        ans1=100;
        dfs(0,0);
        cout<<min(ans1,ans)<<endl;
    }


  • 相关阅读:
    栈和队列_leetcode23
    Android 广播
    Android Service
    Logcat monkey命令
    设计模式-观察者模式
    Android之Fragment优点
    Android架构: MVC 新浪微博
    Android消息处理机制(Handler、Looper、MessageQueue与Message)
    Android Annotations Eclipse 配置 (3)
    Android Annotations(2)
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053303.html
Copyright © 2020-2023  润新知