C++實現(xiàn)乒乓球比分判定
本文實例為大家分享了C++實現(xiàn)乒乓球比分判定的具體代碼,供大家參考,具體內(nèi)容如下
編寫程序判斷乒乓球比賽的結(jié)果:輸入雙方比分,輸出誰勝誰負(fù)
此題的難度分3個級別
1、輸入的是一局比賽結(jié)束時的比分;
2、輸入的不僅可能是一局比賽結(jié)束時的比分,還有可能是比賽進(jìn)行過程中的比分;
3、輸入任意兩個非負(fù)整數(shù)
下面選擇第三種難度完成:
#include <iostream> using namespace std; int main() { int player1, player2; cout << "input two scores: " << endl; cin >> player1 >> player2; if (player1 < 0 || player2 < 0) cout << "wrong input" << endl; else { if (player1 == 11 && player2 < 10) cout << "player1 wins" << endl; if (player2 == 11 && player1 < 10) cout << "player2 wins" << endl; if (player1 < 11 && player2 < 11) cout << "not over" << endl; if (player1 > 10 && player2 > 10) { if ((player1 - player2) > 2 || (player2 - player1) > 2) cout << "wrong input" << endl; if ((player1 - player2) == 2) cout << "player1 wins" << endl; if ((player2 - player1) == 2) cout << "player2 wins" << endl; if ((player1 - player2) <= 1 || (player2 - player1) <= 1) cout << "not over" << endl; } } return 0; }
試題分析:
①考察初學(xué)者的邏輯分析;
②考察基本語法if else的熟練程度;
③將日常生活作為程序設(shè)計的載體,寓教于樂;
補(bǔ)充:C++乒乓球比賽代碼
兩個乒乓球隊進(jìn)行比賽,各處三人,甲隊為ABC,乙隊為XYZ,其中A不和X比賽,C不和X,Z,比賽,找出三隊賽手的名單
#include "stdafx.h" #include<iostream> using namespace std; int main() { char i, j, k; for (i = 'X'; i <= 'Z'; i++) { for (j = 'X'; j <= 'Z'; j++) { if (i != j) { for (k = 'X'; k <= 'Z';k++) { if (i !=k&&j != k) { if (i != 'X'&&k != 'X'&& k != 'Z') { cout << "A-----" << i << endl << "B-----" << j << endl<< "C-----" << k << endl; } } } } } } system("pause"); return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C/C++讀寫JSON數(shù)據(jù)的詳細(xì)過程記錄
JSON文件無論是在web開發(fā)、客戶端開發(fā)、服務(wù)端等開發(fā)中都是應(yīng)用比較廣泛的的第一種輕量級數(shù)據(jù)交換格式,非常方便閱讀和編寫,下面這篇文章主要給大家介紹了關(guān)于C/C++讀寫JSON數(shù)據(jù)的詳細(xì)過程,需要的朋友可以參考下2023-04-04詳解c語言中的 strcpy和strncpy字符串函數(shù)使用
strcpy 和strcnpy函數(shù)是字符串復(fù)制函數(shù)。接下來通過本文給大家介紹c語言中的strcpy和strncpy字符串函數(shù)使用,感興趣的朋友跟隨小編要求看看吧2018-10-10C語言左旋轉(zhuǎn)字符串與翻轉(zhuǎn)字符串中單詞順序的方法
這篇文章主要介紹了C語言左旋轉(zhuǎn)字符串與翻轉(zhuǎn)字符串中單詞順序的方法,給出了相關(guān)的兩道算法題目作為例子,需要的朋友可以參考下2016-02-02