基础
打印1000-2000的闰年并统计出现的次数
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int count = 0;
for (int y = 1000; y <= 2000; y++)
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
printf("%d ", y);
count++;
}
}
printf("\n次数: %d", count);
return 0;
}
#include <stdio.h>
int main()
{
int count = 0;
for (int y = 1000; y <= 2000; y+=4)
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
printf("%d ", y);
count++;
}
}
printf("\n次数: %d", count);
return 0;
}
打印100-200之间的素数
#include <stdio.h>
int main()
{
int count = 0;
for (int i = 100; i <= 200; i++)
{
int j = 0;
for (j = 2; j < i; j++)
{
if (i % j == 0)
{
break;
}
}
if (i == j)
{
printf("%d ", i);
count++;
}
}
printf("\n个数是: %d", count);
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
int count = 0;
for (int i = 101; i <= 200; i+=2)
{
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);
count++;
}
}
printf("\n个数是: %d", count);
return 0;
}
使用goto写一个自动关机的程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char input[20] = { 0 };
system("shutdown -s -t 60");
printf("您的电脑将在60s之后自动关机,请输入密码解除!\n");
again:
printf("请输入:>");
scanf("%s", input);
if (strcmp(input, "123456") == 0)
{
printf("\n密码正确!");
system("shutdown -a");
}
else {
printf("\n输入错误,请重新输入!\n");
goto again;
}
return 0;
}