百钱买百鸡的for循环问题
幻灵小龙 2011-02-19 04:23:27 关于cock、hen和chick的初始化位置,我知道在for循环里定义并初始化的变量只能在for循环里使用。在for循环外边定义和在for循环里定义为什么结果不同?求教
[code=C]
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
//在for循环里分别定义初始化,运行正常
for(int cock=1;cock<=13;cock++)
for(int hen=1;hen<=18;hen++)
for(int chick=3;chick<=96;chick++)
if(cock*7+5*hen+chick/3==100&&cock+hen+chick==100&&chick%3==0)
cout<<"cock="<<cock<<",hen="<<hen<<",chick="<<chick<<endl;
}
[/code]
[code=C]
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int cock=1;int hen=1;int chick=3;//在这里定义并初始化,运行正常但没结果
for(;cock<=13;cock++)
for(;hen<=18;hen++)
for(;chick<=96;chick++)
if(cock*7+5*hen+chick/3==100&&cock+hen+chick==100&&chick%3==0)
cout<<"cock="<<cock<<",hen="<<hen<<",chick="<<chick<<endl;
}
[/code]