学习来源:https://www.bilibili.com/video/BV1q54y1q79w/?spm_id_from=333.337.search-card.all.click
1.判断一个数是否为奇数。
#include<stdio.h>
int main()
{
int a;
printf("请输入需要判断的数字\n");
scanf_s("%d", &a);
if (a % 2 == 1)
printf("奇数\n");
else
printf("偶数\n");
return 0;
}
2.输出1~100之间的奇数。
for循环
#include<stdio.h>
int main()
{
int num;
printf("1到100之间的奇数有:");
for (num = 1; num <= 100; num++)
{
if (num % 2 == 1)
printf("%d、", num);
}
return 0;
}
while循环
#include<stdio.h>
int main()
{
int num=1;
printf("1到100之间的奇数有:");
while(num<=100)
{
if (num % 2 == 1)
printf("%d、", num);
num++;
}
return 0;
}
3.给三个整形数字比较大小并大到小排序。
最基础:
#include<stdio.h>
int main()
{
int a, b, c, max;
printf_s("输入3个值\n");
scanf_s("%d %d %d", &a, &b, &c);
if (a < b)
{
max = a;
a = b;
b = max;
}
if (a < c)
{
max = a;
a = c;
c = max;
}
if(b<c)
{
max = b;
b = c;
c = max;
}
printf_s("从大到小的顺序是:");
printf_s("%d %d %d",a,b, c);
}
不用函数的for循环:
#include <stdio.h>
int main()
{
int a[3],i, j, max;
printf("请输入3个数:\n");
for (i = 0; i < 3; i++)
scanf("%d", &a[i]);
for (i = 0; i < 3; i++)
{
for (j = i + 1; j < 3; j++)
{
if (a[i] < a[j])
{
max = a[i];
a[i] = a[j];
a[j] = max;
}
}
printf("%d,", a[i]);
}
return 0;
}
4.打印1000-2000中的闰年。
闰年的条件:如果N能够被4整除,并且不能被100整除
#include<stdio.h>
int main()
{
int a;
printf_s("闰年有:\n");
for (a = 1000; a <=2000; a++)
{
if (a % 4 == 0)
{
if (a % 100 != 0)
{
printf("%d\n ", a);
}
}
if (a % 400 == 0)
{
printf("%d\n ", a);
}
}
return 0;
}
5.只打印数字字符而忽略其它字符。
#include<stdio.h>
int main()
{
char ch = '\0';
while ((ch = getchar()) != EOF)
{
if (ch < '0' || ch > '9')
continue;
putchar(ch);
}
return 0;
}
6.打印1-100中所有n倍数的数字。
#include<stdio.h>
int main()
{
int i = 1;
printf("1-100中是3的倍数的是:\n");
while (i <= 100)
{
if (i % 3 == 0)
printf("%d\n ", i);
i++;//if不执行,因为if只执行下面第一条语句或代码块
}
return 0;
}
7.找两个数的最大公约数
#include<stdio.h>
int main()
{
int a, b, t;
int i = 0;
scanf("%d %d", &a, &b);
while (t = a % b)
{
a = b;
b = t;
}
printf("%d\n", b);
return 0;
}
8.计算n的阶乘
#include<stdio.h>
int main()
{
int n = 0;
int num = 1;
scanf("%d", &n);
for (int i = 1; i < n + 1; i++)
{
num = i * num;
}
printf("%d", num);
return 0;
}
9.计算1!+2!+3!+......+n!
#include<stdio.h>
int main()
{
int n = 0;
int k = 1;
int sum = 0;
scanf_s("%d", &n);
for (int i = 1; i <= n; i++)
{
k = 1;
for (int j = 1; j <= i; j++)
{
k = j * k;
}
sum = k + sum;
}
printf("%d", sum);
return 0;
}
10.二分查找
在一个有序数组中查找具体的某个数字n。 编写int binsearch(int x,inv[],int n); 功能:在v[0]<=v[1]<=v[2]....<=v[n-1]的数组中查找x。
逻辑:判断数组下标最左和中间的数和待查找值的大小关系,若未查找成功则重新定义left和mid值,继续查找,若left 和 mid 大小关系出现错误,结束查找,输出找不到。
#include<stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int left = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
int right = sz - 1;
int mid = left + (right - left) / 2;
int x = 1;
int flag = 0;
while (left <= mid)
{
if (x < arr[mid])
{
right = mid;
mid = left + (right - left) / 2;
}
else if (x > arr[mid])
{
left = mid;
mid = left + (right - left) / 2;
}
else
{
printf("找到了,下标是:%d", mid);
flag = 1;
break;
}
}
if (0 == flag)
printf("找不到!\n");
return 0;
}
11.计算1/1-1/2+1/3-1/4......+1/100
#include<stdio.h>
int main()
{
float i = 0;
float sum = 0.0;
float flag = 1;
for (i = 1; i <= 100; i++)
{
sum += flag / i;
flag = -flag;//或flag*=-1;
}
printf("%lf", sum);
return 0;
}
12.输入乘法口诀表
#include<stdio.h>
int main()
{
int i = 0;
for (i = 1; i <=9; i++)
{
int j = 1;
for (j = 1; j < i + 1; j++)
{
printf("%d * %d = %-2d ", j, i, i * j);
}
printf("\n");
}
return 0;
}
13.设计出字符可以跳跃的程序
#include<stdio.h>
#include<windows.h>
#include<string.h>
int main()
{
char arr1[] = {"welcome to bit..." };
char arr2[] = { " " };
int right = strlen(arr1) - 1;
int left = 0;
printf("%s\n", arr2);
while (left<=right)
{
Sleep(1000);
arr2[right] = arr1[right];
arr2[left] = arr1[left];
right++;
left++;
printf("%s\n", arr2);
}
return 0;
}
14.猜数字游戏
1)电脑会生成一个随机数
2)猜数字
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void menu()
{
printf("****************************\n");
printf("**** 1.play 0.exit ****\n");
printf("****************************\n");
}
void game()
{
int guess = 0;
int ret = rand() % 100 + 1;//设置随机数在1-100之间
while (1)
{
printf("猜数游戏:>");
scanf_s("%d", &guess);
if (guess > ret)
printf("猜大了\n");
else if (guess < ret)
printf("猜小了\n");
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
int main()
{
int input=0;
srand((unsigned int)time(NULL));//拿时间戳来设置随机数的生成起点
do
{
menu();
printf("请选择>:");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();//猜数字游戏
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
15.设置关机程序
*注意:windows系统不一样,程序有区别,谨慎使用。
#include<string.h>
#include<stdio.h>
int main()
{
char input[20] = { 0 };
//shutdown -s -t 60
//system()- 执行系统命令的
system("shutdown -s -t 60");
again:
printf("请注意,你的电脑在1分钟内关机,如果输入:取消,就取消关机\n请输入>:");
scanf_s("%s", input);
if (strcmp(input, "取消") == 0)//比较字符串-strcmp
{
system("shutdown -a");
}
else
goto again;
return 0;
}