2015-11-14 116 views
0

用戶輸入一個數字ex,7.然後返回該數字的所有倍數,直到1000. X是用戶輸入。如果每個號碼都有if/else。會有不同的方式來做到這一點?什麼是最佳方式將其重寫成簡單的

void printSeries() 
{ 


if (x == 0) 
{ 
    cout << "Closing program" << endl; 


} 


else if (x == 1) 
{ 
    cout << "Printing numbers divisible by " << x << " between " << x << " and 1000" << endl; 
    while (x <= 1000) 

    { 

     while (x % 1 == 0) 

     { 

      cout << "[" << x << "] "; 
      break; 

     } 
     x++; 

    } 

} 
else if (x == 2) 
{ 
    cout << "Printing numbers divisible by " << x << " between " << x << " and 1000" << endl; 
    while (x <= 1000) 

    { 

     while (x % 2 == 0) 

     { 
      cout << "[" << x << "] "; 
      break; 
     } 
     x++; 

    } 

} 
else if (x == 3) 
{ 
    cout << "Printing numbers divisible by " << x << " between " << x << " and 1000" << endl; 
    while (x <= 1000) 

    { 

     while (x % 3 == 0) 

     { 
      cout << "[" << x << "] "; 
      break; 
     } 
     x++; 

    } 

} 
else if (x == 4) 
{ 
    cout << "Printing numbers divisible by " << x << " between " << x << " and 1000" << endl; 
    while (x <= 1000) 

    { 

     while (x % 4 == 0) 

     { 
      cout << "[" << x << "] "; 
      break; 
     } 
     x++; 

    } 

} 
else if (x == 5) 
{ 
    cout << "Printing numbers divisible by " << x << " between " << x << " and 1000" << endl; 
    while (x <= 1000) 

    { 

     while (x % 5 == 0) 

     { 
      cout << "[" << x << "] "; 
      break; 
     } 
     x++; 

    } 

} 
else if (x == 6) 
{ 
    cout << "Printing numbers divisible by " << x << " between " << x << " and 1000" << endl; 
    while (x <= 1000) 

    { 

     while (x % 6 == 0) 

     { 
      cout << "[" << x << "] "; 
      break; 
     } 
     x++; 

    } 

} 
else if (x == 7) 
{ 
    cout << "Printing numbers divisible by " << x << " between " << x << " and 1000" << endl; 
    while (x <= 1000) 

    { 

     while (x % 7 == 0) 

     { 
      cout << "[" << x << "] "; 
      break; 
     } 
     x++; 

    } 

} 
else if (x == 8) 
{ 
    cout << "Printing numbers divisible by " << x << " between " << x << " and 1000" << endl; 
    while (x <= 1000) 

    { 

     while (x % 8 == 0) 

     { 
      cout << "[" << x << "] "; 
      break; 
     } 
     x++; 

    } 

} 
else if (x == 9) 
{ 
    cout << "Printing numbers divisible by " << x << " between " << x << " and 1000" << endl; 
    while (x <= 1000) 

    { 

     while (x % 9 == 0) 

     { 
      cout << "["<< x << "] "; 
      break; 
     } 
     x++; 

    } 

} 
} 
+0

可被x整除的數字也是x的倍數。你不能使用這種關係嗎? – Nandu

回答

0
cin >> x; 

int counter = 1; 
while ((x>0) && (true)) 
{ 
    int multiple = x* counter; 
    if (multiple > 1000) break; 
    cout << multiple; 
    counter++; 
} 
+0

謝謝,完美無缺。它做它需要做的事情,而且更清潔。 –

0

這是相當使用for循環,因爲這樣管理您的「計數」變量很好地直線前進:

#include <iostream> 

using namespace std; 

int main() { 
    unsigned int x = 0; 
    cout << "Please enter a number (greater than 0): "; 
    cin >> x; 
    for (unsigned int i = 0; x * i < 1000; ++i) 
     cout << '\n' << x * i; 
} 
0

您正在尋找這樣的事情?

#include <iostream> 

int main() { 

    int x = 0; 

    std::cout << "Please input number: "; 
    std::cin >> x; 

    for (int i = 0; x*i < 1000; i++) { 
     std::cout << "[" << x*i << "]" << std::endl; 
    } 

    return 0; 
} 
相關問題