Directory
1 Problem Description
2 Solutions
1 problem description Problem Description
Xiao Ming is playing a "coin-flipping" game.
There are a number of coins lined up on the table. We use * to denote the positive side, and O for the opposite side (lowercase letters, not 0).
For example, the possible scenario is: **oo***oooo
If you flip the two coins on the left at the same time, change to: oooo***oooo
Now Xiao Ming's question is: if you know the initial state and to achieve the target State, can only flip the adjacent two coins at a time, then the specific situation, at least how many times to flip?
We agreed: Turn the next two coins called one-step operation, then ask:
Input Format
Two lines of equal length string representing the initial state and the target state to be reached. Length of each line <1000
output Format
An integer that represents the minimum number of operation steps.
Sample Input 1**********
o****o****Sample Output 15Sample Input 2*o**o***o***
*o***o**o***Sample Output 21
2 Solutions
The specific code is as follows:
ImportJava.util.Scanner; Public classMain { Public Static voidReverseChar[] A,inti) {if(A[i] = = ' O ') A[i]= ' * '; Else if(A[i] = = ' * ') A[i]= ' O '; } Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); String A=In.next (); String B=In.next (); intresult = 0; Char[] Arraya =A.tochararray (); Char[] Arrayb =B.tochararray (); for(inti = 0;i < arraya.length-1;i++) { if(Arraya[i]! =Arrayb[i]) {Reverse (Arraya, i); Reverse (Arraya, I+ 1); Result++; }} System.out.println (Result); }}
Algorithmic note _194: Previous questions flipping coins (Java)