cf Educational Codeforces Round 27 B. Luba And The Ticket

编程入门 行业动态 更新时间:2024-10-08 19:51:24

cf  Educational <a href=https://www.elefans.com/category/jswz/34/1770097.html style=Codeforces Round 27 B. Luba And The Ticket"/>

cf Educational Codeforces Round 27 B. Luba And The Ticket

原题:
B. Luba And The Ticket
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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个数字。
可以更改任意一位数,且能改成任何数。问你最少更改多少次能使得前3个加上后3个相等。

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;int main()
{ios::sync_with_stdio(false);string s;while (cin >> s){int fi1[3], se1[3], fi2[3], se2[3];for (int i = 0; i<3; i++)fi1[i] = s[i] - '0';for (int i = 3; i<6; i++)se1[i - 3] = s[i] - '0';int tf1 = 0, ts1 = 0, tf2 = 0, ts2 = 0;for (int i = 0; i<3; i++){tf2 = tf1 += fi1[i];ts2 = ts1 += se1[i];}if (tf1>ts1){swap(tf1, ts1);swap(tf2, ts2);for (int i = 0; i < 3; i++){swap(fi1[i], se1[i]);swap(fi2[i], se2[i]);}}if (tf1 == ts1){cout << 0 << endl;continue;}sort(fi1, fi1 + 3);sort(se1, se1 + 3);for (int i = 0; i<3; i++){fi2[i] = fi1[i];se2[i] = se1[i];}if (ts1 - tf1 + fi1[0]>9){tf1 = tf1 - fi1[0] + 9;fi1[0] = 9;sort(fi1, fi1 + 3);}else{cout << 1 << endl;continue;}if (ts2 - tf2 >se2[2]){ts2 = ts2 - se2[2];se2[2] = 0;sort(se2, se2 + 3);}else{cout << 1 << endl;continue;}if (ts1 - tf1 <= se1[2] || ts2 - tf2 + fi2[0] <= 9){cout << 2 << endl;continue;}if (ts1 - tf1 + fi1[0]>9){tf1 = tf1 - fi1[0] + 9;fi1[0] = 9;sort(fi1, fi1 + 3);}else{cout << 2 << endl;continue;}if (ts2 - tf2 >se2[2]){ts2 = ts2 - se2[2];se2[2] = 0;sort(se2, se2 + 3);}else{cout << 2 << endl;continue;}cout << 3 << endl;}return 0;
}

解答:

只有3位,思路很简单。暴力即可,但是这题要注意有不少坑,杀了不少脑细胞。

设前三个数的和总小于后3个,分别进行两次考虑,第一次考虑更改前3个数中最小的,第二个更改后三个数最大的。迭代下去即可。

更多推荐

cf Educational Codeforces Round 27 B. Luba And The Ticket

本文发布于:2024-02-19 16:29:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1764991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Codeforces   Educational   cf   Ticket   Luba

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!