剪刀、石头、布
#include <bits/stdc++.h>
using namespace std;
void RSP(string p1,string p2){
if(p1 == p2){
cout << "Tie" << endl;
return;
}
if( p1 == "Rock" && p2 == "Scissors"){
cout << "Player1" << endl;
return;
}
if( p1 == "Rock" && p2 == "Paper"){
cout << "Player2" << endl;
return;
}
if( p1 == "Scissors" && p2 == "Rock"){
cout << "Player2" << endl;
return;
}
if( p1 == "Scissors" && p2 == "Paper"){
cout << "Player1" << endl;
return;
}
if( p1 == "Paper" && p2 == "Rock"){
cout << "Player1" << endl;
return;
}
if( p1 == "Paper" && p2 == "Scissors"){
cout << "Player2" << endl;
return;
}
}
int main(){
// "Rock", "Scissors", "Paper"
// RSP("Rock","Scissors");
int n;
cin >> n;
string p1,p2;
while(n--){
cin >> p1>> p2;
RSP(p1,p2);
}
return 0;
}
统计单词长度:
#include <iostream>
using namespace std;
// SymoChan <hnai@qq.com>
int main() {
int tot = 0;
string s ;
while(cin>>s){
tot ++;
if( tot != 1 ) cout << "," ;
cout << s.size();
}
return 0;
}
单词倒序
#include <vector>
#include <iostream>
using namespace std;
// SymoChan <hnai@qq.com>
vector<string> vec;
int main() {
string s ;
while(cin>>s){
vec.push_back(s);
}
int len = vec.size();
for(int i = len -1; i >= 0 ; i--){
cout << vec[i] << " ";
}
return 0;
}
整理药名
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
string s;
cin >> n;
while(n--){
cin >> s;
int len = s.size();
if(s[0] >= 'a' && s[0] <= 'z'){
s[0] = s[0] - 'a' + 'A';
}
for(int i= 1 ; i < len; i++){
if(s[i] >= 'A' && s[i] <= 'Z'){
s[i] = s[i] - 'A' + 'a';
}
}
cout << s << endl;
}
return 0;
}
平衡数
bool phs(int n){
// 判断是否为平衡数
int arr[10] = {0} ;
while(n>0){
arr[ n % 10 ] ++;
n /= 10;
}
for(int i = 0 ; i <= 9 ; i++){
if(arr[i] > 0 ){ // 只循环包含的数
if( arr[i] != i){ // 判断这个数的出现次数
return false;
}
}
}
return true;
}
统计字符数
#include <bits/stdc++.h>
using namespace std;
int main(){
string s ;
cin >> s;
sort(s.begin(),s.end());
// 字符串 结尾 \0
size_t len = s.size(),cnt = 1;
int maxN = -1;
char ch = ' ';
for(int i = 0 ; i < len; i++){
if(s[i] == s[i+1]){
cnt ++;
}else{
// cout << s[i] << " " << cnt << "\n";
if(maxN < cnt){
maxN = cnt;
ch = s[i];
}
cnt = 1;
}
}
cout << ch << " " << maxN;
return 0;
}
出现次数超过一半的数
#include <bits/stdc++.h>
#include <vector>
using namespace std;
vector<int > vec(1,-100);
int main(){
int n ,tmp;
cin >> n;
while(n--){
cin >> tmp;
vec.push_back(tmp);
}
sort(vec.begin(),vec.end()); // 排序
int len = vec.size(),cnt = 1;
// cout << len << endl;
int maxN = -10000;
for(int i = 1; i < len; i++){
if(vec[i] == vec[i-1]){
cnt ++;
}else{
cnt = 1;
}
if(maxN < cnt) maxN = cnt;
if( maxN >= ( len / 2 )){
cout << maxN;
return 0;
}
}
cout << "no";
return 0;
}
白细胞
#include <bits/stdc++.h>
#include <vector>
#include <cmath>
using namespace std;
// vector<double> vec;
double v[305];
int main(){
int n;
cin >> n;
for(int i = 0 ; i < n; i++){
cin >> v[i];
}
sort(v,v+n);
double sum = 0;
for(int i = 1 ; i < n-1; i++){ // 循环忽略头尾
sum += v[i];
}
double ave = sum / (n-2) ; //
double maxX = -1;
// cout << sum << " " << ave << endl;
for(int i = 1 ; i < n-1; i++){ // 循环忽略头尾
double absV = fabs(v[i] - ave);
if(maxX < absV){
maxX = absV;
}
}
printf("%.2f %.2f", ave,maxX);
// cout << ave << " " << maxX ;
// double tmp;
// while(n--){
// cin >> tmp;
// vec.push_back(tmp);
// }
// sort(vec.begin(),vec.end());
// vec.pop_back(); // 删除最后一个
// vec.erase(vec.begin()); // 删除最前面一个
// for(auto x : vec) cout << x << " ";
return 0;
}