1. 程式人生 > >洛谷P1042乒乓球(模擬,技巧細心)

洛谷P1042乒乓球(模擬,技巧細心)

etc 題目 col name urn max int for using

題目鏈接:https://www.luogu.org/problemnew/show/P1042

題目簡單,就是模擬。但有兩點需要註意:

1.輸入方法,用while(cin),用錯輸入方法一直錯。

2.到11或21分不一定結束!還要滿足分差>=2,不然繼續比下去!

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <iomanip>
 5 #include <cstdio>
 6 #include <cstring>
 7
#include <cmath> 8 using namespace std; 9 typedef long long ll; 10 typedef unsigned long long ull; 11 const int maxn=1e6*8; 12 const int inf=1e9; 13 //char a[maxn]; 14 string a; 15 int main() 16 { 17 ios::sync_with_stdio(false); cin.tie(0); 18 19 /*char c; 20 int p=0; 21 while((c=getchar())!=‘E‘)
22 { 23 if(c==‘\n‘) continue; 24 a[p++]=c; 25 } 26 int len=strlen(a);*///輸入問題,一直錯誤 27 28 char c; 29 while(cin>>c && c!=E) a+=c; 30 int len=a.length(); 31 int p1=0,p2=0; 32 for(int i=0;i<=len-1;i++) 33 { 34 if(a[i]==W) p1++; 35
else p2++; 36 37 if((p1>=11 || p2>=11) && abs(p1-p2)>=2) 38 { 39 cout<<p1<<:<<p2<<endl; 40 p1=0; 41 p2=0; 42 } 43 } 44 cout<<p1<<:<<p2<<endl; 45 46 cout<<endl; 47 48 p1=0;p2=0; 49 for(int i=0;i<=len-1;i++) 50 { 51 if(a[i]==W) p1++; 52 else p2++; 53 54 if((p1>=21 || p2>=21) && abs(p1-p2)>=2) 55 { 56 cout<<p1<<:<<p2<<endl; 57 p1=0; 58 p2=0; 59 } 60 } 61 cout<<p1<<:<<p2<<endl; 62 63 return 0; 64 }

完。

洛谷P1042乒乓球(模擬,技巧細心)