求出错地方

LoopYasa 2018-01-05 03:10:47
/*
*/
#include"stdio.h"
main()
{
int a[50];
int i,k=0,n=1,p,j=0;
for(i=0;i<50;i++)
{
a[i]=n;
n=n+1;
}
while(1)
{
for(i=0;i<50;i++)
{
if(a[i]==0||p!=3)
continue;
else
a[i]=0;

}
for(i=0;i<50;i++)
{
if(a[i]!=0)
k=k+1;
}
if(k==1)
{
j=a[i];
break;
}
p=p+1;
if(p==4)
p=0;
if(j!=0)
break;
}
printf("%d",j);
}
...全文
225 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-01-10
  • 打赏
  • 举报
回复
仅供参考:
//假设有n个人团团围做,从第1个人开始数数,数到第m个人时候,第m个人出列,
//然后继续从1开始数数,数到第m个人退出
#include <stdio.h>
#include <conio.h>
int i,k,t;
int n,m;
static char f[1001];//0该座位未出圈,1该座位已出圈
void main() {
    while (1) {
        printf("Input n m(1000>=n>=m>=1):");
        fflush(stdout);
        rewind(stdin);
        if (2==scanf("%d%d",&n,&m)) {
            if (1000>=n && n>=m && m>=1) break;
        }
    }
    t=0;//已出圈总人数
    i=1;//座位编号
    k=1;//当前要数的数
    while (1) {
        if (0==f[i]) {
            if (m==k) {
                t++;
                f[i]=1;
                printf("%3d ",i);
                if (0==t%10) printf("\n");
                if (t>=n) break;
            }
            k++;if (k>m) k=1;
        }
        i++;if (i>n) i=1;
    }
    cprintf("Press any key ...");
    getch();
}
zcsudden 2018-01-09
  • 打赏
  • 举报
回复
while(1)內第一个for循环內应使用break跳出该循环,否则数组被全部置为0
weixin_41445971 2018-01-06
  • 打赏
  • 举报
回复
你的变量p没有赋初始值
赵4老师 2018-01-05
  • 打赏
  • 举报
回复
引用 3 楼 LoveMyTail 的回复:
这样子看的真是头疼
astyle自动整理c,c++,java源代码格式命令行工具源代码。 用astyle --help查看使用帮助。 http://download.csdn.net/detail/zhao4zhong1/5605415
LoveMyTail 2018-01-05
  • 打赏
  • 举报
回复
这样子看的真是头疼
自信男孩 2018-01-05
  • 打赏
  • 举报
回复
约瑟夫环的问题,用数组模拟约瑟夫环。
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE    50

void init_arr(int *arr, int len);
void joseph(int *arr, int len, int key);

int main(void)
{
    int arr[MAX_SIZE];

    init_arr(arr, MAX_SIZE);
    joseph(arr, MAX_SIZE, 3);
    printf("Last no is %d\n", arr[0]);

    return 0;
}

void init_arr(int *arr, int len)
{
    int i;

    for (i = 0; i < len; i++)
        arr[i] = i + 1;
}

void joseph(int *arr, int len, int key)
{
    int i, j = 0, k;
    int cnt = len;

    for (i = 1; cnt > 1; i++, j++) {
        if (j == cnt)
            j = j % cnt;
        if (i % key == 0) {
            for (k = j; k < cnt; k++)
                arr[k] = arr[k + 1];
            cnt--;
            j--;
        }
    }
}
参考一下吧
LoopYasa 2018-01-05
  • 打赏
  • 举报
回复
有50个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