zoukankan      html  css  js  c++  java
  • Lucky Conversion(找规律)

    Description

    Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

    Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:

    • replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
    • swap any pair of digits in string a.

    Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.

    Input

    The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.

    Output

    Print on the single line the single number — the minimum number of operations needed to convert string a into string b.

    Sample Input

    Input
    47
    74
    Output
    1
    Input
    774
    744
    Output
    1
    Input
    777
    444
    Output
    3


    题目意思:给你两个字符串a,b,a,b都是由4和7组成的。a串中的4和7可以通过交换位置或者替换(4替换成7,7替换成4),
    问最少多少步就能得到b串。

    解题思路:这算是一道找规律的题了,其实可以这样做,统计a串和b串不同的字符在b串中表现是4还是7,记录一下个数,
    4的个数和7的个数可以相互抵消来表示交换,剩下不能抵消的可以用来替换。抵消的次数加上替换的次数就是需要的步数。


    上代码:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char a[100001],b[100001];
        int k,i,count_7=0,count_4=0,count=0;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        gets(a);
        gets(b);
        k=strlen(a);
        for(i=0; i<k; i++)
        {
            if(a[i]==b[i])
                continue;
            else
            {
                if(b[i]=='7')
                    count_7++;
                else
                    count_4++;
            }
        }
        if(count_7>=count_4)
            count=count_4+count_7-count_4;
        else if(count_7<count_4)
            count=count_7+count_4-count_7;
        printf("%d
    ",count);
        return 0;
    }
    
    
    
    
    
  • 相关阅读:
    A1066 Root of AVL Tree (25 分)
    A1099 Build A Binary Search Tree (30 分)
    A1043 Is It a Binary Search Tree (25 分) ——PA, 24/25, 先记录思路
    A1079; A1090; A1004:一般树遍历
    A1053 Path of Equal Weight (30 分)
    A1086 Tree Traversals Again (25 分)
    A1020 Tree Traversals (25 分)
    A1091 Acute Stroke (30 分)
    A1103 Integer Factorization (30 分)
    A1032 Sharing (25 分)
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/8687175.html
Copyright © 2011-2022 走看看