PTA第二次作业
2016-05-29 16:14
204 查看
5-1
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; class myDate { private: int year; int mouth; int day; public: myDate(int y,int m,int d):year(y),mouth(m),day(d){}; void display(); }; class myTime { private: int hour; int mini; public: myTime(int h,int min):hour(h),mini(min){}; void display(); }; void myDate::display() { printf("%04d/%02d/%02d ",year,mouth,day); } void myTime::display() { printf("%02d:%02d\n",hour,mini); } int main() { int y,m,d,h,min; int i,j; while(scanf("%d%d%d%d%d",&y,&m,&d,&h,&min) != EOF) { if(y == 0)break; myDate Date(y,m,d); myTime Time(h,min); Date.display(); Time.display(); } return 0; }
5-2
#include <iostream> #include <cstdio> #include <cstdlib> #include <string.h> using namespace std; class calculate { public: int s[105]; calculate() { memset(s,0,sizeof(s)); } void found(); }; void calculate::found() { int l,r; int i,j; l = r = 1; int tot = 0; int cnt = 0; for(i = 1; i <= 7; i++)//i左 { cnt = 0; for(j = i; j <= 7; j++)//j右 { cnt += s[j]; if(cnt > tot) { tot = cnt; l = i; r = j; } else if(cnt == tot) { if(r - l <= j - i && tot == cnt) //相等的时候 取区间范围比较小的 continue; tot = cnt; l = i; r = j; } } } if(tot <= 0) printf("won't buy!\n"); else printf("%d %d %d\n",tot,l,r); } int main() { int i,j; while(1) { calculate cal; scanf("%d%d%d%d%d%d%d",&cal.s[1],&cal.s[2],&cal.s[3], &cal.s[4],&cal.s[5],&cal.s[6],&cal.s[7]); if(cal.s[1] == 0 && cal.s[2] == 0 && cal.s[3] == 0 && cal.s[4] == 0 && cal.s[5] == 0 && cal.s[6] == 0 && cal.s[7] == 0)break; cal.found(); } return 0; } //-1 -1 1 -1 1 -1 1
5-3
继承
#include <cstdio> #include <iostream> #include <sstream> #include <string> using namespace std; class Group { protected: string name;//姓名 public: virtual void display()=0;//显示考核成绩 }; //==== class Group_A : public Group { public: int getscore; void display(); Group_A(string s,int score):getscore(score){name = s;}; }; void Group_A::display() { cout << name << " " << 'A' << " " << getscore << endl; } //==== class Group_B : public Group { public: int getscore; void display(); Group_B(string s,int score):getscore(score){name = s;}; }; void Group_B::display() { cout << name << " " << 'B' << " " << getscore << endl; } //==== int cal_Ascore(string s,int len) { int i,j; int win = 0; int lose = 0; int tot = 0; string play; stringstream stream; for(i = 3 + len; i < s.length(); i++) { if(s[i] == ' ') { if(play != "") { stream << play; stream >> win; stream.clear(); play = ""; } continue; } play += s[i]; } if(play != "") { stream << play; stream >> lose; stream.clear(); } tot = 2 * win - lose; return tot; } int cal_Bscore(string s,int len) { int tot = 0; int pre = 0; int beh = 0; int i,j; string play; stringstream stream; for(i = 3 + len; i < s.length(); i++) { if(s[i] == ':') { stream << play; stream >> pre; stream.clear(); play = ""; continue; } else if(s[i] == ' ') { stream << play; stream >> beh; stream.clear(); if(pre > beh) { tot += pre - beh; } else if(pre < beh) { tot -= beh - pre; } pre = 0; beh = 0; play = ""; continue; } else { play += s[i]; } } if(play != "") { stream << play; stream >> beh; stream.clear(); if(pre > beh) { tot += pre - beh; } else if(pre < beh) { tot -= beh - pre; } } return tot; } //==== int main() { Group *pg[20]; int cnt = 1; int A_win; int A_lose; int i,j; while(1) { string Gsta; getline(cin,Gsta); if(Gsta[0] == '0')break; string name; int namelen = 0; int score = 0; for(i = 2; i < Gsta.length(); i++) { if(Gsta[i] == ' ')break; name += Gsta[i]; namelen ++; } if(Gsta[0] == 'A') { score = cal_Ascore(Gsta,namelen); pg[cnt++] = new Group_A(name,score); //Group_A A1(name,score); //A1.display(); } else if(Gsta[0] == 'B') { score = cal_Bscore(Gsta,namelen); pg[cnt++] = new Group_B(name,score); //Group_B B1(name,score); //B1.display(); } } for(i = 1; i < cnt; i++) { pg[i] -> display(); } return 0; }
5-4
说好的顺序表写成了链表orz。
#include <cstdio> #include <iostream> #include <cstdlib> #include <string> #define LEN sizeof(SeqList) using namespace std; struct SeqList { int data; SeqList* next; }; typedef SeqList* point; point List_Creat(int tot) { point head; point p1,p2; head = (point)malloc(LEN); if(head == NULL) { printf("Overflow\n"); exit(1); } head = NULL; p1 = (point)malloc(LEN); if(p1 == NULL) { printf("Overflow\n"); exit(1); } scanf("%d",&p1 -> data); if(tot == 1) { head = p1; head -> next = NULL; return head; } for(int i = 1; i < tot; i++) { if(head == NULL) { head = p1; } else { p2 -> next = p1; } p2 = p1; p1 = (point)malloc(LEN); if(p1 == NULL) { printf("Overflow\n"); exit(1); } scanf("%d",&p1 -> data); } p2 -> next = p1; p1 -> next = NULL; return head; } point Print(point head) { point p; p = head; printf("%d",p -> data); p = p -> next; while(p != NULL) { printf(" %d",p -> data); p = p -> next; } printf("\n"); } point Insert(point head,int num) { point p,p1,p2; p = (point)malloc(LEN); if(p == NULL) { printf("Overflow\n"); exit(1); } p -> data = num; p -> next = NULL; p1 = p2 = head; if(head -> data >= num) { p -> next = head; head = p; return head; } else { while(p1 -> next != NULL) { if(p1 -> data >= num) { break; } else { p2 = p1; p1 = p1 -> next; } } if(p1 -> next == NULL && p1 -> data < num) { p1 -> next = p; p -> next = NULL; } else { p2 -> next = p; p -> next = p1; } } return head; } int main() { int t; int i,j; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); point head; head = List_Creat(n); int i_data; scanf("%d",&i_data); head = Insert(head,i_data); printf("size=%d:",n + 1); Print(head); } return 0; }
相关文章推荐
- android开发_短信smsManager的导包问题
- 参数模型和非参数模型的区别
- 【DAY7】第七天的关于异常的练习
- Eclipse Debug不为人知的秘密
- 【设计模式】创建型模式之工厂方法Factory Method
- 基因数据处理37之bdg-formats编译成功
- [c/c++]complex类的标准库实现
- **和*的区别
- 深入浅出讲解:php的socket通信
- Excel导出
- scrapy爬取动态分页内容
- canvas基础学习(二)-线条的属性与星空和图形变换
- <LeetCode OJ> 24. Swap Nodes in Pairs
- python学习 变量的操作 与 基本数据类型
- 米老师讲课:到底怎么学习
- QT cannot find -lGL
- python unittest 之mock学习笔记
- Happy Number
- Win10 Mobile将迎来原生的Windows10通知同步功能
- opencl:原子命令实现自旋锁(spinlock)的使用限制