1. 程式人生 > 程式設計 >C++實現乒乓球比分判定

C++實現乒乓球比分判定

本文例項為大家分享了C++實現乒乓球比分判定的具體程式碼,供大家參考,具體內容如下

編寫程式判斷乒乓球比賽的結果:輸入雙方比分,輸出誰勝誰負

此題的難度分3個級別

1、輸入的是一局比賽結束時的比分;
2、輸入的不僅可能是一局比賽結束時的比分,還有可能是比賽進行過程中的比分;
3、輸入任意兩個非負整數

下面選擇第三種難度完成:

#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;
}

試題分析:

①考察初學者的邏輯分析;
②考察基本語法if else的熟練程度;
③將日常生活作為程式設計的載體,寓教於樂;

補充:C++乒乓球比賽程式碼

兩個乒乓球隊進行比賽,各處三人,甲隊為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;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。