嗨我寫這個代碼打印出一個整數的因子與一個for looopC++ for循環轉換爲while循環
我該如何寫一個while循環?
for(int i = 1; i < integer+1; i++)
{
if(integer % i == 0)
cout<< i<<" ";
}
嗨我寫這個代碼打印出一個整數的因子與一個for looopC++ for循環轉換爲while循環
我該如何寫一個while循環?
for(int i = 1; i < integer+1; i++)
{
if(integer % i == 0)
cout<< i<<" ";
}
int i = 1;
while (i < integer+1)
{
if(integer % i == 0)
cout<< i<<" ";
i++;
}
甚至更好:
int i = 0;
while (++i < integer+1)
{
if(integer % i == 0)
cout<< i<<" ";
}
int i = 1;
while(i < integer + 1) {
// your current loop body goes here
i++;
}
下面的代碼將打印相同的結果,你的循環中打印。
int i = 1;
while(i < integer + 1)
{
if(integer % i == 0)
{ cout<< i<<" "; }
i++;
}
你只是在'整數/ 2'處停下來,因爲大的數字顯然不會平均分配給它。你先試試看,看看你能弄明白嗎? – 2012-03-20 22:22:23
http://www.cplusplus.com/doc/tutorial/control/ – 2012-03-20 22:22:37
您遇到了哪些麻煩? – 2012-03-20 22:22:54