POJ 2328 Guessing Game(一道讓我想哭的水題!),pojguessing

來源:互聯網
上載者:User

POJ 2328 Guessing Game(一道讓我想哭的水題!),pojguessing

【題意簡述】:就是猜數,我想大家一定會在電視上看過這樣類似的節目。

【分析】:WA到死,本來就應該是一道簡單的水題,但是由於我本人也很水,在控制字元的輸入上有了點小問題,所以做了很久!想的也有點雜,有點多!還是題做得少。

這個是我WA到死的代碼!!

// WA 代碼!! #include<iostream>#include<cstring>using namespace std;int main(){int a;char ans[10];while(1){bool flag = false;int Max  = 100;int Min  = 0;cin>>a;if(a == 0)break;getchar();  // 注意這裡要加上這個! gets(ans);while(1){if(strcmp(ans,"right on") == 0)break;if(strcmp(ans,"too high") == 0&&a<Max) // 這裡的a<Max 要加上! Max = a;if(strcmp(ans,"too low") == 0&&a>Min) // 同樣這裡也是!!Min = a;cin>>a;getchar();  // 注意這裡要加上這個! gets(ans);}if( a < Min || a > Max )printf("Stan is dishonest\n");elseprintf("Stan may be honest\n");}return 0;}



這個是AC代碼:

// AC 代碼!! #include<iostream>#include<cstring>using namespace std;int main(){int a;char ans[10];while(1){bool flag = false;int Max  = 100;int Min  = 0;cin>>a;if(a == 0)break;getchar();  // 注意這裡要加上這個! gets(ans);while(1){if(strcmp(ans,"right on") == 0)break;if(strcmp(ans,"too high") == 0&&a<Max)Max = a;if(strcmp(ans,"too low") == 0&&a>Min)Min = a;cin>>a;getchar();  // 注意這裡要加上這個! gets(ans);}if( a > Min && a < Max )printf("Stan may be honest\n");elseprintf("Stan is dishonest\n");}return 0;}

看到兩個代碼的不同了嗎?其實就只是我的第一個WA的代碼少了個等號,應該是

if( a <= Min || a >= Max )  //  要有等號!!!printf("Stan is dishonest\n");


這種邊界是最容易出錯的地方!!以後要多加註意!




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.