C++新手

ChristineElin 2011-09-22 10:26:11
#include<iostream>

using namespace std;

#define N 100;

void main()
{
int a[N];
int i,j;

for(i=0; i<N; i++)
a[i]=0;

for(i=0; i<N; i++)
{
for(j=i; j<N; j=(2*i-1))
{
a[j]++;
}
}

cout << "the opened lockers are: " << endl;

for(i=0; i<N; i++)
{
if(a[i]%2!=0)
cout << i << endl;
}

}




F:\Visual Studio\01\03.cpp(9) : error C2143: syntax error : missing ']' before ';'
F:\Visual Studio\01\03.cpp(9) : error C2143: syntax error : missing ';' before ']'
F:\Visual Studio\01\03.cpp(12) : error C2146: syntax error : missing ')' before identifier 'i'
F:\Visual Studio\01\03.cpp(12) : error C2059: syntax error : ';'
F:\Visual Studio\01\03.cpp(12) : error C2059: syntax error : ')'
F:\Visual Studio\01\03.cpp(13) : error C2146: syntax error : missing ';' before identifier 'a'
F:\Visual Studio\01\03.cpp(15) : error C2146: syntax error : missing ')' before identifier 'i'
F:\Visual Studio\01\03.cpp(15) : error C2059: syntax error : ';'
F:\Visual Studio\01\03.cpp(15) : error C2059: syntax error : ')'
F:\Visual Studio\01\03.cpp(16) : error C2143: syntax error : missing ';' before '{'
F:\Visual Studio\01\03.cpp(17) : error C2146: syntax error : missing ')' before identifier 'j'
F:\Visual Studio\01\03.cpp(17) : error C2059: syntax error : ';'
F:\Visual Studio\01\03.cpp(17) : error C2059: syntax error : ')'
F:\Visual Studio\01\03.cpp(18) : error C2143: syntax error : missing ';' before '{'
F:\Visual Studio\01\03.cpp(25) : error C2146: syntax error : missing ')' before identifier 'i'
F:\Visual Studio\01\03.cpp(25) : error C2059: syntax error : ';'
F:\Visual Studio\01\03.cpp(25) : error C2059: syntax error : ')'
F:\Visual Studio\01\03.cpp(26) : error C2143: syntax error : missing ';' before '{'
...全文
124 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
ww884203 2011-09-22
  • 打赏
  • 举报
回复
先学会使用代码表现吧,在最上面一行的ABIUSA后面那个,选C/C++然后把代码放在
//这中间
ChristineElin 2011-09-22
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hulin_hulin 的回复:]

每隔i个数加1,j += i;不就是了,写成j=(2*i-1)是何意呢?还有i的下标不能从零开始,否则你第二个for跳不出第一层(i=0)时的循环。
[/Quote]



是第i个位的元素+1
ChristineElin 2011-09-22
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 kk791159796 的回复:]

#include <iostream>
using namespace std;
#define N 100

