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种,简单判断一下(变一次有一种,变两次有三种)。
- 要使变换次数最少尽量减少后三个中最大的
- 或是补上前三个中最小的(默认前三个的和小于后三个的)
代码
#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的更多相关文章
- codeforeces近日题目小结
题目源自codeforeces的三场contest contest/1043+1055+1076 目前都是solved 6/7,都差了最后一题 简单题: contest/1043/E: 先不考虑m个限 ...
- Codeforeces 13B
计算几何二维基础
- 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 ...
- Codeforeces 707B Bakery(BFS)
B. Bakery time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- CodeForeces 25E (kmp)
E. Test time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputst ...
- CodeForeces 665C Simple Strings
C. Simple Strings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforeces:Mister B and Astronomers分析和实现
题目很长,稍微翻译一下: 外星球每隔T秒中只有一秒可以被观测到,其它T-1秒无法被观测.n个天文学家(分别编号为1,...,n)轮流观测天空1秒,且第i+1个科学家在第i个天文学家后ai+1秒后才执行 ...
- Codeforeces 954C Matrix Walk
题目大意 考虑一个 $x\times y$ 的矩阵 $A_{x\times y}$ ,$A_{i,j} = (i-1)x+y$ . 从矩阵中的某个位置出发,每次可向上下左右移动一步,每到一个位置,记录 ...
- CodeForeces 842d Vitya and Strange Lesson ——(带lazy标记的01字典树)
给一个序列,每次操作对这个序列中的所有数异或一个x,问每次操作完以后整个序列的mex值. 做法是去重后构建01字典树,异或x就是对root加一个x的lazy标志,每次pushDown时如果lazy的这 ...
随机推荐
- XGBoost原理解析
摘要:对xgboost论文中的细节进行记录. 算法原理 系统设计 基于column block的并行 树学习最耗时的部分通常是对数据进行排序,为了降低排序带来的计算负荷,xgb使用基于block的结构 ...
- java设计模式之单例模式你真的会了吗?(懒汉式篇)
java设计模式之单例模式你真的会了吗?(懒汉式篇) 一.什么是单例模式? 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供 ...
- java基础——初识面向对象
面向对象 面向过程&面向对象 面向过程思想 步骤请简单:第一步做什么,第一步做什么 面向过程适合处理一些较为简单的东西 面向对象思想 物以类聚,分类的思维模式,思考的问题首先会解决问题需要哪些 ...
- Linux VMware Tools详解
VMware Tools描述 VMware Tools 中包含一系列服务和模块,可在 VMware 产品中实现多种功能,从而使用户能够更好地管理客户机操作系统,以及与客户机系统进行无缝交互. 在Lin ...
- 使用基于centos7 dbus问题总结
Authorization not available. Check if polkit Authorization not available. Check if polkit service is ...
- ln -s 新目录(最后一个目录新建images) 旧目录(删除最后的images目录)
sudo yum install libvirt virt-install qemu-kvm 默认安装会启用一个NAT模式的bridgevirbr0 启动激活libvirtd服务 systemctl ...
- Linux中find命令用法全汇总,看完就没有不会用的!
Linux中find命令用法全汇总,看完就没有不会用的! 中琦2513 马哥Linux运维 2017-04-10 糖豆贴心提醒,本文阅读时间7分钟 Linux 查找命令是Linux系统中最重要和最 ...
- git cherry-pick(不同分支的提交合并)
git cherry-pick可以选择某一个分支中的一个或几个commit(s)来进行操作.例如,假设我们有个稳定版本的分支,叫v2.0,另外还有个开发版本的分支v3.0,我们不能直接把两个分支合并, ...
- Linux下记录登录用户历史操作
前言:众所周知Linux是一个可以同时让多个用户登录的操作系统,每个用户的操作都影响着Linux运行,除了要做好安全工作以外,防止人为恶意损坏也是很关键的,比如有人恶意执行危险命令,要查找就得记录所有 ...
- redis中AOF和RDB的关闭方法
redis中AOF和RDB的关闭方法 问题:当往redis中导入数据时,有时会出现redis server went away的情况: 原因: 导入的数据量太大,而内存不够(即内存1G,但数据有2 ...