题解

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\n");
else if(p<=9-a[0]||p<=b[2])//只有一个数字变换的最大情况
printf("1\n");
else if(p<=9-a[0]+9-a[1]||p<=b[2]+b[1]||p<=9-a[0]+b[2])//变换两个数字变换有三种情况 两前,两后,一前一后;
printf("2\n");
else //两种情况都不满足 一定是第三种
printf("3\n");
}
return 0;
}

最新文章

  1. 解决Chrome 下载带半角分号出现net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION的问题
  2. 浅析匿名函数、lambda表达式、闭包(closure)区别与作用
  3. mysql-7 数据检索(5)
  4. flex中image控件source属性改变的例子
  5. css基本设置
  6. MySQL: LEAVE Statement
  7. 关于mapcontrol和pagelayoutcontrol切换时闪退
  8. Webstorm &amp; PhpStorm的序列号和证书
  9. [SAP ABAP开发技术总结]BAPI调用
  10. python(1) - 第一个程序 Hello World!
  11. Android, JSONLIB , java.lang.NoClassDefFoundError: Failed resolution of: Lnet/sf/json/JSONArray; 原因
  12. 工作总结:MFC自写排序算法(升序)
  13. 基于jQuery简单实用的Tabs选项卡插件
  14. 在Linux上使用cmake创建CodeBlocks工程
  15. 【Chromium中文文档】Chromium多进程架构
  16. python pdb调试以及sublime3快捷键设置
  17. redis可视化工具redisClient
  18. 你不知的DOM编程
  19. Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
  20. 滑动和animate以及如何停止动画

热门文章

  1. linux 发送邮件
  2. Unittest框架之测试套件:TestSuite
  3. HDU - 2091 空心三角形 水题,但是有点坑...
  4. maven简单入门
  5. 逆向工程初步160个crackme-------1
  6. Summer——从头开始写一个简易的Spring框架
  7. 『动善时』JMeter基础 — 16、JMeter配置元件【HTTP信息头管理器】
  8. 定义私有属性: *String name; * int age; * String gender; * int salary; Date hiredate;//入职时间
  9. Git安装教程最新版本(国内gitee国外github)
  10. [刷题] 200 Number of Islands