int main()
{
bool cupboard[N];
for(int i=0;i!=N;i++)
{
cupboard[i] = false;
}

for(int people=1;people!=N+1;people++)
{
for(int c……
[/Quote]




感谢
lasvegas_xiang 2011-09-22
  • 打赏
  • 举报
回复
copy过来没缩进了,自己注意一下缩进吧
lasvegas_xiang 2011-09-22
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
#define N 100

int main()
{
bool cupboard[N];
for(int i=0;i!=N;i++)
{
cupboard[i] = false;
}

for(int people=1;people!=N+1;people++)
{
for(int cupIndex=1;cupIndex!=N+1;cupIndex++)
{
if(cupIndex%people==0)
{
cupboard[cupIndex-1] = !cupboard[cupIndex-1];
}
}
}

for(int j=0;j!=N;j++)
{
if(cupboard[j] == true)
cout<<j<<" ";
}
}
我写的,你试试
hulin_hulin 2011-09-22
  • 打赏
  • 举报
回复
每隔i个数加1,j += i;不就是了,写成j=(2*i-1)是何意呢?还有i的下标不能从零开始,否则你第二个for跳不出第一层(i=0)时的循环。
ChristineElin 2011-09-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 kk791159796 的回复:]

for(i=0; i<N; i++)
{
for(j=i; j<N; j=(2*i-1))
{
a[j]++;
}
}
这个循环是干吗的 显然内层循环不能退出 第一次进入循环 j=0,a[0]++,然后j=(2*0-1)=-1,-1<N又进入循环,j每次都等于-1,j将一直小于N所以不能退出循环 也就不能执行到后面的输出了
[/Quote]



#include<iostream>

using namespace std;

#define N 100

void main()
{
int a[N];
int i,j;

for(i=0; i<N; i++)
a[i]=0;

cout << "the opened lockers are: " << endl;
cout << "1 , ";

for(i=1; i<N; i++)
{
for(j=i; j<N; j++)
{
if(j==(2*i-1))
{
a[j]++;
if(((a[j])%2)!=0)
cout << j+1 << " , " ;
}
}
}

cout << endl;

}



修改过的程序
但输出的都是偶数
ChristineElin 2011-09-22
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 kk791159796 的回复:]

引用 8 楼 christineelin 的回复:

回复7楼:

我是打算每隔i个数就加1
所以就用了这个循环

什么叫每隔i个数+1 能具体点吗 这样说有歧义,叫人怎么帮你
[/Quote]




是存储柜问题:
100个学生开100个柜,第1个学生打开所有柜子,第2个学生从第2个柜开始,每隔两个柜改变柜子的状态,第三个学生从第3个柜开始,每隔3个柜子改变柜子的状态。如此类推,直至第100个学生改变第100个柜的状态。输出状态改变次数为奇数的柜子的编号。
lasvegas_xiang 2011-09-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 christineelin 的回复:]

回复7楼:

我是打算每隔i个数就加1
所以就用了这个循环
[/Quote]
什么叫每隔i个数+1 能具体点吗 这样说有歧义,叫人怎么帮你
AndyZhang 2011-09-22
  • 打赏
  • 举报
回复
#define N 100;
这个不能加分号
ChristineElin 2011-09-22
  • 打赏
  • 举报
回复
回复7楼:

我是打算每隔i个数就加1
所以就用了这个循环
lasvegas_xiang 2011-09-22
  • 打赏
  • 举报
回复
for(i=0; i<N; i++)
{
for(j=i; j<N; j=(2*i-1))
{
a[j]++;
}
}
这个循环是干吗的 显然内层循环不能退出 第一次进入循环 j=0,a[0]++,然后j=(2*0-1)=-1,-1<N又进入循环,j每次都等于-1,j将一直小于N所以不能退出循环 也就不能执行到后面的输出了
ChristineElin 2011-09-22
  • 打赏
  • 举报
回复
追问:这是存物柜问题
不知道程序有什么错误
没有输出
hulin_hulin 2011-09-22
  • 打赏
  • 举报
回复
#define N 100;
分号不要
再编译试试。。。
菜鸟一个 2011-09-22
  • 打赏
  • 举报
回复
同意楼上的~
ChristineElin 2011-09-22
  • 打赏
  • 举报
回复
感谢二楼的同学
就想叫yoko 2011-09-22
  • 打赏
  • 举报
回复
#define N 100
去掉后面的分号
ChristineElin 2011-09-22
  • 打赏
  • 举报
回复
一直看都不知道为什么
都是用英文输入的
也是半角符号
不解
求高手解答
ChristineElin 2011-09-22
  • 打赏
  • 举报
回复
#include<iostream>

using namespace std;

#define N 100

void main()
{
int a[N];
int i,j;

for(i=0; i<N; i++)
a[i]=1;

cout << "the opened lockers are: " << endl;
cout << "1 , ";

for(i=0; i<N; i++)
{
for(j=i+1; j<N; j+=(i+3))
{
a[j]++;

}
}

for(j=1; j<N; j++)
{
if(((a[j])%2)!=0)
cout << (j+1) << " , " ;
}

cout << endl;
}



可以运行得出结果

65,179

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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