zoukankan      html  css  js  c++  java
  • 2746:约瑟夫问题poj

    2746:约瑟夫问题

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述
    约瑟夫问题:有n只猴子,按顺时针方向围成一圈选大王(编号从1到n),从第1号开始报数,一直数到m,数到m的猴子退出圈外,剩下的猴子再接着从1开始报数。就这样,直到圈内只剩下一只猴子时,这个猴子就是猴王,编程求输入n,m后,输出最后猴王的编号。

    输入
    每行是用空格分开的两个整数,第一个是 n, 第二个是 m ( 0 < m,n <=300)。最后一行是:

    0 0

    输出
    对于每行输入数据(最后一行除外),输出数据也是一行,即最后猴王的编号
    样例输入
    6 2
    12 4
    8 3
    0 0
    样例输出
    5
    1
    7
    代码:
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include <stdlib.h>
    using namespace std;
    
    int main()
    {
        int m,n;
        while(true)
        {
            int i,j;
            int count=0;
            cin>>m>>n;
            if(m==0&&n==0)//m,n同时为0的时候退出
                break;
            char *str;
            if((str=(char *)malloc((m+1)*sizeof(char)))==NULL)//判断是否能够分配空间
            {
                printf("failed!
    ");
                exit(0);
            }
            memset(str,'1',(m+1)*sizeof(char));//分配空间,赋初值
            int last=m,ptr=0;//last为计数当前还有多少个没报数的猴子,ptr为移动指针,指向剩下的猴子
            while(last>1)
            {
                ptr++;
                if(str[ptr]=='1')//如果还没报数,则处理
                {
                    count++;
                    if(count==n)//计数到了出列的数字
                    {
                        count=0;//重新计数
                        str[ptr]='0';//数组置零
                        last--;//猴子的数目减少
                    }
                }
                if(ptr==m)//指针移动到了最后一个猴子
                {
                    ptr=0;
                }
            }
            for(int k=1;k<=m;k++)
            {
                if(str[k]=='1')
                {
                    cout<<k<<endl;
                    break;
                }
            }
            free(str);
            str=NULL;
        }
        return 0;
    }
    #include<iostream>
    #include <stdio.h>
    #include<string.h>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
        int m,n;
        while(cin>>m>>n)
        {
            if(m==0&&n==0)
            {
                break;
            }
            char *str;
            int last,ptr,count;
            if ((str=(char *) calloc (m + 1, sizeof(char))) == NULL) //这里判断需小心,是数组的长度
            {
                printf("空间分配失败!
    ");
                exit(-1);
            }
            memset(str,'1',(m+1)*sizeof(char));
            count=ptr=0;
            last=m;
            while(last>1)
            {
                ptr++;
                if(str[ptr]=='1')
                {
                    count++;
                    
                    if(count==n)
                    {
                        count=0;
                        str[ptr]='0';
                        last--;
                    }
                }
                if(ptr==m)
                {
                    ptr=0;
                }
            }
            for(ptr=1;ptr<=m;ptr++)
            {
                if(str[ptr]=='1')
                {
                    cout<<ptr<<endl;
                    break;
                }
            }
            free(str);
            str=NULL;
        }
        return 0;
    }
    View Code
    #include<iostream>
    #include <stdio.h>
    #include<string.h>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
        int m,n;
        while(cin>>m>>n)
        {
            if(m==0&&n==0)
            {
                break;
            }
            /*
            char *str;
            int last,ptr,count;
            if ((str=(char *) calloc (m + 1, sizeof(char))) == NULL) //这里判断需小心,是数组的长度
            {
                printf("空间分配失败!
    ");
                exit(-1);
            }
            memset(str,'1',(m+1)*sizeof(char));
            count=ptr=0;
            last=m;
            while(last>1)
            {
                ptr++;
                if(str[ptr]=='1')
                {
                    count++;
                    
                    if(count==n)
                    {
                        count=0;
                        str[ptr]='0';
                        last--;
                    }
                }
                if(ptr==m)
                {
                    ptr=0;
                }
            }
            for(ptr=1;ptr<=m;ptr++)
            {
                if(str[ptr]=='1')
                {
                    cout<<ptr<<endl;
                    break;
                }
            }
            free(str);
            str=NULL;*/
            int s=0;
            for(int  i=1;i<=m;i++)
            {
    
                s=(s+n)%i;
            }
            cout<<s+1<<endl;
        }
        return 0;
    }
    View Code
    以大多数人努力程度之低,根本轮不到去拼天赋~
  • 相关阅读:
    javascript深入理解js闭包
    js数组与字符串的相互转换方法
    js 将json字符串转换为json对象的方法解析-转
    js、匿名函数、闭包、回调函数
    $.ajax()方法详解
    typeof操作符返回一个字符串,表示未经计算的操作数的类型。
    【转】mysql中文乱码的一点理解
    【转】国外程序员整理的 C++ 资源大全
    【转】CC++代码优化的27个建议
    一起学JUCE之HashMap
  • 原文地址:https://www.cnblogs.com/gcter/p/7346392.html
Copyright © 2011-2022 走看看