C++乒乓球比分判定 C++实现乒乓球比分判定
Alex山南水北 人气:0编写程序判断乒乓球比赛的结果:输入双方比分,输出谁胜谁负
此题的难度分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; }
加载全部内容