题解

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;
}

codeforeces 845B的更多相关文章

  1. codeforeces近日题目小结

    题目源自codeforeces的三场contest contest/1043+1055+1076 目前都是solved 6/7,都差了最后一题 简单题: contest/1043/E: 先不考虑m个限 ...

  2. Codeforeces 13B

    计算几何二维基础

  3. Codeforeces 617E XOR and Favorite Number(莫队+小技巧)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  4. Codeforeces 707B Bakery(BFS)

    B. Bakery time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  5. CodeForeces 25E (kmp)

    E. Test time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputst ...

  6. CodeForeces 665C Simple Strings

    C. Simple Strings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. codeforeces:Mister B and Astronomers分析和实现

    题目很长,稍微翻译一下: 外星球每隔T秒中只有一秒可以被观测到,其它T-1秒无法被观测.n个天文学家(分别编号为1,...,n)轮流观测天空1秒,且第i+1个科学家在第i个天文学家后ai+1秒后才执行 ...

  8. Codeforeces 954C Matrix Walk

    题目大意 考虑一个 $x\times y$ 的矩阵 $A_{x\times y}$ ,$A_{i,j} = (i-1)x+y$ . 从矩阵中的某个位置出发,每次可向上下左右移动一步,每到一个位置,记录 ...

  9. CodeForeces 842d Vitya and Strange Lesson ——(带lazy标记的01字典树)

    给一个序列,每次操作对这个序列中的所有数异或一个x,问每次操作完以后整个序列的mex值. 做法是去重后构建01字典树,异或x就是对root加一个x的lazy标志,每次pushDown时如果lazy的这 ...

随机推荐

  1. C#是怎么跑起来的

    解释流程前,需要了解一些基本的概念. 基本概念解释: CPU :中央处理器,计算机的大脑,内部由数百万至数亿个晶体管组成,是解释和运行最终转换成机器语言(二进制代码)的地方.机器语言是通过CPU内存的 ...

  2. 6.注册CRT 以及SecureCRT访问

    1.什么是 SecureCRT SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录Unix或Linux服务器主 机的软件. 1)准备工作:安装好Se ...

  3. [DB] ElasticSearch

    安装 root用户解压,修改配置文件 创建新用户es 修改文件权限:chown -R es:es /kkb/install/elasticsearch-6.7.0/ 用es用户启动ElasticSea ...

  4. [转载]XStar's Libvirt+KVM部署记录 目录

    XStar's Libvirt+KVM部署记录 目录 Create: 2013-12-11 Update: 2014-01-03 准备工作 KVM网站 http://sourceforge.net/p ...

  5. IDEA Git 项目实战场景

    实战场景一:上班啦,从远程仓库克隆项目到本地仓库(Clone) 打开 IDEA,在 Check out from Version Control 下拉菜单选择 Git,如下: 在弹出窗口的 URL 地 ...

  6. esp项目,

    http://www.cirmall.com/circuit/6012/%E6%99%BA%E8%83%BD%E5%8C%96%E7%A7%8D%E8%8A%B1%E7%AE%80%E6%98%93% ...

  7. 机器人的运动范围--BFS

    地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] .一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左.右.上.下移动一格(不能移动到方格外),也不能进入行坐标和列 ...

  8. EdgeX Foundry试运行

    EdgeX Foundry试运行 简介 EdgeX Foundry是一个由Linux基金会发起的,且厂商中立的开源IoT边缘计算项目.它可以采集来自多个源的数据,并将这些数据转发到一个中央系统.Edg ...

  9. 使用指定源安装python包

    对于经常需要按照那个python包的同学,外网下载比较慢的话,可以使用公司内部的镜像进行安装 eg: pip install django -i http://mirrors.***.com.cn/p ...

  10. bat脚本总结

    1.修改密码 @echo off set /p b=请输入新密码: net user %username% %b% echo 密码修改成功 %b% 请牢记你的密码 pause 2.删除hosts文件并 ...