2016-08-04 53 views
-6

编写一个程序,确定一个正整数是否完美。您的程序识别排列和组合足球比分

并显示1到10,000之间的所有完美数字。 编写一个从用户读取整数的程序。如果用户输入的值小于2,则您的程序应显示相应的错误消息。否则,你的程序应该

显示屏,可以相乘的素数计算N,每行出现

的一个因素。例如:

0-0, 1-0, 2-0, 2-1 
0-0, 0-1, 1-1, 2-1 



{ 
int m, n; 
cout<<"Enter the finals scores of both teams"; 
cout<<"\nenter the score for team m :"; 
cin>>m; 
cout<<"Enter the score for team n :"; 
cin>>n; 
if (m < 0 && n < 0){ 

     cout<<"score can't be negative"; 
     cout<<"\nenter the score for team m :"; 
     cin>>m; 
     cout<<"Enter the score for team n :"; 
     cin>>n; 
    } 
else{ 
     int k=0; 
     if (n==0){ 
      for (int j = 0; j <= m; j++){ 
       for (k; k <= n; k+=1){ 

        cout<<j<<"-"<<k<<",\t"; 
       } 
       k--; 
      } 
     } 
     else if(m==1 && n==1){ 

      int i=0; 
      int k=0; 
      for (int j = 0; j <= m; j++){ 
       for (k; k <= n; k+=1){ 

        cout<<j<<"-"<<k<<",\t"; 
       } 
       k--; 
      } 
      cout<<endl<<endl; 
      for (int j = 0; j <= n; j++){ 

       for (i; i <= m; i+=1){ 

        cout<<i<<"-"<<j<<",\t"; 
       } 
       i--; 
      } 
     } 
     else { 
      int i=0; 
      int k=0; 
      for (int j = 0; j <= m; j++){ 
       for (k; k <= n; k+=1){ 

        cout<<j<<"-"<<k<<",\t"; 
       } 
       k--; 
      } 

      cout<<endl<<endl; 
      for (int j = 0; j <= n; j++){ 

       for (i; i <= m; i++){ 

        cout<<i<<"-"<<j<<",\t"; 
       } 
       i--; 
      }    
     } 

    } 

} 
+2

首先,如果'(M <0 &&Ñ<0){'应该是'如果(M <0 ||Ñ<0){' 。 – DimChtz

+0

你并不需要所有'if/else if/else',你需要的只是2个嵌套for循环,用于'm','n'的任意组合。 – DimChtz

+0

@DanialKhan你的调试器实际上被破坏了吗? –

回答

0

答案很简单使用递归,如果你不知道递归读取第一

void print_goals(int m,int n,int i,int j,string s) 
{ 
    if(i == m && j == n) 
    { 
     cout<<s+char(48+i)+'-'+char(48+j)<<endl; 
     return; 
    } 
    if(i<=m) 
    print_goals(m,n,i+1,j,s+char(48+i)+'-'+char(48+j)+','); 
    if(j<=n) 
    print_goals(m,n,i,j+1,s+char(48+i)+'-'+char(48+j)+','); 



} 

称其为print_goals(5,2,0,0,""); 其中m = 5和n = 2

+0

谢谢:)它的工作 –

0

除了Ambika的回答,让我告诉你一些与你的io处理有关的问题:

首先,这是s在if-else子句中,if或else块被执行,但从不是两者(除非使用某些脏goto入侵)。

所以没有在要求用户为“M”和“n” if块内再次太大的意义 - 所以无论是不这样做(见好就收cout << "score can't be negative";或删除其他:

if(m < 0 || n < 0) 
{ 
    //... 
} 

print_goals(m, n, 0, 0, ""); 
// using Ambika's solution already... 

但后来考虑到用户可能输入了坏值再次

while(m < 0 || n < 0) 
//^instead of if! 

现在更严重的问题(如果你使用得到控制而它可能变得真的严重e)

想象一下,您的程序的用户输入–而不是有效数字–的无效值,例如, G。通过意外击中一个字母键(s7)。 cin >> n无法读取int,并且流的失败位将被设置。结果,随后从cin读取将立即失败,而不修改m或n。尝试使用上面的建议,并输入第一个-1,然后hello –并准备用ctrl-c打破程序!

解决方案:使用CIN的函数getline()从一开始就和解析字符串,然后,或复位失败位,如下图所示:

std::cin >> n; 
if(std::cin.fail()) 
{ 
    // reset the fail bit 
    std::cin.clear(); 
    // important: discard the invalid input! 
    std::string s; 
    std::cin >> s; 
    // now we are prepared to read the input again 
} 

有这样的循环中,和你的罚款。你可能想把这个循环打包到一个函数中,或者将它放在一个外部循环中以避免重复代码;这两个解决方案也解决了另一个关于可用性的小问题:即使第一个值已经有效,您总是会问这两个值。我来了的双循环变体:

int result[2]; 
char c = 'm'; 
for(int& n : result) 
{ 
    for(;;) 
    { 
     std::cout << "enter the score for team " << c << ": "; 
     std::cin >> n; 
     if(std::cin.fail()) 
     { 
      std::cout << "invalid input" << std::endl; 
      std::cin.clear(); 
      std::string s; 
      std::cin >> s; 
     } 
     else if(n < 0) 
     { 
      std::cout << "score can't be negative" << std::endl; 
     } 
     else 
     { 
      break; 
     } 
    } 
    c = 'n'; 
}