Codeforces

来源:互联网 发布:linux informix 目录 编辑:程序博客网 时间:2024/05/16 11:03

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个数字,求换多少次才能使前三个和等于后三个和。


解题思路:水题,这里给出搜索的做法……之前做搜索比较多,看到什么都想搜。实际上这题有更简单的做法,可以直接模拟。



#include <iostream>#include <stdio.h>#include <stdlib.h>#include <algorithm>#include <string.h>#include <math.h>#include <vector>#include <set>#include <map>#include <queue>#include <stack>#define ll long longusing namespace std;string str;vector<int> a;map<string,int> vis;struct point{    string a;    int step;    point(string aa,int b){        a=aa;        step=b;    }};int bfs(){    queue<point> que;    que.push(point(str,0));    vis[str]=1;    while(!que.empty()){        point tp=que.front();        que.pop();        int fs=tp.a[0]-'0'+tp.a[1]-'0'+tp.a[2]-'0';        int bs=tp.a[3]-'0'+tp.a[4]-'0'+tp.a[5]-'0';        if(fs==bs)            return tp.step;        for(int i=0;i<7;i++)//枚举替换6个数字            for(int j=0;j<10;j++){//枚举替换成9种                string temp=tp.a;                temp[i]='0'+j;                if(vis[temp]==0)//如果没被访问过                    que.push(point(temp,tp.step+1));            }    }}int main(){    cin>>str;    cout<<bfs()<<endl;    return 0;}












原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 7个月胎位不正怎么办 胎心监护老不过怎么办 8个月了胎位不正怎么办 怀孕八个月了胎位不正怎么办 怀孕八个月胎位不正怎么办 欠债的人跑了怎么办 赌博输了100万怎么办 我赌博输了4万怎么办 办80张信用卡怎么办的 19岁负债十几万怎么办 欠了十几万该怎么办 网贷负债十几万怎么办 赌博欠了十几万怎么办 欠了网贷跑了会怎么办 欠银行钱还不起怎么办 欠小额贷款公司的钱还不上怎么办 负债30万没工作怎么办 华为p9耗电太快怎么办 酷派手机反应慢怎么办 网上买手机被骗了怎么办 买手机贵了怎么办12315 在手机店被骗了怎么办 在转转上被骗了怎么办 微信被骗了800怎么办 苹果6被偷走该怎么办 苹果7被偷走该怎么办 月经量突然少了怎么办 我月经老是不来怎么办 月经不来3个月怎么办 月经推迟3个月怎么办 乙肝五项25阳怎么办 乙肝五项25阳性怎么办 流浓鼻涕怎么办速效办法 鼻涕又浓又黄绿怎么办 玉露已经徒长了怎么办 多肉长出了多头怎么办 多肉已经徒长了怎么办 脖子抻筋了 很疼怎么办 声带肥大造成说话声音沙哑怎么办 轻微的骨性龅牙怎么办 19岁没了晨勃怎么办