-1

これまでのところ、このプログラムはすべてのトリプルを出力し、入力された数値にトリプルがないかどうかを示します。すべてのトリプルをリストした後、C の値が最も高いトリプルを再度出力する必要があります。 このプロジェクトの I/O の例

#include <stdio.h>

void main()
{

    int a = 0, b = 0, c = 0, n;
    int counter = 0;    // counter for # of triples

    printf("Please Enter a Positive Integer: \n"); //asks for number
    scanf_s("%d", &n); //gets number

    for (c = 0; c < n; c++)  //for loops counting up to the number
    {
        for (b = 0; b < c; b++)
        {
            for (a = 0; a < b; a++)
            {
                if (a * a + b * b == c * c) //pythag to check if correct
                {
                    printf("%d: \t%d %d %d\n", ++counter, a, b, c); //prints triples in an orderd list

                }
                else if (n < 6) // if less than 6 - no triples
                {
                    printf("There is no pythagorean triple in this range");
                }
            }
        }
    }
    getch(); //stops program from closing until you press a keys
}

N に 15 と入力すると、次のように出力されます。

3 4 5
6 8 10
5 12 13

したがって、出力される最後のトリプルは常に C (5 12 13) の最高値を持ちますが、特定のトリプルが最高であるというステートメントを出力する必要があります。

4

1 に答える 1

0

これを試してみてください。改善できると確信しています。動作しますが、掃除する時間がなくなったので、楽しんでください!

#include <stdio.h>

void main(  )
{

  int a = 0, b = 0, c = 0, n;
  int counter = 0;      // counter for # of triples
  int triple_sum[6];
  int sums = 0;
  int triple_high_sum = 0;
  int triple_high_index = 0;

  printf( "Please Enter a Positive Integer: \n" );  //asks for number
  scanf( "%d", &n );        //gets number

  for ( c = 0; c < n; c++ ) //for loops counting up to the number
  {
    for ( b = 0; b < c; b++ )
    {
      for ( a = 0; a < b; a++ )
      {
    if ( a * a + b * b == c * c )   //pythag to check if correct
    {
      printf( "%d: \t%d %d %d\n", counter, a, b, c );   
     //prints triples in an orderd list
      triple_sum[counter++] = a + b + c;

    }
    else if ( n < 6 )   // if less than 6 - no triples
    {
      printf( "There is no pythagorean triple in this range" );
      break;
    }
      }             // endfor

    }               // endfor
  }

  sums = --counter;
  triple_high_sum = -1;
  if ( sums )
    while ( sums > -1 )
    {
      printf( "\ntriple_sum %d == %d", sums, triple_sum[sums] );
      if ( triple_sum[sums] > triple_high_sum )
      {
    triple_high_sum = triple_sum[sums];
    triple_high_index = sums;
      }
      sums--;
    }               // endwhile

  if ( triple_high_sum > -1 )
    printf( "\nThe triple index with the largest value is %d",
        triple_high_index );

  getchar(  );          //stops program from closing until you press a keys
}
        //stop
于 2015-10-19T02:13:47.910 に答える