Educational Codeforces Round 27 B. Luba And The Ticket(模拟)

偏执的太偏执、 2022-06-10 14:39 228阅读 0赞

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

  1. 000000

output

  1. 0

input

  1. 123456

output

  1. 2

input

  1. 111000

output

  1. 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位数,问你至少改变多少位才能使得前3位的和等于后三位,直接模拟,我这里t表示每次改变的值,把t排一下序两边差值每次减去t[i]就好了

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<string>
  7. #include<stdio.h>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<vector>
  12. #include<deque>
  13. using namespace std;
  14. #define lson k*2
  15. #define rson k*2+1
  16. #define M (t[k].l+t[k].r)/2
  17. #define INF 1008611111
  18. #define ll long long
  19. #define eps 1e-15
  20. int a[10];
  21. int t[10];
  22. int cmp(int x,int y)//从大到小排序
  23. {
  24. return x>y;
  25. }
  26. int main()
  27. {
  28. int i,j,n,num1,num2;
  29. for(i=0;i<6;i++)
  30. {
  31. scanf("%1d",&a[i]);
  32. }
  33. num1=a[0]+a[1]+a[2];
  34. num2=a[3]+a[4]+a[5];
  35. if(num1==num2)
  36. printf("0\n");
  37. else
  38. {
  39. int ans=0;
  40. sort(a,a+3);//对前三位排序
  41. sort(a+3,a+6);//后三位排序
  42. int d=abs(num1-num2);
  43. if(num1>num2)
  44. {
  45. t[0]=a[0];
  46. t[1]=a[1];
  47. t[2]=a[2];
  48. t[3]=9-a[3];
  49. t[4]=9-a[4];
  50. t[5]=9-a[5];
  51. sort(t,t+6,cmp);//对t从大到小排序,表示每次可以改变的差值
  52. for(i=0;i<6;i++)
  53. {
  54. if(d<=t[i])
  55. {
  56. ans=i+1;
  57. break;
  58. }
  59. else
  60. d-=t[i];
  61. }
  62. }
  63. else
  64. {
  65. t[0]=a[3];
  66. t[1]=a[4];
  67. t[2]=a[5];
  68. t[3]=9-a[0];
  69. t[4]=9-a[1];
  70. t[5]=9-a[2];
  71. sort(t,t+6,cmp);
  72. for(i=0;i<6;i++)
  73. {
  74. if(d<=t[i])
  75. {
  76. ans=i+1;
  77. break;
  78. }
  79. else
  80. d-=t[i];
  81. }
  82. }
  83. printf("%d\n",ans);
  84. }
  85. return 0;
  86. }

发表评论

表情:
评论列表 (有 0 条评论,228人围观)

还没有评论,来说两句吧...

相关阅读