2015-11-14 93 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; 
} 
相关问题