zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    Java:synchronized关键字引出的多种锁
    Java:Web Service初入门
    Java:HashMap原理与设计缘由
    Java:集合类的数据结构
    NoSQL数据库兴起
    Hadoop介绍与安装
    Java:泛型的理解
    《代码整洁之道》总结和笔记
    shell运算
    shell变量
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053303.html
Copyright © 2011-2022 走看看