<Blue Bridge Cup> 2013 4th Zhenti [Flip the coin] (C language)

Title description

Xiao Ming is playing a "coin flip" game.
There are several coins in a row on the table. We use * to represent the front and o to represent the back (lowercase letters, not zeros).
For example, the possible situations are:Insert picture description here

If you flip the two coins on the left at the same time, it becomes:

Insert picture description here

Now Xiaoming’s question is: If the initial state and the target state to be reached are known, and only two adjacent coins can be flipped at the same time at a time, how many times is the least flipped for a particular situation?

We agree: Turning two adjacent coins is called a one-step operation.

Input format
Two lines of equal length character strings respectively represent the initial state and the target state to be reached. (The length of each line <1000)

Output format
An integer representing the minimum number of operation steps.

Sample input Insert picture description here
Sample output
1

Thinking analysis

Define two strings by comparing and replacing them so that they become two identical strings.
Add a counter to get the result

  • The source code is as follows
#include<stdio.h>
#include<string.h>
int main()
{
    
    
	int cnt = 0;
	char s1[1000];
	char s2[1000];
	scanf("%s%s", s1, s2);
	for(int i = 0; i < strlen(s1); i++){
    
    
		if(s1[i] != s2[i]){
    
    
			if(s2[i+1] == '*')
			s2[i+1] = 'o';
			else if(s2[i+1] == 'o')
			s2[i+1] = '*';
			cnt ++;
		}
	}
	printf("%d", cnt);
	return 0;
}

problem solved.

Guess you like

Origin blog.csdn.net/qq_52760982/article/details/112581064