计算导论与C语言基础week8

本文精选了五道C++编程题目,包括寻找最佳赛车、比较骑车与走路效率、计算购房年限、查找序列中两数之和是否等于特定值及筛选自除整数等,旨在通过实际案例加深读者对C++语言的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

抄写题.枚举法

4名专家对4款赛车进行评论
1)A说:2号赛车是最好的;
2)B说:4号赛车是最好的;
3)C说:3号赛车不是最好的;
4)D说: B说错了。
事实上只有1款赛车最佳,且只有1名专家说对了,其他3人都说错了。
请编程输出最佳车的车号,以及说对的专家。

#include <iostream>
using namespace std;

int main() 
{
    int a, b, c, d,car;
    for (car = 1; car <= 5; car++) {
    //枚举就在这个for循环体现的,car从1到5看哪个满足条件
        a = (car == 2);
        b = (car == 4);
        c = (car != 3);
        d = (car != 4);
        if (a + b + c + d == 1)//这个是只有1个说的正确
            break;
    }
    cout << car << endl;
    if (a == 1)cout << 'A' << endl;
    else if (b == 1)cout << 'B' << endl;
    else if (c == 1)cout << 'C' << endl;
    else cout << 'D' << endl;
    return 0;
}

2.骑车与走路
描述

在北大校园里,没有自行车,上课办事会很不方便.但实际上,并非去办任何事情都是骑车快,因为骑车总要找车、开锁、停车、锁车等,这要耽误一些时间.假设找到自行车,开锁并车上自行车的时间为27秒;停车锁车的时间为23秒;步行每秒行走1.2米,骑车每秒行走3.0米.请判断走不同的距离去办事,是骑车快还是走路快.
输入

第一行为待处理的数据的数量n

其后每一行整数为一次办事要行走的距离,单位为米.
输出

对应每个整数,如果骑车快,输出一行”Bike”;如果走路快,输出一行”Walk”;如果一样快,输出一行”All”.

#include <iostream>
using namespace std;

int main() 
{
    int n,i,s;
    int b = 27 + 23;//骑车的固定消耗时间
    double v_w = 1.2, v_b = 3.0;//走路,骑车速度
    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> s;
        double t1, t2;
        t1 = b + s / v_b;
        t2 = s / v_w;
        if (t1 > t2)cout << "Walk" << endl;
        else if (t1 < t2)cout << "Bike" << endl;
        else cout << "All" << endl;

    }
    return 0;
}

3.买房子
描述

某程序员开始工作,年薪N万,他希望在中关村公馆买一套60平米的房子,现在价格是200万,假设房子价格以每年百分之K增长,并且该程序员未来年薪不变,且不吃不喝,不用交税,每年所得N万全都积攒起来,问第几年能够买下这套房子(第一年房价200万,收入N万)。程序员每年先拿工资,再尝试买房,然后房子才涨价。
输入

有多行,每行两个整数N(10 <= N <= 50), K(1 <= K <= 20)
输出

针对每组数据,如果在第20年或者之前就能买下这套房子,则输出一个整数M,表示最早需要在第M年能买下,否则输出Impossible,输出需要换行

#include <iostream>
using namespace std;

int main() 
{
    int N, K;
    while (cin >> N >> K) {
//这段是C++里多行输入(在不知道一共有多少行的情况下),每输入一组数据就可以输出其结果,不用等待所有数据都输入完毕。
        double price = 200;//price存房价
        int year = 1;
        int store = N;//年薪
        while (store < price && year <= 20){
            store += N;
            price += price*K / 100.0;
            year++;
        }
        if (store < price)cout << "Impossible" << endl;
        else cout << year << endl;
    }
    return 0;
}

4.找和为k的两个元素
描述

在一个长度为n(n < 1000)的整数序列中,判断是否存在某两个元素之和为k。

输入
第一行输入序列的长度n和k,用空格分开。
第二行输入序列中的n个整数,用空格分开。
输出
如果存在某两个元素的和为k,则输出yes,否则输出no。

#include <iostream>
using namespace std;

int main() 
{
    int n, k,i,j;
    int flag = 0;
    cin >> n >> k;
    int a[1000] = { 0 };
    for (i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (a[i] + a[j] == k) {
                flag = 1;//找到的话标志置1
                break;
            }
        }
    }
    if (flag == 1)cout << "yes" << endl;
    else cout << "no" << endl;
    return 0;
}

5.自除整数
描述

对一个整数n,如果其各个位数的数字相加得到的数m能整除n,则称n为自整除数.例如21,21%(2+1)==0,所以21是自整除数.现求出从10到n(n < 100)之间的所有自整除数.

输入
有一行,整数n,(10 <= n < 100)
输出
有多行.按从小到大的顺序输出所有大于等于10,小于等于n的自整除数,每行一个自整除数.

#include <iostream>
using namespace std;

int main() 
{
    int n, i;
    cin >> n;
    for (i = 10; i <= n; i++) {
        int a1 = i / 10;//十位
        int a2 = i % 10;//个位
        if (i % (a1 + a2) == 0)cout << i << endl;
    }
    return 0;
}

这个week8看视频课程和作业题用了4个小时,久了点》。

For Instructors: Courses Based on the Book Instructors can use the CS:APP book to teach a number of different types of systems courses. Five categories of these courses are illustrated in Figure 2. The particular course depends on curriculum requirements, personal taste, and the backgrounds and abilities of the students. From left to right in the figure, the courses are characterized by an increasing emphasis on the programmer’s perspective of a system. Here is a brief description. ORG. A computer organization course with traditional topics covered in an untraditional style. Traditional topics such as logic design, processor architecture, assembly language, and memory systems are covered. However, there is more emphasis on the impact for the programmer. For example, data representations are related back to the data types and operations of C programs, and the presentation on assembly code is based on machine code generated by a C compiler rather than handwritten assembly code. ORG+. The ORG course with additional emphasis on the impact of hardware on the performance of application programs. Compared to ORG, students learn more about code optimization and about improving the memory performance of their C programs. ICS. The baseline ICS course, designed to produce enlightened programmers who understand the impact of the hardware, operating system, and compilation system on the performance and correctness of their application programs. A significant difference from ORG+ is that low-level processor architecture is not covered. Instead, programmers work with a higher-level model of a modern out-of-order processor. The ICS course fits nicely into a 10-week quarter, and can also be stretched to a 15-week semester if covered at a more leisurely pace. ICS+. The baseline ICS course with additional coverage of systems programming topics such as system-level I/O, network programming, and concurrent programming. This is the semester-long Carnegie Mellon course, which covers every chapter in CS:APP except low-level processor architecture.翻译以上英文为中文
最新发布
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值