15.习题和go to语句

#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函数只能在一个函数内跳转,不能跨函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值