出圈问题:100个人围成一桌,从第13个开始走,此后每数13走一人,直至全部走完。

up___lqw 2019-12-08 04:08:28
写的程序运行后不知道为什么会有重复的。就是有人出两次。哪里写错了呢?
#include <stdio.h>
main()
{
int a[100],count=0,i=12,j=12;
for(i=0;i<100;i++)
a[i]=1;
i=12;
do
{
if (a[i]==1) j++;
if (j%13==0)
{ printf("%4d",i+1);
a[i]=0;
count++;
}
i++;
if (i==100)
i=i-100;
}while(count!=100);

}
...全文
74 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
libralibra 2019-12-10
  • 打赏
  • 举报
回复
引用 6 楼 up___lqw 的回复:
[quote=引用 3 楼 libralibra的回复:][quote=引用 2 楼 up___lqw 的回复:]
调整哪个呢?我还是不太清楚。我没看出来哪里不对

#include<stdio.h>
#define NUM 100
#define CNT 13

int main() {
int a[NUM], count = 0, i = 0, j = 0;
for(i=0;i<NUM;++i) a[i] = 1;

i = 0;
do{
// 已经出圈,跳过这个,继续循环,判断下标,需要就归零
if(a[i]==0){
i++;
if(i>=NUM) i = 0;
continue;
}
// 运行到这里说明没出圈,当前计数器加1
j++;
// 如果是第13个,打印出圈,标志归零,总计数器加1,当前计数器归零
if(j%CNT==0){
printf("%4d ",i+1);
a[i] = 0;
count++;
j = 0;
}
// 继续循环,判断下标,需要就归零
i++;
if(i>=NUM) i = 0;
}while(count<NUM);
return 0;
}
[/quote]
程序运行后还是有很多重复的出圈数[/quote]

你自己运行看了吗?哪个数字重复了?
这是我用set检测的输出结果,如果有重复的,set的长度不会是100,输出是True

s = ''' 13
26
39
52
65
78
91
4
18
32
46
60
74
88
2
17
33
48
63
79
94
9
25
42
58
75
92
8
27
44
62
81
98
16
36
55
73
95
14
35
56
77
99
21
43
67
87
11
37
61
85
10
38
66
90
20
49
76
5
34
69
100
30
68
1
40
72
12
51
89
29
80
22
64
15
59
19
71
28
86
50
7
84
54
41
23
3
97
6
31
53
83
47
24
45
82
93
96
57
70 '''
print(len(set([int(line) for line in s.split('\n')]))==100)

遇见女神 2019-12-09
  • 打赏
  • 举报
回复
很简单,当你的代码执行a[i]=0之前,有代码保证a[i]不等于0吗?所以版主说的对。
up___lqw 2019-12-09
  • 打赏
  • 举报
回复
引用 5 楼 遇见女神的回复:
int main(){
int a[100] = {0},count = 100,curr = -1;
while (count) {
for(int i=0;i<13;++i){
do{
curr = (curr+1)%100;
}while(a[curr]);
}
a[curr] = 1;
printf("%d ", curr);
count--;
}
return 0;
}
中间的for 循环没看太懂,你能帮我看下我写的哪错了吗?
up___lqw 2019-12-09
  • 打赏
  • 举报
回复
引用 3 楼 libralibra的回复:
[quote=引用 2 楼 up___lqw 的回复:]
调整哪个呢?我还是不太清楚。我没看出来哪里不对

#include<stdio.h>
#define NUM 100
#define CNT 13

int main() {
int a[NUM], count = 0, i = 0, j = 0;
for(i=0;i<NUM;++i) a[i] = 1;

i = 0;
do{
// 已经出圈,跳过这个,继续循环,判断下标,需要就归零
if(a[i]==0){
i++;
if(i>=NUM) i = 0;
continue;
}
// 运行到这里说明没出圈,当前计数器加1
j++;
// 如果是第13个,打印出圈,标志归零,总计数器加1,当前计数器归零
if(j%CNT==0){
printf("%4d ",i+1);
a[i] = 0;
count++;
j = 0;
}
// 继续循环,判断下标,需要就归零
i++;
if(i>=NUM) i = 0;
}while(count<NUM);
return 0;
}
[/quote] 程序运行后还是有很多重复的出圈数
遇见女神 2019-12-09
  • 打赏
  • 举报
回复
int main(){
int a[100] = {0},count = 100,curr = -1;
while (count) {
for(int i=0;i<13;++i){
do{
curr = (curr+1)%100;
}while(a[curr]);
}
a[curr] = 1;
printf("%d ", curr);
count--;
}
return 0;
}
自信男孩 2019-12-09
  • 打赏
  • 举报
回复
约瑟夫环,建议在网上查查,有很多这样的解决方案:数组/链表~
libralibra 2019-12-09
  • 打赏
  • 举报
回复
引用 2 楼 up___lqw 的回复:
调整哪个呢?我还是不太清楚。我没看出来哪里不对

#include<stdio.h>
#define NUM 100
#define CNT 13

int main() {
int a[NUM], count = 0, i = 0, j = 0;
for(i=0;i<NUM;++i) a[i] = 1;

i = 0;
do{
// 已经出圈,跳过这个,继续循环,判断下标,需要就归零
if(a[i]==0){
i++;
if(i>=NUM) i = 0;
continue;
}
// 运行到这里说明没出圈,当前计数器加1
j++;
// 如果是第13个,打印出圈,标志归零,总计数器加1,当前计数器归零
if(j%CNT==0){
printf("%4d ",i+1);
a[i] = 0;
count++;
j = 0;
}
// 继续循环,判断下标,需要就归零
i++;
if(i>=NUM) i = 0;
}while(count<NUM);
return 0;
}
up___lqw 2019-12-09
  • 打赏
  • 举报
回复
引用 1 楼 Italink的回复:
需要调整一结构哦,不然万一连续两个人都被访问过,你这个就出问题了

#include <stdio.h>
int main()
{
	int a[100], count = 0, i = 12, j = 12;
	for (i = 0; i < 100; i++)
		a[i] = 1;
	i = 12;
	do
	{
		if (a[i] == 1) {
			j++;
			if (j % 13 == 0)
			{
				printf("%4d", i + 1);
				a[i] = 0;
				count++;
			}
		}
			i++;
		if (i == 100)
			i = i - 100;
	} while (count != 100);
	return 0;
}
调整哪个呢?我还是不太清楚。我没看出来哪里不对
Italink 2019-12-08
  • 打赏
  • 举报
回复
需要调整一结构哦,不然万一连续两个人都被访问过,你这个就出问题了

#include <stdio.h>
int main()
{
	int a[100], count = 0, i = 12, j = 12;
	for (i = 0; i < 100; i++)
		a[i] = 1;
	i = 12;
	do
	{
		if (a[i] == 1) {
			j++;
			if (j % 13 == 0)
			{
				printf("%4d", i + 1);
				a[i] = 0;
				count++;
			}
		}
			i++;
		if (i == 100)
			i = i - 100;
	} while (count != 100);
	return 0;
}

69,368

社区成员

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

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