Java解题思路
鸡鸭分类问题
http://www.nowcoder.com/questionTerminal/f87616abbac44842b43f24ddd3cf49e8
- 思路
- 交换结果只有两种,鸡左或鸡右,两种同时进行,取其中最小值输出即可
- 代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); char[] all = b.readLine().toCharArray(); int numC=0,numD=0,countC=0,countD=0; for(int i=0;i<all.length;i++) { if(all[i]=='C') { numC++; countD+=numD; } if(all[i]=='D') { numD++; countC+=numC; } } System.out.print(countC>countD?countD:countC);
}
}