输入包括一个字符串S,字符串长度length(3 ≤ length ≤ 50),其中只包含'W'和'B'两种字符串,分别表示白色和黑色。整个字符串表示卡片序列的初始状态。
输出一个整数,表示牛牛最多需要翻转的次数。
BBBW
1
由题意得,要得到交替排列形式,只有BWBWBW....或者WBWBWB...两种可能。 只需要用两个变量记录得到这两种形式串所需要的步数即可,输出最小值 import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String line = scanner.next(); int b = 0, w = 0; for(int i=0, len = line.length();i<len;i++){ char c = line.charAt(i); if((i&1)==0){ if(c=='W') b++; else w++; }else{ if(c=='W') w++; else b++; } } System.out.println(Math.min(b, w)); } }
#include<stdio.h>intmain(){chara[50];intcount = 0;scanf("%s",a);if(sizeof(a)/2<3|| sizeof(a)/2>50){printf("ERROR");return0;}inti = 0;intwCount1 = 0;intwCount2 = 0;while(a[i] != '\0'){if(i % 2== 0) {if(a[i] == 'W') {wCount1++;}} else{if(a[i] == 'W') {wCount2++;}}i++;}intwFlag = (wCount1 > wCount2 ? wCount1 : wCount2) > ((i - wCount1) > (i - wCount2) ? (i - wCount1) : (i - wCount2)) ? 1: 0;intflag = 1;if(wFlag) {flag = wCount1 > wCount2 ? 1: 0;} else{flag = (i - wCount1) > (i - wCount2) ? 1: 0;}intj = 0;while(a[j] != '\0') {if(flag) {if(wFlag) {if(j % 2== 0&& a[j] == 'B') {count++;} elseif(j % 2!= 0&& a[j] == 'W') {count++;}} else{if(j % 2== 0&& a[j] == 'W') {count++;} elseif(j % 2!= 0&& a[j] == 'B') {count++;}}} else{if(wFlag) {if(j % 2!= 0&& a[j] == 'B') {count++;} elseif(j % 2== 0&& a[j] == 'W') {count++;}} else{if(j % 2!= 0&& a[j] == 'W') {count++;} elseif(j % 2== 0&& a[j] == 'B') {count++;}}}j++;}printf("%d",count);return0;}
#include <iostream>
#include <string>
using namespace std;
int main( int argc, const char * argv[]) {
string str;
int length;
int count1 = 0;
int count2 = 0;
cin >>str;
length = ( int )str. length ();
if (length < 3 || length > 50){
cout <<0<< endl ;
return 0;
}
for ( int i=0; i<length; i++) {
if (i%2 == 0) {
if (str[ i ] != 'B') {
count1++;
} else {
count2++;
}
} else {
if (str[ i ] != 'W') {
count1++;
} else {
count2++;
}
}
}
if (count1 > count2) {
cout <<count2<< endl ;
} else {
cout <<count1<< endl ;
}
return 0;
}
#include<bits/stdc++.h>typedeflonglongll;usingnamespacestd;constintinf = 0x3f3f3f3f;constll INF = (ll)inf*inf;constintmaxn = 55;intmain(){string str;cin>>str;intlen=str.size();string t=str;intcnt=0;for(inti=1;i<len;i++){if(str[i]==str[i-1]){if(str[i-1]=='B')str[i]='W';elsestr[i]='B';cnt++;}}intMin=cnt;cnt=1;if(t[0]=='W')t[0]='B';elset[0]='W';for(inti=1;i<len;i++){if(t[i]==t[i-1]){if(t[i-1]=='B')t[i]='W';elset[i]='B';cnt++;}}Min=min(Min,cnt);cout<<Min<<endl;return0;}
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String string = ""; while ((string = bufferedReader.readLine()) != null) { System.out.println(getMaxBlackWhiteCounts(string)); } } public static int getMaxBlackWhiteCounts(String string) { int countBW = 0; int countWB = 0; char[] chars = string.toCharArray(); for (int i = 0; i < chars.length; i++) { char current = chars[i]; if (i % 2 == 0) { if (current == 'B') { continue; } else { countBW++; } } else { if (current == 'W') { continue; } else { countBW++; } } } for (int i = 0; i < chars.length; i++) { char current = chars[i]; if (i % 2 == 0) { if (current == 'W') { continue; } else { countWB++; } } else { if (current == 'B') { continue; } else { countWB++; } } } return Math.min(countBW, countWB); } }人笨代码多,但是我觉得这个够清楚。。
#include<iostream> #include<string> #include<algorithm> using namespace std; int cnt(string x,string y){ int res=0; for(int i=0;i<x.length();i++) if(x[i]!=y[i]) res++; return res; } int main(){ string x,s1="B",s2="W"; cin>>x; for(int i=1;i<x.length();i++) s1+=(s1[i-1]=='B'?'W':'B'), s2+=(s2[i-1]=='B'?'W':'B'); printf("%d\n",min(cnt(x,s1),cnt(x,s2))); }
import java.util.Scanner; public class Practice3 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String str = sc.next(); System.out.println(function1(str)); } }//要得到交替排列形式,只有BWBWBW....或者WBWBWB...两种可能。//只需要用两个变量记录得到这两种形式串所需要的步数即可,输出最小值static int function1(String str) { char c[] = str.toCharArray(); char c1[] = new char[100],c2[] = new char[100]; int count1 = 0; int count2 = 0; int count; for(int i = 0;i < c.length;i++) { c1[i] = i%2 == 0 ? 'W' : 'B'; c1[i] = i%2 == 0 ? 'B' : 'W'; } for(int i = 0;i < c.length;i++) { if(c[i] != c1[i]) { count1++; } if(c[i] != c2[i]) { count2++; } } count = Math.min(count1, count2); return count; } }
//根据最高票的答案,先生成“BWBWBW。。。”和“WBWBWB。。。”,再与原字符串进行比较 //测试过,能用 #include <iostream> #include <string> using namespace std; string create(string str, int n) { string::iterator pStr; n--; while(n--) { //先输入一个空字符 str.push_back('?'); //string的长度变化了,原来的迭代器需要更新 pStr = str.end(); if(*(pStr - 2) == 'B') { *(pStr - 1) = 'W'; } else { *(pStr - 1) = 'B'; } } return str; } int fun(string str) { int cnt1 = 0, cnt2 = 0; int n = str.size(); string str1, str2; string::iterator pStr1, pStr2; str1.push_back('B'); str2.push_back('W'); str1 = create(str1, n); str2 = create(str2, n); pStr1 = str.begin(); pStr2 = str1.begin(); while(pStr1 != str.end()) { if(*pStr1 != *pStr2) { cnt1++; } pStr1++; pStr2++; } pStr1 = str.begin(); pStr2 = str2.begin(); while(pStr1 != str.end()) { if(*pStr1 != *pStr2) { cnt2++; } pStr1++; pStr2++; } if(cnt1 < cnt2) { return cnt1; } else { return cnt2; } } int main(int argc, char * argv[]) { string str; cin >> str; cout << fun(str) << endl; return 0; }
#include<iostream>#include<string>
//两种情况bwbwbw或者wbwbwb #include <iostream> #include <string> using namespace std; int main() { string arr; getline(cin, arr, '\n'); int len = arr.size(); int count1 = 0; int count2 = 0; for(int i=0; i < len; i++) { if(i%2 == 0) { if(arr[i] != 'B') count1++; else count2++; } else { if(arr[i] != 'W') count1++; else count2++; } } if(count1 > count2) cout<<count2<<endl; else cout<<count1<<endl; return 0; }
两种情况#include<iostream>#include<string>using namespace std;intmain(){string s_in;while(getline(cin,s_in)){string s_out1 = s_in;string s_out2 = s_in;intcount =0;intcountm = 0;for(inti=0; i<s_in.size(); i=i+2){s_out1[i] = 'B';s_out2[i] = 'W';}for(inti=1; i<s_in.size(); i=i+2){s_out1[i] = 'W';s_out2[i] = 'B';}for(inti=0; i<s_in.size(); i++){if(s_in[i] != s_out1[i])count++;if(s_in[i] != s_out2[i])countm++;}cout<<(count<countm?count:countm)<<endl;}return0;}
import java.util.Scanner; public class Main1 { public static void main(String[] args) { //获取输入 Scanner scanner = new Scanner(System.in); //读取字符串 String value = scanner.nextLine(); //记录反转次数 int max = 0; //偶数位置为黑色的个数 int evenBlack = 0; //偶数位置为白色的个数 int evenWhite = 0; //奇数位置为白色的个数 int oddWhite = 0; //奇数位置为黑色的个数 int oddBlack = 0; for (int i = 0; i < value.length(); i++) { if ((i % 2 == 0) && (value.charAt(i) == 'W')) { evenWhite++; continue; } if ((i % 2 == 0) && (value.charAt(i) == 'B')) { evenBlack++; continue; } if ((i % 2 == 1) && (value.charAt(i) == 'W')) { oddWhite++; continue; } if ((i % 2 == 1) && (value.charAt(i) == 'B')) { oddBlack++; continue; } } //包含两个战略 //分别是将偶数位置的黑色换为白色,奇数位置的白色换为黑色 int temp1 = evenBlack + oddWhite; //将奇数位置的黑色换为白色,偶数位置的白色换为黑色 int temp2 = evenWhite + oddBlack; System.out.println(temp1 > temp2 ? temp2 : temp1); } }
import java.util.*; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); while(s.hasNext()){ String line = s.nextLine(); int EventWhite=0,EventBlack=0,OddWhite=0,OddBlack=0; //分别表示偶数白黑,奇数白黑; for(int i=0, len = line.length();i<len;i++){ char c = line.charAt(i); /* *将对应改成WBWB或者BWBW的最小次数即可; */ if((i%2)==0){ if(c=='W') EventWhite++; else EventBlack++; }else{ if(c=='W') OddWhite++; else OddBlack++; } } int res=(EventWhite+OddBlack>EventBlack+OddWhite)?EventBlack+OddWhite:EventWhite+OddBlack; System.out.println(res); } s.close(); } }
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); String str = scanner.next(); int wsl = 0; int bsl = 0; int count; for(int i = 0; i < str.length(); i++) { int s = i % 2; if(s == 0 && str.charAt(i)!= 'W') { wsl++; } if(s != 0 && str.charAt(i) != 'B'){ wsl++; } if(s == 0 && str.charAt(i) != 'B'){ bsl++; } if(s != 0 && str.charAt(i) != 'W'){ bsl++; } } count = Math.min(wsl, bsl); System.out.println(count); } }
}
不知道哪里错了,求大神解答下 import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String str = sc.next(); int num = str.length(); String[] str1 = str.split(""); List<String> list1 = new ArrayList<>(); List<String> list2 = new ArrayList<>(); for (int i = 0; i < num; i++) { if (i % 2 == 0) { list1.add("W"); list2.add("B"); } else { list1.add("B"); list2.add("W"); } } int j = 0; int k = 0; for (int i = 0; i < num; i++) { if (str1[i].equals(list1.get(i))) { j++; } if (str1[i].equals(list2.get(i))) { k++; } } if(j<k){ System.out.println(j); }else{ System.out.println(k); } } } } 您的代码已保存答案错误:您提交的程序没有通过所有的测试用例
两种结果“WBWBWB……”,“BWBWBW……”,检验两种结果的最短次数 x = input()