2016-08-04 54 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'; 
}