B. Luba And The Ticket
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
input
000000
output
0
input
123456
output
2
input
111000
output
1
Note

In the first example the ticket is already lucky, so the answer is 0.

In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required.

In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.

题意:

对于给出的长度为6的整数串,求最小改变多少个整数可以使前三个数之和等于后三个数。

起初直接暴力写太复杂了,可以先求出前后的和s1,s2 。将较小的一边三个数变为9-a[i],即将它们全部变为9时差值的变化量,而后排序,从大到小减差值直到差值不大于0即可。

AC代码:

 #include<bits/stdc++.h>
using namespace std; int a[]; int main(){
ios::sync_with_stdio(false);
string s;
cin>>s;
int s1=,s2=;
for(int i=;i<;i++){
a[i]=s[i]-'';
s1+=a[i];
}
for(int i=;i<;i++){
a[i]=s[i]-'';
s2+=a[i];
}
int num=s1-s2;
if(num>){
for(int i=;i<;i++){
a[i]=-a[i];
}
}
else{
for(int i=;i<;i++){
a[i]=-a[i];
}
num=-num;
}
sort(a,a+);
for(int i=;i>;i--){
if(num<=){
cout<<-i<<endl;
break;
}
num-=a[i];
}
return ;
}

最新文章

  1. sql 比模糊查询速度快的查询方法
  2. AngularJS学习之全局API(应用程序编程接口)
  3. 集成学习(Ensembling Learning)
  4. 百度网页搜索部来自Console的招聘信息
  5. codeforces 260 div2 B题
  6. Json部分知识(前台显示格式、Json-lib日期处理)
  7. 计算textView的高度
  8. C#实现DirectShow技术开发准备
  9. URL, URI, URN三者区别
  10. [Swift]LeetCode687. 最长同值路径 | Longest Univalue Path
  11. pytho命名规范
  12. SQL手工注入入门级笔记(更新中)
  13. Oracle&#160;启动实例(instance)、打开数据库
  14. H5公共样式,用于所有H5开发页面
  15. notepad++查看二进制文件
  16. mysqli_query数据库有数据,查不出来
  17. jquery对象与dom对象之间互相转换的方法
  18. 2016年终总结--一个Python程序猿的跨界之旅
  19. 移动端H5页面注意事项
  20. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)

热门文章

  1. 仿易讯clientloading效果
  2. EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:bootstrap弹窗功能的实现
  3. protect,internal的区别
  4. HDFS relaxes a few POSIX requirements to enable streaming access to file system data
  5. 【题解】Codeforces 961G Partitions
  6. client网络优化方法
  7. imagick图片压缩。
  8. Java for LeetCode 130 Surrounded Regions
  9. 使用urllib2打开网页的三种方法(Python2)
  10. 升级pip3的正确姿势