天天看點

循環與分支語句練習題

練習1:編寫代碼實作,模拟使用者登入,并且隻能輸入三次密碼,密碼正确則登入成功,三次都錯誤将退出程式。

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

int main()
{
  char password[20] = { 0 };
  int i = 0;
  for(i = 1;i <= 3;i++)
  {
    printf("請輸入密碼:");
    scanf("%s", password);
    if (strcmp(password, "123456") == 0)
    {
      printf("密碼正确,登入成功!\n");
      break;
    }
    else
    {
      printf("密碼錯誤");
      Sleep(500);
      system("cls");
    }
  }
  if (i > 3)
  {
    printf("登入失敗,退出程式!");
  }
  return 0;
}      

練習2:寫代碼将三個數從大到小輸出

int main()
{
  int x = 0;
  int y = 0;
  int z = 0;
  int i = 0;
  scanf("%d%d%d", &x, &y, &z);
  if (x < y)
  {
    i = x;
    x = y;
    y = i;
  }
  if (x < z)
  {
    i = x;
    x = z;
    z = i;
  }
  if (y < z)
  {
    i = y;
    y = z;
    z = i;
  }
  printf("%d > %d > %d\n", x, y ,z);
  return 0;
}      

練習3:寫代碼輸出1到100之間所有的三的倍數

int main()
{
  int i = 0;
  for (i = 1; i <= 100; i++)
  {
    if (i % 3 == 0)
      printf("%d ", i);
  }
  return 0;
}      

練習4:給定兩個數,求這兩個數的最大公約數

int main()
{
  int x = 20;
  int y = 15;
  int z = 1;
  while (z != 0)
  {
    z = x % y;
    x = y;
    y = z;
  }
  printf("最大公約數為:%d\n", x);
  return 0;
}      

方法:輾轉相除法

練習5:列印100到200之間的素數

方法一:(不夠優)

int main()
{
  int i = 0;
  for (i = 100; i <= 200; i++)
  {
    int x = 0;
    for (x = 2; x < i; x++)
    {
      if (i % x == 0)
        break;
    }
    if (x == i)
      printf("%d ", i);
  }
  return 0;
}      

方法二:

int main()
{
  int i = 0;
  for (i = 101; i <= 200; i += 2)
  {
    int x = 0;
    for (x = 2; x <= sqrt(i); x++)
    {
      if (i % x == 0)
        break;
    }
    if (x > sqrt(i))
    {
      printf("%d ", i);
    }
  }
  return 0;
}      

練習6:計算1到100之間數字9的個數

int main()
{
  int i = 0;
  int count = 0;
  for (i = 1; i <= 100; i++)
  {
    if (i % 10 == 9)
    {
      printf("%d ", i);
      count++;
    }
    if (i / 10 == 9)
    {
      printf("%d ", i);
      count++;
    }
  }
  printf("\ncount = %d\n", count);
  return 0;
}      

練習7:計算1/1-1/2+1/3-1/4+......+1/99-1/100

方法一:

int main()
{
  int i = 0;
  double x = 0;
  int flag = 1;
  for (i = 1; i <= 100; i++)
  {
    x += flag*1.0 / i;
    flag = -flag;
  }
  printf("%lf\n", x);
  return 0;
}      

方法二:

int main()
{
  int x = 0;
  double a = 0;
  int y = 0;
  double b = 0;
  double z = 0;
  for (x = 1; x <= 100; x += 2)
  {
    a += 1.0 / x;
  }
  for (y = 2; y <= 100; y += 2)
  {
    b -= 1.0 / y;
  }
  z = a + b;
  printf("%lf\n", z);
  return 0;
}      

練習8:求10個數中的最大值

int main()
{
  int a[] = { 0,1,2,3,4,5,6,7,8,9 };
  int i = 0;
  int max = 0;
  int sz = sizeof(a) / sizeof(a[0])-1;
  for (i = 0; i <= sz; i++)
  {
    if (max < a[i])
      max = a[i];
  }
  printf("%d\n", max);
  return 0;
}      

練習9:列印乘法口訣表

int main()
{
  int x = 0;
  int y = 0;
  int z = 0;
  for (y = 1; y <= 9; y++)
  {
    for (x = 1; x <= y; x++)
    {
      z = y * x;
      printf("%d * %d = %-2d  ", x, y, z);
    }
    printf("\n");
  }
  return 0;
}      

練習10:猜數字遊戲

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

void game()
{
  int i = 0;
  int guess = 0;
  i = rand()%100 + 1;
  while (1)
  {
    printf("請猜數字:");
    scanf("%d", &guess);
    if (guess > i)
    {
      printf("猜大了\n");
      Sleep(1000);
      system("cls");
    }
    else if (guess < i)
    {
      printf("猜小了\n");
      Sleep(1000);
      system("cls");
    }
    else if (guess == i)
    {
      printf("猜對了,遊戲結束!\n");
      Sleep(1000);
      system("cls");
      break;
    }
  }
}

void menu()
{
  printf("                         \n");
  printf("      1.play  0.exit     \n");
  printf("                         \n");
}

int main()
{
  int choose = 0;
  srand((unsigned int)time(NULL));
  do
  {
    menu();
    printf("請選擇:");
    scanf("%d", &choose);
    if (choose == 0)
    {
      printf("遊戲結束\n");
      Sleep(1000);
      system("cls");
      break;
    }
    else if (choose == 1)
    {
      printf("開始遊戲\n");
      Sleep(1000);
      system("cls");
      game();
    }
    else
    {
      printf("輸入錯誤,請重新輸入!\n");
      Sleep(1000);
      system("cls");
    }
  } while (choose);
  return 0;
}