《计算机程序设计》
计算机学院计算机研究所编译系统研究室
编程实现
书店图书的类型分为八大类,书的信息使用结构Book
表示。书店目前有一批孤本图书(某书只有一本),总数为TOTALNUM=16
,这些书的信息用一个数组books
来存储,书的ID号就是该书在books
数组中的序号。小明想把已有的各个类型的孤本书各买1本,请问他最少需支付多少钱?
onlybuyone.cpp
#include <iostream>
using namespace std;
#define TOTALNUM 16
struct Book {
double price; // 价格,小于100
int tpid; // 取0~7,分别对应政治、经济、社会、艺术、物理、化学、生物、地理八大类
};
double onlyBuyOne(Book books[]) {
}
int main() {
int i;
Book books[TOTALNUM]; // 孤本图书构成的数组
for (i = 0; i < TOTALNUM; i++) // 输入孤本书的信息,数组的序号作为该书本的ID号
cin >> books[i].price >> books[i].tpid;
cout << onlyBuyOne(books) << endl;
return 0;
}
只买一本:解法2
double onlyBuyOne(Book books[]) {
// 价格数组,存储每类书的最低价格,初始化为100.0
double prices[8];
for (int i = 0; i < 8; i++)
prices[i] = 100.0;
// 遍历所有书,找到同类别最低价格
for (int i = 0; i < TOTALNUM; i++)
if (books[i].price < prices[books[i].tpid])
prices[books[i].tpid] = books[i].price;
// 计算总价格
double total = 0.0;
for (int i = 0; i < 8; i++)
total += prices[i];
return total;
}
编程实现
下面有结构node
的定义,指针head
中存放着node
节点组成的单向链表的首节点地址。函数insertAfterBiggest
的功能是将新节点t
插入到head
所指向的链表中值最大的节点后面,返回新链表。
insertafterbiggest.cpp
int main() {
int n; cin >> n;
node *head = nullptr, *cur = nullptr, *tmp;
for (int i = 0; i < n; i++) {
tmp = new node;
cin >> tmp->data; tmp->next = nullptr;
if (head == nullptr) {
head = tmp;
cur = head;
} else {
cur->next = tmp;
cur = cur->next;
}
}
tmp = new node;
cin >> tmp->data;
head = insertAfterBiggest(head, tmp);
return 0;
}
在最大节点后插入新节点
考考你
如果要在最大结点之前插入,应该如何实现?
在最大节点前插入新节点
node *insertAfterBiggest(node *head, node *t) {
// 找到最大节点,同时记录前一个节点
node *biggest = head, prevbiggest = nullptr;
for (node *p = head, prev = nullptr; p; prev = p, p = p->next) {
if (p->data > biggest->data) {
biggest = p;
prevbiggest = prev;
}
}
// 插入新节点
t->next = biggest;
if (prevbiggest)
prevbiggest->next = t;
else
head = t;
return head;
}
编程实现
实现getLogCount
函数,查询在日志文件file_path
中源自日志产生源log_src
的记录有多少条
WARNING
2024/4/15 14:44:14
Microsoft-Windows-Servicing21
A reboot is necessary before the selectable update Microsoft-RemoteDesktopConnection
INFO
2024/3/15 7:48:25
Microsoft-Windows-Servicing20
Package KB5034441 was successfully changed to the Installed state.
查询给定日志源的日志条数
#include <fstream>
using namespace std;
int getLogCount(const char* file_path, char *log_src) {
int count = 0;
char line[200];
ifstream fin(file_path);
while (fin.getline(line, 200)) { // 读取第一行
fin.getline(line, 200); // 跳过第二行
fin.getline(line, 200); // 读取第三行
if (strcmp(log_src, line) == 0) // 判断是否匹配
count++;
fin.getline(line, 200); // 跳过第四行
// 当前日志已处理完毕
}
fin.close();
return count;
}
计算机程序设计