编程珠玑第三章习题

来源:互联网 发布:智慧城市 大数据 编辑:程序博客网 时间:2024/05/24 16:15

1.使用二分搜索来搜索大于等于income的最小的lower_limit

#include "stdafx.h"#include <iostream>using namespace std;int binarysearch(int start, int end, int find, int* a);int main(int argc, char* argv[]){int lower_limit[40];int iter;int find;for(int i = 0; i < 40; i++) //初始化下限数组{lower_limit[i] = 2200 + i * 500; }cin >> find;while(find != -1){iter = binarysearch(0,39,find,lower_limit);cout << lower_limit[iter] <<endl;cin >> find;}return 0; }int binarysearch(int start, int end, int find, int* a){int mid;if(start == end)return end;else {mid = (start + end)/2;if(find <= a[mid])return(binarysearch(start,mid,find,a));elsereturn(binarysearch(mid+1,end,find,a)); }} 

其实根本不用那么麻烦。。。还要用上二分搜索

其实观察书中下限的数据为2200,2700,3200,3700。。。

就是2200+i*500

那么只要将要找的数据find: 

  iter = (find-2200)/500;

  if((find-2200)%500 == 0)

return iter;

  else 

return iter +1;

就可以了,不用搜索。

2.没意思的递归

3.有点费时间,有时间再做

4.有意思的题:给定两个日期,计算两者之间的天数;给定一个日期,返回值为周几;给定月和年,使用字符数组生成该月的日历

先看下关于题目背景的科普知识:

问题的提出:日历的编排是每400年一个大循环周期,即今年的月、日、星期几和400年前的完全一样。现行天文历法根据天体运行规律,取每年365.2425天。这样,每400年共有365.2425×400=146097天。如果以365天作为一年,每400年就少了0.2425×400=97天。这97天要靠设置闰年(每年366)天来凑齐,所以,每400年要设置97个闰 -Of the problem: the structure of the calendar every 400 years is a big cycle, that is, this year' s month, day, week and 400 a few years ago, exactly the same. Existing astronomical calendar to run in accordance with the laws of celestial bodies, 365.2425 days a year access. In this way, every 400 years a total of 365.2425 × 400 = 146097 days. As if 365 days a year, every 400 years less 0.2425 × 400 = 97 days. This is a leap year 97 days depends on the set (366 a year) days to put together, so every 400 years a leap to set up 97

#include "stdafx.h"#include <iostream>using namespace std;struct date{int year;int mouth;int day;};int mouth_date[12] = {31,28,31,30,31,30,31,31,30,31,30,31};int MakeCalender(int year, int mouth);void PrintCalender(int lines);int DateToDays(struct date d);int Cal[6][7];void InitialCal(){for(int i = 0; i < 6; i++)for(int j = 0; j < 7; j++)Cal[i][j] = 0;}int main(int argc, char* argv[]){int lines = MakeCalender(2012,1);PrintCalender(lines);return 0;}int IsLeapYear(int year)//能被400整除的年份 或 不能被100整除却能被4整除的年份是闰年. {if((year%400 == 0) || ((year%4== 0) && (year%100 != 0)))return 1; //是闰年else return 0;}int cmpDate(struct date cmp, struct date cmp1) //如果cmp < cmp1,则返回-1,大于返回1,等于返回0{if(cmp.year < cmp1.year)return -1;if(cmp.year > cmp1.year)return 1;if(cmp.mouth < cmp1.mouth)return -1;if(cmp.mouth > cmp1.mouth)return 1;if(cmp.day < cmp1.day)return -1;if(cmp.day > cmp1.day)return 1;return 0;}int IntervalDays(struct date begin, struct date end){return DateToDays(end) - DateToDays(begin);}int DateToDays(struct date d){int days = 0;//int temp;days += (d.year - 1)*365 + (d.year - 1)/4 - (d.year-1)/100 + (d.year -1 )/400;if(IsLeapYear(d.year))mouth_date[1] = 29;for(int i = 1; i < d.mouth; i++){days += mouth_date[i-1];}days += d.day;mouth_date[1] = 28;return days;}int XingQi(struct date d){struct date today;today.year = 2011;today.mouth = 12;today.day = 30;int xingqi = 5;int interval = 0;if(cmpDate(d,today) == -1){interval = IntervalDays(d,today) % 7;xingqi = (xingqi + (7 - interval))%7;return xingqi;}if(cmpDate(d,today) == 1){interval = IntervalDays(today,d) % 7;xingqi = (xingqi + interval)%7;return xingqi;return xingqi;}int MakeCalender(int year, int mouth){struct date temp;temp.year = year;temp.mouth = mouth;temp.day = 1;if(IsLeapYear(year))mouth_date[1] = 29;int j = XingQi(temp);for(int i = 0; i <mouth_date[mouth-1]; i++){Cal[j/7][j%7] = i+1;j++;}return (j-1)/7 + 1;    //返回行数}void PrintCalender(int lines){//Cal[0] ={'日','一','二','三','四','五','六'}cout<<"\t日"<<"\t一"<<"\t二"<<"\t三"<<"\t四"<<"\t五"<<"\t六"<<endl;for(int i = 0; i <lines; i++){for(int j = 0; j < 7; j++){if(Cal[i][j]!=0)cout<<'\t'<<Cal[i][j];elsecout<<'\t';}cout << endl;}}

看到别人写的cmp函数很优雅,贴上来当学习了

//比较时间  int datecmp(date a, date b)  {      if (a.year != b.year) return a.year - b.year;      if (a.month != b.month) return a.month - b.month;      return a.day - b.day;  }  


5.写了一个很弱的程序

// ProgramPearl3_5.cpp : Defines the entry point for the console application.//#include "stdafx.h" #include<stdio.h>#include<stdlib.h>#include<string>#include<iterator>#include<iostream>using namespace std;#define MAX 10;char* pp[24] = {"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"};int IsEqual(const char* a, const char *b);char* reverseString(char * p);int main(){char p[24][10];for(int i = 0; i < 24; i++){strcpy(p[i],pp[i]);reverseString(p[i]);}//qsort(a,24,sizeof(struct affix),cmp);char find[10] = "eth-nic";cout << p[10] << endl;reverseString(find);cout << find << endl;for( i = 0; i < 24; i++){if(IsEqual(find,p[i]))break;}cout << i << endl;return 0;}char* reverseString(char * p){int n = strlen(p);char temp;for(int i = 0; i <= (n/2); i++){temp = p[i];p[i] = p[n-1-i];p[n-1-i] = temp;}return p;}int IsEqual(const char* a, const char *b){for(int i = 0; i < strlen(b); i++){if((b[i] != a[i]) || (a[i] == '\0'))break;}if(i == strlen(b))return 1;else return 0;}








原创粉丝点击