编程珠玑第三章笔记加习题解答
1.感悟
看到数据第一时间应该想用什么数据结构去表示而不是直接就下手,比如100个if判断完全可以把判断条件放在数组中然后遍历数组判断,从而节省代码量,并且看到有序数据就要想到用二分查找去搜索
2.习题解答
1.分段税收,看到这种不断递增的税收直接二分查找
#include <iostream>
using namespace std;int basetax[100]; //固定交税的钱
int lowerbound[100]; //分区金钱
double taxrate[100]; //利率int find(int left, int right, int Income)
{int l = left;int r = right;while (l <= r){int mid = l + (r - l) / 2;if (Income - lowerbound[mid] < 500 && Income - lowerbound[mid] >= 0)return mid;else if (Income - lowerbound[mid] < 0) //目标值在左侧r = mid - 1;elsel = mid + 1;}return 0;
}int main()
{int left = 0;int right = 99;//给数组赋值for (int i = left; i < right; i++){if (i == 0)lowerbound[i] = 0;elselowerbound[i] = 2200 + (i-1) * 500;if (i == 0 || i == 1) basetax[i] = 0;elsebasetax[i] = 75 * i - 80;if (i == 0) taxrate[i] = 0;elsetaxrate[i] = (double)(14 + i - 1) / 100;}int Income;cout << "请输入收入" << endl;cin >> Income;int val = find(left, right, Income);double tax = basetax[val] + (double)taxrate[val] * (Income - lowerbound[val]);cout << "交的税为" << endl;cout << tax;return 0;
}
2.求多项式系数
没啥多说的暴力就行
3.编写函数,输入一个大写字母,输出一个字符数组,该字符数组用字符图形方式表示该字母
这个题就是对应转换
4.给定两个日期,计算两者之间的天数;给定一个日期,返回值为周几;给定月和年,使用字符数组生成该月的日历
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;vector<int>monthDay = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };class Date
{public:Date(int year, int month, int day) :m_year(year), m_month(month), m_day(day){}int dateDay();int betweenDay(const Date& date);int judgeWeekDay();void printCalendar();
private:bool judgeLeap(const int year);int m_year;int m_month;int m_day;
};//判断某年是不是闰年
bool Date::judgeLeap(const int year)
{if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return true;}return false;
}//判断日期是这年第几天
int Date::dateDay()
{int allday = 0;for (int i = 1; i < this->m_month; i++){allday += monthDay[i];}if (this->m_month > 2 && judgeLeap(this->m_year)){allday += 1;}return allday + this->m_day;
}//计算两个日期的间隔天数
int Date::betweenDay(const Date&date)
{int totalDays = 0;Date minDate = this->m_year < date.m_year ? *this : date;Date maxDate = this->m_year < date.m_year ? date : *this;if (this->m_year == date.m_year)return abs(this->dateDay() - const_cast<Date&>(date).dateDay());else{for (int i = minDate.m_year; i < maxDate.m_year; i++){totalDays += judgeLeap(i) ? 366 : 365;}return totalDays + maxDate.dateDay() - minDate.dateDay();}
}//判断日期是这周星期几
int Date::judgeWeekDay()
{Date td(1900, 1, 1);return this->betweenDay(td) % 7 + 1;
}//打印某年某月月历
void Date::printCalendar()
{printf("%d年%d月月历\n", this->m_year, this->m_month);printf("日 一 二 三 四 五 六\n");this->m_day = 1;int firstDay = this->judgeWeekDay();int i;if (firstDay != 7) //先打印第一行日期{for (i = 0; i < firstDay; i++){printf(" ");}for (i = 1; i <= 7 - firstDay; i++) //把第一周打印完,从firstDay开始的星期几到星期天 {printf("%3d", i);}cout<<endl;}int monthday = (this->m_month == 2) ? (this->judgeLeap(this->m_year) ? 29 : 28) : monthDay[this->m_month];for (i = 8 - firstDay; i <= monthday; i++){printf("%-3d", i);if (i % 7 == 7 - firstDay)printf("\n");}printf("\n");
}int main()
{Date d(2019,10,21);d.printCalendar();return 0;
}
5.查找后缀连字符的连接
没啥好说的直接用find函数查找
#include<iostream>
#include<vector>
#include<string>
using namespace std;int main()
{vector<string>v = { "et-ic", "al-is-tic", "s-tic", "p-tic", "-lyt-ic", "ot-ic", "an-tic", "n-tic", "c-tic", "at-ic", "h-nic", "n-ic", "m-ic", "l-lic", "b-lic", "-clic", "l-ic", "h-ic", "f-ic", "d-ic", "-bic", "a-ic", "-mac", "i-ac" };string s;while (cin >> s){for (auto temp : v){int i = s.find(temp);if (i != -1){cout << temp;break;}}}return 0;
}
6.就是写一个文件这个文件有好多人的姓名,年龄啥的然后用正则匹配替换
7.完全看不懂在干嘛
8.好吧也没看懂
编程珠玑第三章笔记加习题解答相关推荐
- Unix网络编程卷一第三章笔记
前言 这篇文章主要是Unix网络编程卷一第三章的个人笔记 1.POSIX 规范的三个字段 sin_family sin_addr sin_port 2.IPV4 套接字结构 五个套接字结构 IPV4( ...
- 《高等工程数学》吴孟达版 第三章部分课后习题解答
啊!时间过得太快了,开学已经两个多月了,数学都学到第三章了,自我感觉很混乱,学习一知半解,没能很好的消化,小脑袋瓜子不够灵活哇-- 第三章只做了部分习题,还有很多没太明白,加油ing
- 编程珠玑第三章习题答案
1 税收问题 .if-else语句的每个分支的形式都差不多,我们可以用数组来使循环简单一点.数组中每个点表明一个阶段,用level[i]表示阶段i的起始点,tax[i]表示阶段i的税率.然后就是输入一 ...
- 编程珠玑之第二章习题5
问题描述: n元一维向量旋转问题数将向量ab变为ba.如何将向量abc变为cba? (这对交换非相邻内存块问题进行了建模) 问题解析: 1.这里需要用到一个重要的性质:CBA=(ArBrCr)r ...
- 【编程珠玑】陪着奶猫看看书--《编程珠玑》第一章
陪着奶猫看看书–<编程珠玑>第一章 首先说说小奶猫我为什么要读<编程珠玑>这本神作,当年小奶猫刚刚进入大学时候是个纯洁的少年,啥都不懂,要是哪个女生下午在外面问我带身份证没有, ...
- IA-32系统编程指南 - 第三章 保护模式的内存管理【1】
第三章 保护模式的内存管理[1] [作者:lion3875 原创文章 参考文献<Intel 64 and IA-32 system programming guide>] IA-32保护模 ...
- Java7并发编程指南——第三章:线程同步辅助类
Java7并发编程指南--第三章:线程同步辅助类 @(并发和IO流) Java7并发编程指南第三章线程同步辅助类 思维导图 项目代码 思维导图 项目代码 GitHub:Java7Concurrency ...
- ROS2机器人编程简述humble-第三章-COMPUTATION GRAPH .2
ROS2机器人编程简述humble-第三章-PERCEPTION AND ACTUATION MODELS .1 避开障碍物计算图如何呢? 该应用程序的计算图非常简单:订阅激光主题的节点向机器人发布速 ...
- 算法图解第六章笔记与习题(广度优先搜索)
算法图解第六章笔记与习题(广度优先搜索) 文章目录 算法图解第六章笔记与习题(广度优先搜索) 6.1 图(graph) 6.2 广度优先搜索 6.3.1 查找最短路径 6.3.2 队列 6.4 实现图 ...
- 算法图解第二章笔记与习题(选择排序)
算法图解第二章笔记与习题(选择排序) 文章目录 算法图解第二章笔记与习题(选择排序) 2.2 数组和链表 2.2.1 链表 2.2.2 数组 2.2.3 数组和链表的读取.插入和删除操作的时间复杂度 ...
最新文章
- python 将字符串转换成字典dict
- 开启报名 | 清华游凯超:预训练模型这么多,该如何选择?一种提速3000倍的高效方法...
- lvs+keepalived简单配置
- office2013软件安装资料及教程
- OpenCASCADE :VTK集成服务(VIS) 之使用低级 API
- wxWidgets:wxAUI 概述
- Floyd求传递闭包
- 转-SpringBoot——使用外置的Tomcat服务器
- xbox手柄接收器驱动_xbox手柄连接 win10电脑
- 神经网络为什么需要激活函数
- 欢迎使用CSDN-markdown编辑器-入门
- 利用display属性写的遮罩层
- mxnet导入图像数据
- appium之adb常用命令
- Nginx禁止直接通过IP地址访问网站以及限制IP登陆某目录(关闭默认站点或空主机头)...
- 海思Hi3798硬件设计,Hi3798 datasheet(2)参考资料
- Python3制作百度文库免费下载器
- 短信工具类 SmsUtil
- hba卡在服务器什么位置,设置服务器通过HBA卡启动目前服务器主流使用的是Qlogic和Emulex.doc...
- 全国省市县三级数据库