【问题描述】
抽签是我们日常生活中经常遇到的一件事,并且其形式有很多种。这里介绍一种抽签游戏,如图3-2所示,最上面一排是游戏的参加者——称为抽签者,最下面一排是签号(奖品、公差等)。每个人依次顺着竖线往下走,当碰到横线时,即转横向前进,碰到竖线再往下,以此类推,则游戏结束后,抽签者会一一对应到最下面一排的签号。
#include "stdafx.h" #include<iostream> #include<string> #include<iomanip> #define N 6 #define M 8 using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int i = 0; int j = 0; string t; string A[N] = { "张三","李四","王五","赵阳","孟飞","凌墨" }; //抽签者 string P[N] = { "苹果手机一部", "PAID 一部", "100元充值卡", "谢谢抽奖","谢谢抽奖","美女一位" }; //奖品 const int C[M][N - 1] = { { 0, 0, 1, 0,0 }, { 1,0 ,1, 0,1 }, { 1, 0, 1, 0,0 }, { 1, 0, 1,0, 1 }, { 0, 1,0, 0, 1 }, { 0, 1, 0, 1,0 }, { 1, 0,0, 1, 0 }, { 1,0,1, 0, 0 } }; std::cout << "以下是参加抽奖的观众:" << endl; for (j = 0; j < N; j++) { std::cout << setiosflags(ios_base::left) << setw(10) << A[j] << endl;; } std::cout << "以下是今天的全部奖品:" << endl; for (j = 0; j < N; j++) { std::cout << setiosflags(ios_base::left) << setw(10) << P[j]<<endl; } for (i = 0; i < M; i++) //进行交换数据 { for (j = 0; j < N - 1; j++) { if (C[i][j] == 1) { t = A[j]; A[j] = A[j + 1]; A[j + 1] = t; } } } std::cout << "最终抽签者抽到的奖品是:" << endl; for (j = 0; j < N; j++) { std::cout << setiosflags(ios_base::left) << setw(10) << A[j] << setiosflags(ios_base::left) << setw(10) << P[j] << endl;; } std::cout << endl; return 0; }