zoukankan      html  css  js  c++  java
  • codeforeces 845B

    题解

    codefores 845B

    原题

    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.

    题目大意

    给你一个长度为6的字符串,可以使任意数变为另一个数,问最多需要几次变换使前三个数的和等于后三个数的和。

    样例

    simple1
    000000
    0
    simple2
    123456
    2
    simple3
    111000
    1

    思路

    变换的情况只有4种,简单判断一下(变一次有一种,变两次有三种)。

    1. 要使变换次数最少尽量减少后三个中最大的
    2. 或是补上前三个中最小的(默认前三个的和小于后三个的)

    代码

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    char g[8];  
    int sum1,sum2,a[3],b[3];  
    void Swap(int a[3],int b[3])  
    {  
        for(int i=0;i<3;i++) swap(a[i],b[i]);  
    }  
      
    int main()  
    {  
        while(gets(g)!=NULL)  
        {  
            for(int i=0;i<3;i++){
                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) 
                printf("0
    ");  
            else if(p<=9-a[0]||p<=b[2])//只有一个数字变换的最大情况
                printf("1
    ");  
            else if(p<=9-a[0]+9-a[1]||p<=b[2]+b[1]||p<=9-a[0]+b[2])//变换两个数字变换有三种情况 两前,两后,一前一后; 
                printf("2
    ");  
            else            //两种情况都不满足 一定是第三种
                printf("3
    ");  
        }  
        return 0;  
    }  
    
  • 相关阅读:
    HTML 之轮播图
    Django 框架之 URL
    Django 框架搭建入门案例
    Python 操作 MySQL
    Python 之RabbitMQ使用
    Python 之协程
    Python 之网络编程
    Python 序列化
    Python 装饰器
    python(3)-内置函数
  • 原文地址:https://www.cnblogs.com/bbqub/p/7422293.html
Copyright © 2011-2022 走看看