分支循环作业讲解1

第1题

关于语句说法知正确是:()
A.if语句后面只能跟一条语句
B.if语句中0表示假,1表示真
C.if语是一种分支语句,可可以实现单分支,也可以实现多分支
D.else语句总是和它的对齐的if语句配
答案:C

if语句可以跟大括号,大括号里面可以写多条语句

if语句0表示假对的,但是非零表示真

else语句不一定和相应的if语句对齐

第2题

关于switchi说法不正确的是:()
A.switch语话句中的default子句可以放在任意位置
B.switch语句中case后的表达式只能是整型常量表达式
C.switch语句中case子句必须在default子句之前
D.switch语句中case表达式不要求顺序
答案:C

关键还是那句话:switch的子句没有顺序可言,无所谓谁先谁后

第3题

#include<stdio.h>
int func(int a)
{
    int b;
    switch(a)
    {
    case 1: b = 30;
    case 2: b = 20;
    case 3: b = 16;
    default: b = 0;
    }
    return b;
}
int main()
{
    printf("%d\n",func(1));
    return 0;
}
则func(1) = (  0  )

这里的case没有break语句,就会使语句往下继续执行

第4题

switch语句中,C不可以显什么型()
题目内含:
A.int
B.long
C.char
D.float
答案:D

第5题

int main()
{
    int × = 3;
    int y = 3;
    switch (x%2)
    {
            case 1:
            switch(y)
            {
                case 0:  
                    printf("first\n");
                case 1:
                    printf ("second\n");
                    break;
                default:printf("hello\n");
            }
            case 2:
            printf("third\n");
    }
    return 0;
}
打印结果:hello
        third

第6题

写代码将三个整数按从大到小输出。

#include<stdio.h>
int main()
{
    int a,b,c,sw,sy,sz;
    scanf("%d%d%d",&a,&b,&c);
    if(a < b)
    {
        sw = a;
        a = b;
        b =sw;
    }
    if(a < c)
    {
        sy = a;
        a = c;
        c = sy;
    }
    if(b < c)
    {
        sz = b;
        b = c;
        c = sz;
    }
    printf("%d %d %d",a,b,c);
    return 0;
}

看上去很繁琐

#include <iostream>
#include <algorithm>
using namespace std;
int a[4];
int main()
{
    cin>>a[1]>>a[2]>>a[3];
    sort(a+1,a+4);
    cout<<a[1]<<' '<<a[2]<<' '<<a[3];
    return 0;
}

当然还有别的实现形式

#include <iostream>
using namespace std;
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    if(a>b)swap(a,b);
    if(b>c)swap(b,c);
    if(a>b)swap(a,b);
    cout<<c<<' '<<b<<' '<<a;
}
#include<iostream>
using namespace std;
int main()
{
    int a,b,c,sum;
    cin>>a>>b>>c;
    sum = a+b+c;
    a = max(a,max(b,c));
    c = min(c,min(a,b));
    b = sum-a-c;
    cout<<a<<" "<<b<<" "<<c;
    return 0;
}
#include<iostream>
using namespace std;
int main()
{
    int a,b,c,sum,ma,mi,mm;
    cin>>a>>b>>c;
    sum = a+b+c;
    ma = max(a,max(b,c));
    mi = min(a,min(b,c));
    mm = sum-a-c;
    cout<<ma<<" "<<mm<<" "<<mi;
    return 0;
}

第7题

第7题(编程题)
题目名称:
打印3的倍数
1-100中3的倍数
#include<stdio.h>
int main()
{
    for(int i = 3;i <= 100; i++)
    {
        if(i % 3 == 0)
        {
            printf("%d ",i);
        }
    }
    return 0;
}
输出结果:3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99

第8题

做一道最大公约数的小题

常见的解法有如下

