题解

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. app.json文件配置

    pages 页面路径的存放列表 通过手写页面路径可以在对应目录下自动创建对应文件 "pages": [ "pages/demo3/demo3", "p ...

  2. FHE-Toolkit 安装

    什么是FHE-Toolkit? FHE-Toolkit-linux是用于Linux的IBM全同态加密工具包, 该工具包是一个基于Linux的Docker容器,可演示对加密数据的计算而无需解密, 该工具 ...

  3. Pytorch_Part2_数据模块

    VisualPytorch beta发布了! 功能概述:通过可视化拖拽网络层方式搭建模型,可选择不同数据集.损失函数.优化器生成可运行pytorch代码 扩展功能:1. 模型搭建支持模块的嵌套:2. ...

  4. Word·去掉复制粘贴自动添加的空格

    阅文时长 | 0.05分钟 字数统计 | 145.6字符 主要内容 | 1.引言&背景 2.声明与参考资料 『Word·去掉复制粘贴自动添加的空格』 编写人 | SCscHero 编写时间 | ...

  5. 都 2021 年了,竟然有人搞大数据时忽略 JSON 而去研究用 C# 把 XML 转换为 XML 的技术

    在大数据项目开发过程中,ETL(Extract-Transform-Load)是必不可少.即便目前 JSON 非常流行,开发人员也有必定会有对远古系统的挑战,而 XML 格式的数据源作为经典存在浑身上 ...

  6. (原创)高DPI适配经验系列:(四)高DPI适配示例

    一.前言 光说不练假把式. 原理说再多,也不如一个例子直观明了.所以本篇文章就来通过一个例子演示一下高DPI适配的流程. 相信看完的你,一定会有所收获! 本文地址:https://www.cnblog ...

  7. 用源码搭建LNMP环境+部署WordPress

    首先要做的是就是关闭Centos7.4的防火墙及selinux #systemctl stop firewalld #systemctl disable firewalld #sed -ri 's/^ ...

  8. debian配置---->/etc/apt/sources.list apt基本源设置指南

    yum或apt基本源设置指南   关于: 管理Linux服务器的运维或开发人员经常需要安装软件,最常用方式应该是通过Linux系统提供的包管理工具来在线安装,比如centos的yum,ubuntu或d ...

  9. Linux服务之批量部署篇

    批量部署步骤: 1.检查环境 getenforce               #检查内核防火墙是否关闭 systemctl status firewalld       #检查firewalld是否 ...

  10. 90%的人都不知道的Node.js 依赖关系管理(下)

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文参考:https://dzone.com/articles/node-dependency-manage ...