zoukankan      html  css  js  c++  java
  • CF-845B

    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的整数串,求最小改变多少个整数可以使前三个数之和等于后三个数。

    起初直接暴力写太复杂了,可以先求出前后的和s1,s2 。将较小的一边三个数变为9-a[i],即将它们全部变为9时差值的变化量,而后排序,从大到小减差值直到差值不大于0即可。

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int a[10];
     5 
     6 int main(){
     7     ios::sync_with_stdio(false);
     8     string s;
     9     cin>>s;
    10     int s1=0,s2=0;
    11     for(int i=0;i<3;i++){
    12         a[i]=s[i]-'0';
    13         s1+=a[i];
    14     }
    15     for(int i=3;i<6;i++){
    16         a[i]=s[i]-'0';
    17         s2+=a[i];
    18     }
    19     int num=s1-s2; 
    20     if(num>0){
    21         for(int i=3;i<6;i++){
    22             a[i]=9-a[i];
    23         }
    24     }
    25     else{
    26         for(int i=0;i<3;i++){
    27             a[i]=9-a[i];
    28         }
    29         num=-num;
    30     }
    31     sort(a,a+6);
    32     for(int i=5;i>0;i--){
    33         if(num<=0){
    34             cout<<5-i<<endl;
    35             break;
    36         }
    37         num-=a[i];
    38     }
    39     return 0;
    40 } 
  • 相关阅读:
    10.11-10.16
    10.8-10.10
    9.26-28
    9.29css继承属性
    表单的学习
    排版
    css补充
    今天学的新内容
    新内容
    文本样式的修改
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/7421927.html
Copyright © 2011-2022 走看看