#include<stdio.h>
#include<math.h>
int main()
{
    int m,n,min,max;
    printf("Please input m,n:");
    scanf("%d%d",&m,&n);
    if(m>n)
        min = n;
    else min = m;
    for(int i = 2;i <= min; i++)
    {   
        if( m % i == 0&& n % i == 0)
           {
            max = i;
           }
    }
    printf("Output maximum common divisor:%d\n",max);
    return 0;
}
#include<stdio.h>
int main()
{
    int m,n,max;
    printf("Please input m,n:");
    scanf("%d%d",&m,&n);
    if (m > n)
        max = n;
    else max = m;
    while(1)
    {
        if(m % max == 0&&n % max == 0)
        {
            printf("Output maximum common divisor:%d\n",max);
            break;
        }
        max--;
    }
    return 0;
}

同时看辗转相除法

#include<stdio.h>
int main()
{
    int m,n,t;
    printf("Please input m,n:");
    scanf("%d%d",&m,&n);
    while(m % n)
    {
        t = m % n;
        m = n;
        n = t;
    }
    printf("Output maximum common divisor:%d\n",n);
    return 0;
}

看我的随笔和算法1,上面有对辗转相除法的详细说明

第9题

打印1000-2000年之间的全部闰年,此题很经典,也在我的随笔和算法1中

#include<stdio.h>
int main()
{
    int year;
    for(year = 1000;year <= 2000; year++)
    {
        if(year % 4 == 0)
        {
            if(year % 100 != 0)
            printf("%d ",year);
         }
        if(year % 400 == 0)
        {
            printf("%d ",year);
        }
    }
    return 0;
}
1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044 1048 1052 1056 1060 1064 1068 1072 1076 1080 1084 1088 1092 1096 1104 1108 1112 1116 1120 1124 1128 1132 1136 1140 1144 1148 1152 1156 1160 1164 1168 1172 1176 1180 1184 1188 1192 1196 1200 1204 1208 1212 1216 1220 1224 1228 1232 1236 1240 1244 1248 1252 1256 1260 1264 1268 1272 1276 1280 1284 1288 1292 1296 1304 1308 1312 1316 1320 1324 1328 1332 1336 1340 1344 1348 1352 1356 1360 1364 1368 1372 1376 1380 1384 1388 1392 1396 1404 1408 1412 1416 1420 1424 1428 1432 1436 1440 1444 1448 1452 1456 1460 1464 1468 1472 1476 1480 1484 1488 1492 1496 1504 1508 1512 1516 1520 1524 1528 1532 1536 1540 1544 1548 1552 1556 1560 1564 1568 1572 1576 1580 1584 1588 1592 1596 1600 1604 1608 1612 1616 1620 1624 1628 1632 1636 1640 1644 1648 1652 1656 1660 1664 1668 1672 1676 1680 1684 1688 1692 1696 1704 1708 1712 1716 1720 1724 1728 1732 1736 1740 1744 1748 1752 1756 1760 1764 1768 1772 1776 1780 1784 1788 1792 1796 1804 1808 1812 1816 1820 1824 1828 1832 1836 1840 1844 1848 1852 1856 1860 1864 1868 1872 1876 1880 1884 1888 1892 1896 1904 1908 1912 1916 1920 1924 1928 1932 1936 1940 1944 1948 1952 1956 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000

第10题

打印100-200之间的素数(经典)

版本1

#include<stdio.h>
int main()
{
    for(int i = 100;i <= 200; i++)
    {
        for(int j = 2;j < i; j++)
        {
            if (i % j == 0);
            {
                break;
            }
        }
        if(i == j)
        {
            printf("%d ",i);
        }
    }
    return 0;
}

版本2

#include<stdio.h>
int main()
{
    for(int i = 100;i <= 200; i++)
    {
        int flag = 1;
        for(int j = 2;j < i; j++)
        {
            if(i % j == 0)
            {
                flag = 0;
                break;
            }
        }
        if(flag == 1)
        {
            printf("%d ",i);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Williamtym

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值