TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
C语言趣味程序百例精解之java实现: 70.魔术师猜牌(2)

- //思考方法同第69题
- public class Test70{
- public static void main(String args[]){
- magic70();
- }
- public static void magic70(){
- int Max=27;
- int[] box=new int[Max];//空盒子26个,box[0]不用
- box[1]=1;//初始化第一张牌
- int i,n,j=1;
- for(i=2;i<=26;i++){
- n=1;
- do{
- if(j>26) j=1;//j超过最后一个元素则指向1号盒子
- if(box[j]!=0){//跳过非空的盒子,不进行计数
- j++;
- }else{
- if(n==3) box[j]=i;//若数到第3个空盒子,则将牌放入空盒中
- j++;
- n++;//对空盒计数
- }
- }while(n<=3);
- }
- for(i=1;i<=26;i++){//输出牌的排列顺序
- System.out.printf("%c",box[i]>13?"r":"b");
- System.out.printf("%-4d",box[i]>13?box[i]-13:box[i]);
-
- }
- }
- }
-
- 运行:
- C:at>java Test70
- b1 r6 b10 b2 r12 r3 b3 b11 r9 b4 r7 b12 b5 r4 r13 b6
- b13 r11 b7 r5 r1 b8 r8 r10 b9 r2
复制代码
源码下载:http://file.javaxxz.com/2014/11/21/000507984.zip |
|