| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1012 人关注过本帖
标题:[讨论]切磋之技高一筹
只看楼主 加入收藏
三少爷
Rank: 1
等 级:新手上路
帖 子:192
专家分:0
注 册:2004-4-29
收藏
 问题点数:0 回复次数:3 
[讨论]切磋之技高一筹

Simulating of the Shuffling Deck Machine Practice: Simulating of the Shuffling Deck Objectives: To train the student’s ability of integrating all the knowledge from the book to solve the problem. To improve the ability of programming in C and active the interesting at C programming language through this integrated experiment. Contents: This assignment gives students the opportunity to work with arrays and sorting.Programs that simulate card games usually have to simulate the operation of shuffling the deck. Like sorting, shuffling is a process that involves rearranging the elements of an array. Algorithmically, the only difference between sorting and shuffling is how you select elements. When you sort an array using selection sort, you choose the smallest element in the rest of the array on each cycle of the loop. When you shuffle an array, you choose a random element. At last, you should sort the 13 cards in descending order by suit. Steps: Write a function Shuffle that shuffles an array of strings. To test the Shuffle function, write a program that 1. Declares an array with 52 elements, each of which are strings. 2. Fills the elements of that array with strings representing standard playing cards. Each card is represented by a string consisting of a rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) concatenated with a single letter representing a suit (C, D, H, S). Thus, the queen of spades is represented by the string "QS". The function IntegerToString described in Chapter 9 (page 302) will probably come in handy here. 3. Shuffles the array using the Shuffle function. 4. Deals a bridge hand by copying the first 13 cards from the deck to a separate array. 5. Sorts the 13 cards in the hand so that the cards are in descending order by suit. When the hand is sorted, all the spades come first, followed by the hearts, the diamonds, and finally the clubs. Within each suit, the cards should be listed in the order given in step 2: first the ace, then the king, then the queen, and so on down to the two. Note that this step requires an operation that is pretty much the same as the Sort function in the text. The only differences are that (1) the array elements are strings and (2) the comparison operation is slightly more complicated. In all other ways, your program should follow exactly the same structure. 6. Displays the 13 cards in the hand on a single line.

The following is a sample run of the program: Hand: AS 6S AH 10H 7H 4H KD 8D 5D 4D 3D 2D AC 题目虽然不难,但为了发扬精益求精之精神,最大限度发掘此题优质,也总算是为了促进交流,希望藉此日后交流切磋之风更盛,各路学友们互相切磋一下,看看谁的程序算法更优,结构更凑,或者代码更短,或者可读性更好,同时小有奉上100金(无分割)。以下是在下写的,大致算法是 先 排 花色,再各自按花色排序,自知效率非最优,但易于设计。 #include <iostream> #include <algorithm> #include <string> #include "conio.h" using namespace std; string cards[13],elements[52],rank[13]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"},suit[4]={"C","D","H","S"};

bool judgeSmaller(string p,string q) { for( int i=0 ; i<13 ; i++ ) if(rank[i]==p) break; for( int j=0 ; j<13 ; j++ ) if(rank[j]==q) break; if(i<j) return true; else return false; }

int main() { while(getch()!=0x1b) //ESC 退出 { int k=0; for( int i=0 ; i<52 ; i++ ) elements[i]=rank[i%13]+suit[i%4]; random_shuffle(elements,elements+52); for( int j=3 ; j>=0 ; j-- ) for( i=0 ; i<13 ; i++ ) { if(elements[i].substr(elements[i].length()-1,1)==suit[j]&&elements[i]!="0") { cards[k++]=elements[i]; elements[i]="0"; } } for( j=3 ; j>=0 ; j-- ) for(int l=0 ; l<13 ; l++ ) for( i=0 ; i<12-l ; i++ ) { if(cards[i].substr(cards[i].length()-1,1)==suit[j]&&cards[i+1].substr(cards[i+1].length()-1,1)==suit[j]) { if(judgeSmaller(cards[i].substr(0,cards[i].length()-1),cards[i+1].substr(0,cards[i+1].length()-1))) swap(cards[i],cards[i+1]); } } for( i=0 ; i<13 ; i++ ) cout<<cards[i]<<" "; cout<<endl; } return 0; }

搜索更多相关主题的帖子: 技高一筹 
2004-12-29 11:35
三少爷
Rank: 1
等 级:新手上路
帖 子:192
专家分:0
注 册:2004-4-29
收藏
得分:0 
晕啊,这年头是“低手”没人理,还是送钱也没人要了么

2004-12-30 19:31
C++大粉丝
Rank: 4
等 级:贵宾
威 望:10
帖 子:477
专家分:0
注 册:2004-4-23
收藏
得分:0 
呵呵

I am a big fan of c plus plus.
2005-01-02 14:14
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
中文
2005-01-02 15:13
快速回复:[讨论]切磋之技高一筹
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011644 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved