| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 738 人关注过本帖
标题:课程设计:猴子选大王,用C编写的,求每行注释~
只看楼主 加入收藏
月月睡不着
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-12-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
课程设计:猴子选大王,用C编写的,求每行注释~
#include <stdio.h>
#include <stdlib.h>

void ysf(int *pG, int n, int last);
int main(int argc, char *argv)
{
    int N = 0;
    int nGroup = 0;
    int N1 = 0, N2 = 0, N3 = 0;
    int *pG1 = 0, *pG2 = 0, *pG3 = 0;
    int *pG4 = 0;
    int N4 = 0;

    int i = 0;
    int i4 = 0;
   
    do
    {
        printf("Input the count of monkey(N >= 9):");
        scanf("%d", &N);
    } while (N < 9);


   
    srand((unsigned int)time(NULL));   
    nGroup = rand() % 3 + 1;

   
    N1 = N / nGroup;

    if (nGroup > 2)  //3
    {
        N2 = N1;         
    }
    else //2
    {
        N2 = N - N1;
         
    }
    N3 = N - N1 -N2;

    printf("Total:%d, devided into  %d groups, count: %d / %d / %d \r\n" , N, nGroup, N1, N2, N3);

    pG1 = (int *)malloc(sizeof(int) * N1);
    if (N2 > 0)
    {
        pG2 = (int *)malloc(sizeof(int) * N2);
    }
    if (N3 > 0)
    {
        pG3 = (int *)malloc(sizeof(int) * N3);
    }

   
    for (i = 0; i < N1; i++)
    {
        pG1[i] = i + 1;
    }
    for (i = 0; i < N2; i++)
    {
        pG2[i] = i + 1 + N1;
    }
    for (i = 0; i < N3; i++)
    {
        pG3[i] = i + 1 + N1 + N2;
    }
 
    ysf(pG1, N1, 3);
    if (N2 > 0)
    {
        ysf(pG2, N2, 3);
    }
    if (N3 > 0)
    {
        ysf(pG3, N3, 3);
    }
   

    //last turn.
   
    N4 = 3;
    if (N2 > 0)
    {
        N4 += 3;
    }
    if (N3 > 0)
    {
        N4 += 3;
    }
    //printf("N4 is %d\r\n", N4);

    pG4 = (int *)malloc(sizeof(int) * N4);

    for (i = 0; i < N1; i++)
    {
        if (pG1[i] > 0)
        {
            pG4[i4++] = pG1[i];
        }
    }

    for (i = 0; i < N2; i++)
    {
        if (pG2[i] > 0)
        {
            pG4[i4++] = pG2[i];
        }
    }

    for (i = 0; i < N3; i++)
    {
        if (pG3[i] > 0)
        {
            pG4[i4++] = pG3[i];
        }
    }

    ysf(pG4, N4, 1);
    for (i = 0; i < N4; i++)
    {
        if (pG4[i] > 0)
        {
            printf("The king is %d\r\n", pG4[i]);
        }
    }

    getchar();
}

void ysf(int *pG, int n, int last)
{   
    int count = n;
    while (count > last)
    {
        int m=0;
        int i = 0;
        while (1)
        {
            if (i == n)
            {
                i = 0;
            }

            if (pG[i] > 0)
            {
                m++;
                if (m == n)
                {
                    pG[i] = 0;   //out
                    count--;
                    //m = 0;
                    break;
                }
            }
            i++;
        }   
    }
}
2015-12-11 11:13
月月睡不着
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-12-11
收藏
得分:0 
up求大神
2015-12-11 11:13
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
怎么一行注释都没有啊?
2015-12-11 12:18
邹阳
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:124
专家分:402
注 册:2015-10-8
收藏
得分:10 
我想问LZ既然出现了srand((unsigned int)time(NULL));这么一句,为何头文件中没有#include<time.h>

I'm a new comer
2015-12-11 12:40
快速回复:课程设计:猴子选大王,用C编写的,求每行注释~
数据加载中...
 
   



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

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