本文不涉及指针,主要练习选择,循环语句
题目一:计算n的阶乘
#include<stdio.h>
int main()
{
int i = 0;
int n = 0;
int ret = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
ret *= i;
}
printf("%d\n", ret);
return 0;
}
题目二:计算 1!+2!+3!+..+10!
#include<stdio.h>
int main()
{
int i = 0;
int n = 0;
int ret = 1;
int sum = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
ret *= i;
sum += ret;
}
printf("%d\n", sum);
return 0;
}
题目三:在一个有序数组中查找具体的某个数字n
方法一:遍历查找
#include<stdio.h>
int main()
{
int i = 0;
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int k = 7;
int flag = 0;
for (i = 0; i < 10; i++)
{
if (arr[i] == 7)
{
printf("找到了,下标为%d\n", i);
flag = 1;
}
}
if (flag == 0)
printf("没找到");
return 0;
}
方法二:二分查找(只对有序数组有用)
#include<stdio.h>
int main()
{
char arr[] = {1,2,3,4,5,6,7,8,9,10};
scanf("%s", arr);
int left = 0;
int right = sizeof(arr) / sizeof(arr[0]) - 2;
int mid = 0;
int flag = 0;
while (left <= right)
{
mid = (left + right) / 2;
if (arr[mid] < 7)
{
left = mid + 1;
}
else if(arr[mid] == 7)
{
printf("找到了,下标为%d\n", mid);
flag = 1;
break;
}
else
{
right = mid - 1;
}
}
if (flag == 0)
printf("没找到!");
return 0;
}
题目四:编写代码,演示多个字符从两端移动,向中间汇聚。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char arr1[]= "Every single day couns!";
char arr2[]= " ";
int i = 0;
int sz = strlen(arr1) - 1;
for (i = 0; i <= sz; i++, sz--)
{
arr2[i] = arr1[i];
arr2[sz] = arr1[sz];
printf("%s\n", arr2);
Sleep(1000);
system("cls");
}
printf("Every single day couns!");
return 0;
}
题目五:编写代码实现,模拟用户登录情景,并且只能登录三次。只允许输入三次密码,如果密码正确则提示登录成,如果三次均输入错误,则退出程序。
#include<stdio.h>
int main()
{
int i = 0;
int flag = 0;
char password[] = "123456";
for (i = 0; i < 3; i++)
{
printf("请输入密码:");
scanf("%s", password);
if (strcmp(password, "123456") == 0)
{
printf("登录成功!\n");
flag = 1;
break;
}
else
printf("密码错误!\n");
}
if (flag == 0)
printf("退出程序!\n");
return 0;
}
题目六:猜数字游戏
#include<stdio.h>
void menu()
{
printf("********游戏开始*********\n");
printf("******1.play 0.exit******\n");
}
void game()
{
//1.生成随机值 int ret=rand()
int ret = rand() % 100 + 1; //生成1—100的随机数
//2.猜数字
int k = 0;
while (1)
{
printf("猜数字:");
scanf("%d", &k);
if (k > ret)
printf("猜大了!\n");
else if (k < ret)
printf("猜小了!\n");
else
{
printf("猜对了!\n");
break;
}
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("请输入(1开始 0退出):");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏结束!\n");
break;
default:
printf("请重新输入!\n");
break;
}
} while (input);
return 0;
}
题目七:关机程序
1.程序运行,1分钟内电脑关机
2.如果输入:我是猪,就取消关机
#include<stdio.h>
#include<stdlib.h>
int main()
{
char arr[20] = { 0 };
system("shutdown -s -t 60");
while (1)
{
printf("电脑将在一分钟后关机,如果输入我是猪就会取消关机\n");
printf("请输入:");
scanf("%s", arr);
if (strcmp(arr, "我是猪") == 0)
{
system("shutdown -a");
break;
}
}
return 0;
}
题目八:写代码将三个整数按照从小到大输出
#include<stdio.h>
int main()
{
int a, b, c;
int i = 0;
scanf("%d %d %d", &a, &b, &c);
if (a > b)
{
i = a;
a = b;
b = i;
}
if (a > c)
{
i = a;
a = c;
c = i;
}
if (b > c)
{
i = b;
b = c;
c = i;
}
printf("%d %d %d", a, b, c);
return 0;
}
题目九:输出100-200之间的素数
#include<stdio.h>
#include<math.h>
int main()
{
int i = 0;
for (i = 100; i <= 200; i++)
{
int j = 0;
int flag = 1;
for (j = 2; j <= sqrt(i); j++)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
printf("%d ", i);
}
return 0;
}
题目十:求1000~2000年中的闰年
#include<stdio.h>
int main()
{
int count = 0;
int i = 0;
for (i = 1000; i <= 2000; i++)
{
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))
{
printf("%d ", i);
count++;
}
}
printf("\ncount=%d", count);
return 0;
}
题目十一:求两个数的最大公约数
方法一:暴力求解
#include<stdio.h>
int main()
{
int m = 0;
int n = 0;
scanf("%d %d", &m, &n);
int k = 0;
k = (m > n ? n : m);
int i = 0;
while(1)
{
if ((m % k == 0) && (n % k == 0))
{
break;
}
k--;
}
printf("%d\n", k);
return 0;
}
方法二:辗转相除法
#include<stdio.h>
int main()
{
int m = 0;
int n = 0;
int k = 0;
scanf("%d %d", &m, &n);
while (k = m % n)
{
m = n;
n = k;
}
printf("%d\n", n);
return 0;
}
ps:代码都是本人自己写的,如果有错误或者更好的方法欢迎指正!