#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
打印1000 - 2000的闰年
int main()
{
int y = 0;
int count = 0;
for (y = 1000;y <= 2000;y++)
{
//判断y是不是闰年
//1.被4整除,不能被100整除的是闰年
//2.能被400整除的是闰年
if (y % 4 == 0)
{
if (y % 100 != 0)
{
printf("%d ", y);
count++;
}
}
if (y % 400 == 0)
{
printf("%d ", y);
count++;
}
}
printf("\ncount = %d\n", count);
return 0;
}
int main()
{
int y = 0;
int count = 0;
for (y = 1000;y <= 2000;y++)
{
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) //&& 是并且,|| 是或者
{
printf("%d", y);
count++;
}
}
printf("\ncount = %d\n", count);
return 0;
}
打印100-200直接的素数
素数 - 质素
只能被1和它本身整除
int main()
{
int count = 0;
int i = 0;
//m = a*b
//a和b中一定有一个数字 <= 开平方m的
//16 = 2*8 = 4*4
//sqrt - 是开平方的函数 - 引用 #include <math.h>
for (i = 101;i <= 200;i += 2)
{
//判断i是否位素数
//2 -> i - 1之间的数字去试除i,看能不能被整除
int j = 0;
int flag = 1;
for (j = 2;j <= sqrt(i);j++)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
goto语句:
int main()
{
flag:
printf("hehe\n");
printf("hehe\n");
goto flag;
return 0;
}
关机程序,
只要运行起来,电脑就会再1分钟内关机,如果输入:我是猪,就取消关机!
电脑cmd里
shutdown -s -t 60 设置 时间 关机
shutdown -a 取消关机
int main()
{
关机
C语言提供了一个函数: system() - 执行系统命令
char input[20] = { 0 }; //存放输入的信息
system("shutdown -s -t 60"); //system - #include <stdlib.h>
while (1)
{
printf("请注意,你的电脑再1分钟内关机,如果输入:我是猪,就取消关机\n");
scanf("%s", input);
if (strcmp(input, "我是猪") == 0)//两个字符串比较是不能使用==的,应该使用strcmp() srting compare
{ //strcmp - #include <string.h>
system("shutdown -a");
break;
}
}
return 0;
}
goto函数只能在一个函数内跳转,不能跨函数