• 约瑟芬公主把乔治放在了第三位,对吧


    想起了一个约瑟芬公主的问题。原题是:

    古代有一位国王,他有一个才华出众,美艳绝伦的女儿叫约瑟芬。到了出嫁的年龄求婚者络绎不绝为了确定女儿应该嫁给谁国王决定通过一种传统的仪式来进行选择。
    仪式是这样的:先由公主从求婚者中选出10人然后让他们站成一圈,公主选一个人作为起点 
    并按顺时针方向逐个数到17(公主的年龄)然后再从1数到17每次在17那个位置的人都要被淘汰。
    可是公主已经喜欢上了英俊的王子乔治,怎样才能让最后留下的是乔治呢?公主拿出10个金币围成一圈试了又试终于明白了并且如愿以偿!
    问:公主是如何如愿以偿的呢 ???

    本来在写C程序,突然想起了这个问题。就查找资料,去尝试解决了他。(在ubuntu 下用的Geany)

    1.C语言代码

    View Code
    /*
     * BaoShu.c
     * 约瑟芬问题
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    //声明结构体
    struct ele {
        int no;
        struct ele * link;
    };
    
    //声明函数
    void game();
    
    int main(int argc, char **argv)
    {
        game();
        return 0;
    }
    
    void game()
    {
        int n, m, i;
        struct ele *h, *u, *p;
        
        system("clear");
        printf("Please input n&m:\n");
        scanf("%d%d",&n,&m);
        
        h = u = (struct ele *)malloc(sizeof(struct ele));
        h -> no = 1;//第一个数
        //一个圈
        for (i = 2; i <= n; ++i)
        {
            u -> link = (struct ele *)malloc(sizeof(struct ele));
            u = u -> link;
            u -> no = i;
        }
        
        //指向头节点
        u -> link = h;
        puts("\nThe numbers of who will quit the cycle in the turn are:");
        
        //通过循环来处理
        while(n) {
            //找到该同学的前一个同学
            for (i = 1; i < m; i++) {
                u = u -> link;
            }
            p = u -> link;//要出列的同学
            u -> link = p -> link;
            printf("%4d",p -> no);
            free(p);//释放节点
            --n;//节点少一个
        }
        
        printf("\n\n Press any key to quit...\n");
        getchar();
    }

    2.Java代码(我比较喜欢用Java,但最近在复习C)

    View Code
    /*
     * BaoShu.java
     * 
     */
    import java.util.Scanner;
    
    public class BaoShu {
        
        //变量
        int n,m;
        INode node,nodeHead,nodeTemp;
        
        public static void main (String args[]) {
            System.out.printf("Hello,World,I am in Linux\n");
            //INode.doGame();
            new BaoShu().doGame();
        }
        
        public void doGame(){
            
            System.out.println("Please input n&m:");
            Scanner s = new Scanner(System.in);
            
            n = s.nextInt();
            m = s.nextInt();
            
            System.out.println("n = " + n + "  m = " + m);
            
            nodeHead = node = new INode();
            nodeHead.no = 1;
            
            //make the inode be a circle with n
            for (int i = 2; i <= n; ++i){
                node.inode = new INode();
                node = node.inode;
                node.no = i;
            }
            
            node.inode = nodeHead;
            
            System.out.println("All the Node are:");
            
            //for(int k = 1; k <= n; ++k) {
            //    node = node.inode;
            //    System.out.printf("%4d", node.no);
            //}    
                
            System.out.println("\nThe Numbers of who will quit....");
            //此时,node 表示一个最后一个节点
            while(n>0)
            {
                for (int j = 1; j < m; ++j) {
                    node = node.inode;//从第一个节点,开始寻找,找到目标节点的上一个节点
                    
                }
                //找到后
                nodeTemp = node.inode;
                node.inode = nodeTemp.inode;
                //打印
                System.out.printf("%4d",nodeTemp.no);
                if (n == 1)
                    System.out.println("\n\n n= "+nodeTemp.no);
                --n;//节点少一个
                
            }
            
        }
    }
    
    
    //封装一个节点类
     class INode {
        
        public int no;
        INode inode;
    }
  • 相关阅读:
    CSS Grid 网格布局全解析
    VSCode插件和首选项配置
    mybatis分页实现原理
    org.apache.ibatis.exceptions.PersistenceException解决办法
    使用ssm框架在使用id查询时应注意的问题
    出现Caused by: org.apache.ibatis.executor.ExecutorException: No constructor found in com.duowenjia.bean.StudentInfo matching [java.lang.Integer, java.lang.String, java.lang.String, java.lang.String]的问题
    如何实现每个页面都判断session
    count(*),count(常量),count(列名)的区别
    json的jsonarray是有区别的
    毕业设计(高校网上作业提交系统)开发记录(20)
  • 原文地址:https://www.cnblogs.com/scherrer/p/2981570.html
Copyright © 2020-2023  润新知