计算导论与C语言基础week7

本文介绍了C++编程中的几个实用案例,包括使用setprecision控制浮点数输出精度、统计疾病与年龄的关系、查找第k大的数及人民币支付问题的解决方法。

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

1.setprecision控制输出流显示浮点数的有效数字个数

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    float a = 0, b = 0;
    a = 123456.789e5;
    b = a + 20;
    cout << setprecision(20) << b << endl;
    return 0;
}

手算结果是12345678920
但这段代码结果是12345678848
因为浮点数精度只有7位,超过7位就不准确-用浮点数运算时应避免大+小(数位相差过多)

IEEE754,浮点数

作业题
1.年龄与疾病
描述

某医院想统计一下某项疾病的获得与否与年龄是否有关,需要对以前的诊断记录进行整理。
输入

共2行,第一行为过往病人的数目n(0 < n <= 100),第二行为每个病人患病时的年龄。
输出

每个年龄段(分四段:18以下,19-35,36-60,大于60注意看样例输出的格式)的患病人数占总患病人数的比例,以百分比的形式输出,精确到小数点后两位(double)。关于c++的格式化的输入输出,请参考:http://www.cplusplus.com/reference/iomanip。也可以在网上搜索一下,资料很多的。

#include <iostream>
#include <iomanip>
//输出可以用 cout<<fixed<<setprecision(2) << f; 来保留f后面的两位小数
using namespace std;

int main() 
{
    int n, age;
    int a, b, c, d, i,f;
    cin >> n;
    a = 0;
    b = 0;
    c = 0;
    d = 0;
    for (i = 0; i < n; i++) {
    //逐个输入判断
        cin >> age;
        if (age > 60)d++;
        else if (age >= 36)c++;
        else if (age >= 19)b++;
        else a++;
    //这里从60往下比从1-18往上要好看点
    }
    cout << "1-18: " << fixed << setprecision(2) << a*100.0 / n << "%\n";
    //注意%在c++中可以直接打出来,\n会换行
    cout << "19-35: " << fixed << setprecision(2) << b*100.0 / n << "%\n";
    cout << "36-60: " << fixed << setprecision(2) << c*100.0 / n << "%\n";
    cout << "60-: " << fixed << setprecision(2) << d*100.0 / n << "%";
    cin >> f;
    //这个cin是为了debug中看结果,没有实际含义,防止闪退出来,记得有其他办法的,这个简单.
    return 0;
}

3.找出第k大的数
描述

用户输入N和K,然后接着输入N个正整数(无序的),程序在不对N个整数排序的情况下,找出第K大的数。注意,第K大的数意味着从大到小排在第K位的数。
输入

N

K

a1 a2 a3 a4 ….. aN
输出

b


#include <iostream>
using namespace std;
//http://blog.sina.com.cn/xqfphd参考这个的解法五
int main() 
{
    int n, k, i, num,f;//f仍然是为了debug用的
    cin >> n;
    cin >> k;
    int a[1000] = { 0 };//长度1000的数组初始化为0

    for (i = 0; i < n; i++) {
        cin >> num;
        a[num - 1] += 1;
    }
//a[i]存的是i这个数出现的次数

    int sumCount, v;
    for (sumCount = 0, v = 999; v >= 0; v--) {
        sumCount += a[v-1];
        if (sumCount >= k)
            break;
    }
//sumCount是从大到小的数的序号,加到大于等于k的时候,v就是第k大的
    cout << v <<endl;
    cin >> f;
    return 0;
}
//这个实现既浪费时间也浪费空间,只不过自己写的简单...

4.人民币支付
描述

从键盘输入一指定金额(以元为单位,如345),然后输出支付该金额的各种面额的人民币数量,显示100元,50元,20元,10元,5元,1元各多少张,要求尽量使用大面额的钞票。
输入

一个小于1000的正整数。
输出

输出分行,每行显示一个整数,从上到下分别表示100元,50元,20元,10元,5元,1元人民币的张数

#include <iostream>
using namespace std;

int main() 
{
    int m,rest;
    cin >> m;
    int a, b, c, d, e, f;
    //100,50,20,10,5,1六种面值,感觉下面的代码不用一个一个重复写下来
    a = m / 100;
    rest = m % 100;
    b = rest / 50;
    rest = rest % 50;
    c = rest / 20;
    rest = rest % 20;
    d = rest / 10;
    rest = rest % 10;
    e = rest / 5;
    rest = rest % 5;

    cout << a<<endl << b<<endl << c<<"\n" << d<<"\n" << e<<"\n" << f<<"\n";
//这里试了下打换行,endl可以,"\n"也可.有更简单点的输出方式吗,这里太多<<
    return 0;
}

不得不说这段代码丑爆了,大晚上的先A了再说,你先丑着吧..

前几个小时把第2题写上来了,还有个最大公约数的欧几里得算法实现,但草稿不知怎搞的丢了,引以为戒,注意随手保存。

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、付费专栏及课程。

余额充值