第11回演習の解答例

課題1

#include <stdio.h>

#define N 10

int count_even(int x[], int n) {
  int i;
  if (n == 0) {
    return 0;
  } else if (x[n - 1] % 2 == 0) {
    return count_even(x, n - 1) + 1;
  } else {
    return count_even(x, n - 1);
  }
}

int main() {
  int x[N];
  int i;
  int n = 0;

  for (i = 0; i < N; i++) {
    scanf("%d", &x[i]);
  }

  printf("%d\n", count_even(x, N));

  return 0;
}

課題2

#include <stdio.h>

int pascal(int n, int k)
{
  if (k==0 || n==k)
  {
    return 1;
  }
  else
  {
    return pascal(n-1, k-1)+pascal(n-1, k);
  }
}

int main(int argc, char *argv[])
{
  int n, k;
  printf("n = ");  
  scanf("%d", &n);
  printf("k = ");
  scanf("%d", &k);
  printf("%d\n", pascal(n,k));
  return 0;
}

Updated: