已解决问题

c++的几个问题

[ 标签:c++ ]
注释里有问题:
// Ex_Final_1.cpp : 约瑟夫问题 数组版
// 呆呆蜗牛 aka 天羽云尘

#include <iostream>
using namespace std;

int main( void )
{
const int ARRAY_SIZE = 1000;

// 人数
int number;
int temp;
// 报到skip的人出列
int skip;
// 计数器
int counter = 0;
// 当前位置标记
int loc = -1;

// 是否在圈内标记
bool isInCircle[ ARRAY_SIZE ]; //bool关键字是什么意思?
for ( counter = 0; counter < ARRAY_SIZE; isInCircle[ counter++ ] = true ); //这句话是什么意思?

// 开始输入
cout << "How many people are there in the circle? ";
cin >> number;
temp = number;
cout << "The one counting n will be ejected! Please input n: ";
cin >> skip;
cout << endl;

// 踢人
while ( temp > 1 )
{
counter = 0;
while ( counter < skip )
{
loc = ( loc + 1 ) % number;
if ( isInCircle[ loc ] ) counter++;
}
isInCircle[ loc ] = false; //这里面的false又是什么意思?是表示为0吗?
cout << "Person " << loc + 1 << " is kicked!" << endl;
temp--;
}
cout << endl;

// 判断幸运儿并输出
loc = 0;
while ( !isInCircle[ loc ] ) loc++;

cout << "The lucky dog is Person " << loc + 1 << " !!" << endl;

return 0;
}
匿名 回答:2 人气:11 解决时间:2009-03-30 23:44
  
满意答案
注释里有问题:
// Ex_Final_1.cpp : 约瑟夫问题 数组版
// 呆呆蜗牛 aka 天羽云尘

#include <iostream>
using namespace std;

int main( void )
{
const int ARRAY_SIZE = 1000;

// 人数
int number;
int temp;
// 报到skip的人出列
int skip;
// 计数器
int counter = 0;
// 当前位置标记
int loc = -1;

// 是否在圈内标记
bool isInCircle[ ARRAY_SIZE ]; //bool关键字是什么意思?
//bool,布尔型变量,只有TRUE和FALSE,这里定义bool数组
//在这里TRUE表示人还在圈里,没被踢出去;FALSE表示人被点到,踢出去

for ( counter = 0; counter < ARRAY_SIZE; isInCircle[ counter++ ] = true ); //这句话是什么意思?
//把数组的值全赋为TRUE,表示所有人都还没被踢出去


// 开始输入
cout << "How many people are there in the circle? ";
cin >> number;
temp = number;
cout << "The one counting n will be ejected! Please input n: ";
cin >> skip;
cout << endl;

// 踢人
while ( temp > 1 )
{
counter = 0;
while ( counter < skip )
{
loc = ( loc + 1 ) % number;
if ( isInCircle[ loc ] ) counter++;
}
isInCircle[ loc ] = false; //这里面的false又是什么意思?是表示为0吗?
//当第loc个人被点到,就把数组里第loc个值赋为FALSE,表示被踢出去


cout << "Person " << loc + 1 << " is kicked!" << endl;
temp--;
}
cout << endl;

// 判断幸运儿并输出
loc = 0;
while ( !isInCircle[ loc ] ) loc++;

cout << "The lucky dog is Person " << loc + 1 << " !!" << endl;

return 0;
}
回答采纳率:42.9% 2009-03-30 23:06
评价答案
  • 是否解决问题(参与评价0次)
  • 0
  • 0
  • 0
  • 是否原创答案(参与评价0次)
  • 0
  • 0
满意答案
//bool关键字是什么意思?
答:这个关键字只有真和假之分,就像一个开关一样。
for ( counter = 0; counter < ARRAY_SIZE; isInCircle[ counter++ ] = true ); //这句话是什么意思?
答:对在圈内的人做个循环,如果计数器的值小于用户输入的这个数组值,计数器加上1,并把上面的bool型开关变量赋值为真,接着一下循环,否则跳出循坏。
isInCircle[ loc ] = false; //这里面的false又是什么意思?是表示为0吗?
答:是的。就是值为假时,因为+1取number的模,为真时Counter++,为假是才会输出被踢出的人。
回答采纳率:25.4% 2009-03-30 22:58
评价答案
  • 是否解决问题(参与评价0次)
  • 0
  • 0
  • 0
  • 是否原创答案(参与评价0次)
  • 0
  • 0

相关内容