当前位置:   article > 正文

cfB. Luba And The Ticket time limit per test2 seconds memory limit per test256 megabytes inputstanda_a. partition time limit per test1 second memory li

a. partition time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output you are given a sequence a consisting of n integers. you may partition this sequence into two sequences b and c in such a way that every
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.


这题没能在2小时内做出来,但是也并不难。

开始的思路就是找规律,如果暴力求解,问题会复杂化。

感觉会有个坑,就是答案为2时,除了18-小的和的两个最小数  以及  大的和的2个最大数 还有 一个打的和最大+9-一个小的和最小。

思路就是判1时 ,当然为 大的和的最大值 或者 小的和的9-最小  为活动范围。这写起来有点麻烦,看代码把

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. int a[220];
  6. int main()
  7. {
  8. int A[3],B[3],a[3],b[3];
  9. for(int i=0;i<6;i++){
  10. char x;
  11. scanf("%c",&x);
  12. if(i<3){ A[i]=x-'0'; }
  13. else B[i-3]=x-'0';
  14. }
  15. int sum1,sum2;
  16. int s1=A[0]+A[1]+A[2];
  17. int s2=B[0]+B[1]+B[2];
  18. if(s1>s2){
  19. b[0]=A[0];
  20. b[1]=A[1];
  21. b[2]=A[2];
  22. a[0]=B[0];
  23. a[1]=B[1];
  24. a[2]=B[2];
  25. sum2=s1;
  26. sum1=s2;
  27. }
  28. else {
  29. b[0]=B[0];
  30. b[1]=B[1];
  31. b[2]=B[2];
  32. a[0]=A[0];
  33. a[1]=A[1];
  34. a[2]=A[2];
  35. sum2=s2;
  36. sum1=s1;
  37. }
  38. sort(a,a+3);
  39. sort(b,b+3);
  40. int tmp=abs(sum1-sum2);
  41. if(tmp==0) printf("0");
  42. else if(tmp<=b[2]||tmp<=(9-a[0])) printf("1");
  43. else if(tmp<=b[2]+b[1]||tmp<=(18-a[0]-a[1])||tmp<=b[2]+9-a[0]) printf("2");
  44. else printf("3");
  45. return 0;
  46. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/71488
推荐阅读
相关标签
  

闽ICP备14008679号