C语言问题,关于循环报数

Yunas 2012-12-07 08:54:03
#include<stdio.h>
main(){
int circle[17]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int call;
int number;
int i;
number=17;
call=1;
while(number>0)
{
for(i=1;i<=17;i++){
if(call%3!=0&&circle[i]!=-1){
call++;
}else if(call%3=0){
circle[i]=-1;
number--;
call++;
}
}
}
for(i=1;i<=17;i++){
while(circle[i]!=-1){
printf("the num is %d",i);
}
}
system("pause");
}
编译失败,显示错误提示是需要逻辑0或非0在main函数中,求解释
...全文
404 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yunas 2012-12-07
  • 打赏
  • 举报
回复
引用 5 楼 baidang201 的回复:
C/C++ code?12345678910111213141516171819202122232425262728293031323334353637int circle[17]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; int call; int number; int i=0; number=16;//出……
if(i>=17) i%=17;这里的i%是什么意思···?
Yunas 2012-12-07
  • 打赏
  • 举报
回复
我照着改了···还是有问题···· 原题是17,编号0到16个人循环报数,从1开始,报到3的倍数的人离开知道剩下最后一个人····问这个人原来的编号是多少···答案是10 我改成这样的代码: #include<stdio.h> main(){ int circle[17]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; int call; int number; int i; number=17; call=1; while(number>0) { for(i=0;i<=16;i++){ if(call%3!=0&&circle[i]!=-1){ call++; }else if(call%3==0){ circle[i]=-1; number--; call++; } } } for(i=1;i<=17;i++){ if(circle[i]!=-1){ printf("the num is %d\n",i); } } system("pause"); } 运行结果还是不对···
剑有偏锋 2012-12-07
  • 打赏
  • 举报
回复

int circle[17]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
   int call;
   int number;
   int i=0;
   number=16;//出队次数
   call=1;//出队循环变量
   while(number>0)
   {
	   if((circle[i]!=-1))//当前元素未出队
	   {
		   if((call%3) == 0)//当前到循环点
		   {
			   circle[i]=-1;			  
			   call++;
			   number--;
		   }
		   else
		   {
			   call++;
		   }
	   }	  
	   
	   i++;
	   if(i>=17)
		   i%=17;
    
   }

   for(i=0;i<17;i++)
   {
      if(circle[i]!=-1)
	  {
        printf("the num is %d\n",i);
      }
   }
 
   system("pause");
更改了你的求解逻辑
breakfisher 2012-12-07
  • 打赏
  • 举报
回复
printf("the num is %d",i);
这个后面最后加个'\n'
printf("the num is %d\n",i);
这样结果很清晰
breakfisher 2012-12-07
  • 打赏
  • 举报
回复
还有几个明显的逻辑错误
for(i=1;i<=17;i++)
这个错误,应该是
for(i=0;i<=16;i++)
程序里面连个for loop都有这个问题
while(circle[i]!=-1)
应该是
if(circle[i]!=-1)
nice_cxf 2012-12-07
  • 打赏
  • 举报
回复
else if(call%3=0){ =应该是== 调用system需要 #include<stdlib.h> main(){ 最好改为int main(){
breakfisher 2012-12-07
  • 打赏
  • 举报
回复
  }else if(call%3=0){
语法错误,应该是
else if(call%3==0)
zhouxf_cn 2012-12-07
  • 打赏
  • 举报
回复
约瑟夫环?

#include"stdio.h"
#include"stdlib.h"
struct node
{
int num;
struct node *next;
};

struct node *head = NULL;
struct node *last = NULL;
void create(int n)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
head = p;
p->num = n;
last = p;
last->next = head;
}
else
{
last->next = p;
p->num = n;
last = p;
last->next = head;
}
// printf("%d %d\n",p->num,head->num);
}

void my_move(struct node *p)
{
struct node *q,*front;
q = head;
front = head;
if(p == head)
{
while(q->next != head)
{
q=q->next;
}
front = p;
q->next = p->next;
head = p->next;
free(front);
}
else
{
while(q!=p)
{
front =q;
q=q->next;
}
if(q->next ==head)
{
front->next = head;
free(q);
}
else
{
front->next = q->next;
free(q);
}
}
}


int main()
{
int n;
int i;
printf("please input the num\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
create(i);
}
printf("create success\n");
struct node *p = head;
while(n!=1)
{
struct node *q;
p = p->next->next;
q = p;
p = p->next;
my_move(q);
n--;
}
printf("the end staty is%d\n",p->num);
return 0;
}

赵4老师 2012-12-07
  • 打赏
  • 举报
回复
//假设有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();
}
breakfisher 2012-12-07
  • 打赏
  • 举报
回复
引用 6 楼 Yunas 的回复:
我照着改了···还是有问题···· 原题是17,编号0到16个人循环报数,从1开始,报到3的倍数的人离开知道剩下最后一个人····问这个人原来的编号是多少···答案是10 我改成这样的代码: #include<stdio.h> main(){ int circle[17]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}……
你代码有点问题, 帮你改了一下:
#include<stdio.h>
int main()
{
  int circle[17]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  int call;
  int number;
  int i;
  number=16;
  call=1;
  while(number>0)
  {
    for(i=0;i<=16;i++){
      if(circle[i] != -1) {
        if(call%3 == 0) {
          circle[i] = -1;
          number--;
        }
        call ++;
      }
    }
  }
  for(i=0;i<=16;i++){
    if(circle[i]!=-1){
      printf("the num is %d\n",i);
    }
  }
  //system("pause");
  return 0;
}

69,371

社区成员

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

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