第4回 (5/27) 演習の解答例

課題2

#include <stdio.h>

int main(void)
{
  float x = 1.0;
  float a = 1.0;
  float total = 1.0;
  int i;
  for (i=1; i<100; i++) {
    a *= x / i;
    total += a;
  }
  printf("%f\n", total);
  return 0;
}

課題3

#include <stdio.h>

int main(void)
{
  int n, x, i, j, k;
  scanf("%d", &n);
  scanf("%d", &x);
  for (i=1; i<=n; i++)
  {
    for (j=i+1; j<=n; j++)
    {
      for (k=j+1; k<=n; k++)
      {
        if (i+j+k == x)
        {
          printf("%d + %d + %d = %d\n", i, j, k, x);
        }
      }
    }
  }
  return 0;
}

Updated: