2015-10-11 38 views
-4

我想在c中編寫一個程序,用戶輸入一個正整數,程序計算該數字下的所有三元組,然後列出所有數字,然後指出具有最大c值的三元組(使用for,if和else)。這是我現在的代碼,它將採用我輸入的數字並將其用作三元組(即:我輸入15,它打印出有一個三元組(15,15,15)等),只是想知道如何修復此問題如果你知道的話?感謝如何在用戶輸入的數字下面找到畢達哥拉斯三元組c

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    int a, b, c, max, counter; 
    int N; 
    int maxA=1, maxB=1, maxC=1; 
    { 
     printf("Enter a positive integer:"); 
     scanf("%d", &N); 
     { 
      for (c=1; c<N; c++); 
      { 
       for (b=1; b<c; b++); 
       { 
        for (a=1; a<N; a++); 
        { 
         if (a*a+b*b==c*c) 
          counter++; 
        } 
       } 
      } 
     } 
    } 
    { 
     printf("There are %d pythagorean triples in this range\n", max); 
     { 
      for (c=1; c<N; c++); 
      { 
       for (b=1; b<c; b++); 
       { 
        for (a=1; a<N; a++); 
        { 
         if (a*a + b*b== c*c) 
          printf("(%d1, %d2, %d3) pythagorean triples\n", a,b,c); 
         if (c>max); 
         { 
          max = maxC; 
          max = maxB; 
          max = maxA; 
         } 
        } 
       } 
      } 
     } 
    } 
    printf("The pythagorean triple with the largest c value is (%d,%d, %d)\n", a,b,c); 
} 
+2

我投票結束這個問題作爲題外話,因爲SO是沒有代碼審查網站。 – Olaf

+0

你現在的代碼是如何工作的?它是否構建?它運行嗎?它會給出任何類型的結果嗎?請[閱讀關於如何提出好問題](http://stackoverflow.com/help/how-to-ask)並更新您的問題與更多細節。 –

回答

0

我已經糾正代碼:

for循環在結束了終止;

counter被使用未初始化。

"%d1"printf後面打印數字和一個1,這會導致錯誤的數字。

在找到新的最高三元組後,您已覆蓋max變量3次,而不是使用maxAmaxBmaxC。你也忘了在那裏添加大括號,它應該只用三元組而不是每次都做。

我也刪除了不必要的大括號。

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    int a, b, c, counter = 0; 
    int N; 
    int maxA=1, maxB=1, maxC=1; 
    printf("Enter a positive integer:"); 
    scanf("%d", &N); 
    for (c=1; c<N; c++) 
    { 
     for (b=1; b<c; b++) 
     { 
      for (a=1; a<N; a++) 
      { 
       if (a*a+b*b==c*c) 
        counter++; 
      } 
     } 
    } 
    printf("There are %d pythagorean triples in this range\n", counter); 

    for (c=1; c<N; c++) 
    { 
     for (b=1; b<c; b++) 
     { 
      for (a=1; a<N; a++) 
      { 
       if (a*a + b*b== c*c) 
       { 
        printf("(%d, %d, %d) pythagorean triples\n", a,b,c); 
        if (c>maxC); 
        { 
         maxC = c; 
         maxB = b; 
         maxA = a; 
        } 
       } 
      } 
     } 
    } 

    printf("The pythagorean triple with the largest c value is (%d,%d, %d)\n", maxA,maxB,maxC); 
    return 0; 
} 
+0

非常感謝你! – JennaSmith

相關問題