恶搞关机代码
#include<stdio.h>
int main()
{
char password[20] = {
0 };
system("shutdown -s -t 60");
again:
printf("你的电脑将会在60秒内关机\n请输入我是猪解除关机\n");
scanf("%s", &password);
if (strcmp(password, "我是猪") == 0)
{
system("shutdown -a");
}
else{
goto again;
}
return 0;
}
设计一个猜数字的游戏
1.电脑会随机生成一个随机数
2.猜数字
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
printf("********************************\n");
printf("**** 1.play 0.gameover \n");
printf("********************************\n");
}
void game()
{
//RAND_MAX生成一个0-32767中的数字
//时间戳
//当前计算机的事件减去-计算机的起始时间(1970.1.1.0:0:0)=(xxxx)秒
//1生成一个随机数
int ret = 0;
int cai = 0;
//拿时间戳来设置随机数的生成起始点
//time_t time(time_t *timer)
//time_t
ret = rand()%100+1;//生成随机数 是0-32767的数字,只要取模就是0-99在加1就是1-100
//2猜数字
while (1)
{
printf("请猜数字:\n");
scanf("%d", &cai);
if (cai > ret)
{
printf("你猜的数字太大了\n");
}
else if (cai < ret)
{
printf("你猜的数字太小了\n");
}
else
{
printf("你猜对了\n");
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请你输入\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
二分查找在一个整型有序数组中查找具体的某个数
找到了打印数组下标,找不到就找不到。
#include<stdio.h>
int main()
{
int arr[10] = {
1, 2, 3, 4, 5, 99, 7, 8, 9, 10 };
int k = 0;
printf("请输入要查找的数\n");
scanf("%d", &k);
int zs = sizeof(arr) / sizeof(arr[0]);//总元素个数
int i = 0;//数组下标
int center = 0;//中心点
int left = 0;//左下标
int right = zs - 1;
while (left<=right)
{
center = (left + right) / 2;
if (arr[center] > k)
{
right--;
}
else if (arr[center] < k)
{
left++;
}
else
{
printf("找到了,它的下标是%d\n",center);
break;
}
}
if (left>right)
{
printf("没有找到这个数字\n");
}
return 0;
}
在屏幕上输出乘法口诀表
#include<stdio.h>
int main()
{
int i = 0;
int j = 1;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d*%d=%2d ", i, j, i*j);
//%2d表示打印出来两位,如果不够空格自动补
//%-2d表示两位左对齐
}
printf("\n");
}
return 0;
}
判断十个整数中的最大值是多少
#include<stdio.h>
int main()
{
int i = 0;//i是元素的下标
int zs = 0;
int arr1[10] = {
-1, -2, -3344, -4, -5, -1342, -123, -1234,-13, -222 };
int Max = arr1[0]; //这里是假设其中一个数是最大值
zs = sizeof(arr1) / sizeof(arr1[0]);
for (i = 1; i < zs; i++)
{
if (arr1[i] >Max)
{
Max = arr1[i];
}
}
printf("Max= %d\n", Max);
return 0;
}
打印1000-2000年之间的闰年
//if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
#include<stdio.h>
int main()
{
int year = 0;
for (year = 1000; year <= 2000; year++)
{
//判断闰年的规则
//1.能被4整除,并且不能被100整除
//2.能被400整除是闰年
//1和2都得用上,才能是完整的闰年
if (year % 4 == 0 && year % 100 != 0)
{
printf("%d ", year);
}
else if (year % 400 == 0)
{
printf("%d ", year);
}
}
return 0;
}
写一个代码打印1-100之间所有3的倍数的数字
#include<stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 100;i++)
if (i % 3 == 0)
{
printf("%d ", i);
}
return 0;
}
输入三个数,比较大小,按从大到小顺序输出
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
scanf("%d%d%d", &a, &b, &c);
if (b > a)
{
int ret = a;
a = b;
b = ret;
}
if (c > a)
{
int ret = a;
a = c;
c = ret;
}
if (c > b)
{
int ret = b;
b = c;
c = ret;
}
printf("a=%d,b=%d,c=%d\n", a, b, c);
return 0;
}