第6回 (6/10) 演習の解答例

Problem 1

#include <stdio.h>

int main(void)
{
    int i, c;
    char t[2], s[1024];
    scanf("%s", t);
    scanf("%s", s);
    c=0;
    for (i=0; s[i]!=0; i++)
    {
        if (s[i]==t[0]) { c++; }
    }
    printf("%d\n", c);
    return 0;
}

Problem 2

#include <stdio.h>

int main()
{
  char x[1024], y[1024];
  int i;
  scanf("%s", x);
  scanf("%s", y);
  i=0;
  while (x[i]==y[i] && x[i]!='\0')
  {
    i++;
  }
  if (x[i]<y[i])
  {
    printf("<\n");
  }
  else if (x[i]>y[i])
  {
    printf(">\n");
  }
  else
  {
    printf("=\n");
  }
  
  return 0;
}

Problem 3

#include <stdio.h>

#define MAX_N 1000

int main(void)
{
    float x[MAX_N];
    int i, j, N;

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

    for (i=N-1; i>=1; i--)
    {
        for (j=1; j<=i; j++)
        {
            if (x[j-1] < x[j])
            {
                float temp = x[j-1];
                x[j-1] = x[j];
                x[j] = temp;
            }
        }
    }

    for (i=0; i<N; i++)
    {
        printf("%f ", x[i]);
    }
    printf("\n");
    return 0;
}

Updated: