题解

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. HashSet添加操作底层判读(Object类型)

    Object类型添加操作判读 第一步:程序首先创建一个Object泛型的Set数组,这里用到了上转型: 第二步:执行object里面的add添加方法,传进的值为"JAVA": 首先 ...

  2. 编译课设·CLion到VS踩坑·解决·备忘录

    应试用,VS使用习惯和JB系差别还是蛮大的 打不过他们就加入他们 键位修改 工具-选项 键盘:改keymap 字体和颜色:宋体必改. 自动恢复:自动保存默认3分钟 CMake:自救时可以看一下 键位名 ...

  3. work2_求交点数

    教学班级:周三上午三四节 项目地址:https://github.com/875571216/- PSP表格 psp2.1 Personal Software Process Stages 预估耗时( ...

  4. stm32开发笔记(二):stm32系列使用V3.5固件库的帮助文件以及GPIO基本功能(一)

    前言   stm32系列是最常用的单片机之一,不同的版本对应除了引脚.外设.频率.容量等'不同之外,其开发的方法是一样的.  本章讲解使用库函数使用GPIO引脚功能.   补充   本文章为多年前学习 ...

  5. foreign key 多对一 多对对 一对一

    使用foreign key 要清除先有哪张表再有哪张表,后表对应前表 例如现有部门再有员工,所以员工对应部门 现有作者后有书,所以书对应作者 现有潜在顾客后有顾客,所以顾客对应潜在顾客 多对多建立3张 ...

  6. jQ的四类基本选择器

    jQuery的四种选择器 jQ选择器与css选择器本质上相差不大,但是在使用容易混淆格式或属性 1.基础选择器 $('#id名') $('.类名') $('.类名1 .类名2') $('标签名.类名' ...

  7. 适用于windows10 Linux子系统的安装管理配置 How To Management Windows Subsystem for Linux WSL

    什么是WSL Windows Subsystem for Linux 简称WLS,适用于Linux的Windows子系统,可以直接在Windows上运行Linux环境(包括大部分命令行工具) Linu ...

  8. [bug] Docker:Error ruuning deviceCreate(createSnapDevice) dm_task_run failed

    原因 删除容器时报错,元信息出错,需要修复 最后一个参数要改成自己docker元信息路径,如: thin_check --clear-needs-check-flag /var/lib/docker/ ...

  9. [刷题] 51 N-Queens

    要求 将n个皇后摆放在n*n的棋盘中,使横.竖和两个对角线方向均不会同时出现两个皇后 思路 尝试在一行中摆放,如果摆不下,回到上一行重新摆放,直到所有行都摆下 快速判断不合法情况 竖向:col[i]表 ...

  10. 攻防世界(十一)warmup

    攻防世界系列 :warmup 1.打开题目,一个贱贱的滑稽表情 F12看到注释内容source.php 2.访问source.php <?php highlight_file(__FILE__) ...