忽略用于气泡排序的 if 语句
Ignoring if statement used for bubble sort
srand(time(NULL));
int rand = Randomiser();
temp = placement[49];
//Loops variables in array to produce scrolling
for(i = 0;i<50; i++)
{
placement[49-i] = placement[49-(i+1)];
}
if(temp==2)
{
temp=temp2;
}
if(rand>90)
{
temp2=temp;
temp=2;
}
placement[0]=temp;
所以我把它改成这样,我的值现在入了,但问题是这个值一次插入了大约 12 次。所以 6 个展示位置 = 2
下面的代码是你真正想要的吗?在此示例中,i环旋转环。请注意,通常您不会移动所有元素,而只是通过(start+i)%size
移动环的起点和地址。
请注意,对于排序,您有std::sort
它比气泡排序更有效。
#include <cstdlib>
#include <vector>
#include <iostream>
int Randomiser() {
return rand() % 101;
}
int main() {
srand(time(NULL));
std::vector<int> placement(50,0);
for(int j = 0; j<100; j++)
{
int temp, temp2;
int rand = Randomiser();
bool controlRod=true;
if(rand<80)
{
temp2 = placement[49];
placement[49]=2;
}
temp = placement[49];
for(int i = 0; i<50; i++)
{
placement[49-i] = placement[49-(i+1)];
}
placement[0] = temp;
}
std::cout << "nThis time in placement[0]: " << placement[0] << "n";
}
/**
Local Variables:
compile-command: "g++ -g test.cc -o test.exe; ./test.exe"
End:
*/
它不会忽略 if
语句,它只是在内部for
循环的第一次迭代后计算为 false。您的代码:
for( ... )
{
bool controlRod = true;
for( ... )
{
if( ... && controlRod == true )
{
// ...
}
// ...
controlRod = false; // <-- set to false so condition is always false
}
}
如果我们展开内部循环,我们会得到如下内容:
for( ... )
{
bool controlRod = true;
// iteration 1 ----------------------------------------------------
if( ... && controlRod == true )
{
// ...
}
// ...
controlRod = false; // <-- set to false so condition is always false
// iteration 2 ------------------------------------------------------
if( ... && controlRod == true ) // controlRod was set to false,
// so this will never be true
{
// ...
}
// ...
controlRod = false; // <-- set to false so condition is always false
// iteration 3 --------------------------------------------------
if( ... && controlRod == true ) // controlRod was set to false,
// so this will never be true
{
// ...
}
// ...
controlRod = false; // <-- set to false so condition is always false
// more iterations...
}
相关文章:
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复