第三周作业
2017-06-30 20:51
585 查看
编程题#1
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)注意: 总时间限制: 1000ms 内存限制: 65536kB
请填空:
#include <iostream> using namespace std; class A { public: int val; // 在此处补充你的代码 }; main() { A a; cout << a.val << endl; a.GetObj() = 5; cout << a.val << endl; }
使得程序输出:
0 5
源码:
#include <iostream> #include <stdio.h> using namespace std; class A { public: int val; A(){ val=0; } int & GetObj(){ return val; } }; int main() { A a; cout << a.val << endl; a.GetObj() = 5; cout << a.val; return 0; }
编程题 #2
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
请补足Sample类的成员函数。不能增加成员变量。
#include <iostream> using namespace std; class Sample{ public: int v; Sample(int n):v(n) { } // 在此处补充你的代码 }; int main() { Sample a(5); Sample b = a; cout << b.v; return 0; }
使程序输出:
10
源码:
#include <iostream> #include <stdio.h> using namespace std; class Sample{ public: int v; Sample(int n):v(n) { } Sample(const Sample &a){ v=a.v*2; } }; int main() { Sample a(5); Sample b = a; cout << b.v<<endl; return 0; }
编程题 #3
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
请填空:
#include <iostream> using namespace std; class Base { public: int k; Base(int n):k(n) { } }; class Big { public: int v; Base b; // 在此处补充你的代码 }; int main() { Big a1(5); Big a2 = a1; cout << a1.v << "," << a1.b.k << endl; cout << a2.v << "," << a2.b.k << endl; return 0; }
使程序输出:
5,5 5,5
源码
#include <iostream> #include <st d6ce dio.h> using namespace std; class Base { public: int k; Base(int n):k(n) { } }; class Big { public: int v; Base b; Big(int d):v(d),b(d){} Big(const Big &c):v(c.v),b(c.v){} }; int main() { Big a1(5); Big a2 = a1; cout << a1.v << "," << a1.b.k << endl; cout << a2.v << "," << a2.b.k << endl; return 0; }
编程题#4:魔兽世界之一:备战
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。
红司令部,City 1,City 2,……,City n,蓝司令部
两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值、攻击力这三种属性。
双方的武士编号都是从1开始计算。红方制造出来的第n个武士,编号就是n。同样,蓝方制造出来的第n个武士,编号也是n。
武士在刚降生的时候有一个生命值。
在每个整点,双方的司令部中各有一个武士降生。
红方司令部按照iceman、lion、wolf、ninja、dragon的顺序循环制造武士。
蓝方司令部按照lion、dragon、ninja、iceman、wolf的顺序循环制造武士。
制造武士需要生命元。
制造一个初始生命值为m的武士,司令部中的生命元就要减少m个。
如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。
给定一个时间,和双方司令部的初始生命元数目,要求你将从0点0分开始到双方司令部停止制造武士为止的所有事件按顺序输出。
一共有两种事件,其对应的输出样例如下:
1) 武士降生
输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter
表示在4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。
2) 司令部停止制造武士
输出样例: 010 red headquarter stops making warriors
表示在10点整,红方司令部停止制造武士
输出事件时:
首先按时间顺序输出;
同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。
输入
第一行是一个整数,代表测试数据组数。
每组测试数据共两行。
第一行:一个整数M。其含义为, 每个司令部一开始都有M个生命元( 1 <= M <= 10000)。
第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000。
输出
对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。
对每组测试数据,首先输出”Case:n” n是测试数据的编号,从1开始 。
接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。
要求输入:
1 20 3 4 5 6 7
要求输出:
Case:1 000 red iceman 1 born with strength 5,1 iceman in red headquarter 000 blue lion 1 born with strength 6,1 lion in blue headquarter 001 red lion 2 born with strength 6,1 lion in red headquarter 001 blue dragon 2 born with strength 3,1 dragon in blue headquarter 002 red wolf 3 born with strength 7,1 wolf in red headquarter 002 blue ninja 3 born with strength 4,1 ninja in blue headquarter 003 red headquarter stops making warriors 003 blue iceman 4 born with strength 5,1 iceman in blue headquarter 004 blue headquarter stops making warriors
源码:
#include <iostream> #include <string> #include <vector> #include <math.h> #include <iomanip> #include <stdio.h> using namespace std; const string ss[]={"iceman","lion","wolf","ninja","dragon"}; const vector<string> soldier(ss,ss+sizeof(ss)/sizeof(string)); const string s[]={"red","blue"}; const vector<string> colors(s,s+2); int red_sum[]={0,0,0,0,0}; int blue_sum[]={0,0,0,0,0}; int redlife; int bluelife; vector<int> blood(5,0); vector<int> bloodd; int bll(const int &a){ switch(a%5){ case 0: {return 1;break;} case 1: {return 4;break;} case 2: {return 3;break;} case 3: {return 0;break;} case 4: {return 2;break;} break; default: {return 9;break;} } } class gs{ private: int soldier_blood; string name;//具体什么士兵 string color;//司令部颜色 friend void shengming(string b); int i,j; public: //构造函数,a代表是红的[0]还是蓝的[1],b代表第几个士兵 gs(const int &a,const int &b){ i=b;j=a; if(a==0){ color=colors[a]; name=soldier[b%5]; soldier_blood=blood[b%5]; redlife-=soldier_blood; ++red_sum[b%5]; }else if(a==1){ color=colors[a]; name=soldier[bll(b)]; soldier_blood=blood[bll(b)]; bluelife-=soldier_blood; ++blue_sum[bll(b)]; } } void out(){ if(j==0){cout<<setw(3)<<setfill('0')<<i<<" "<<color<<" "<<name<<" "<<i+1<<" "<<"born with strength "<<soldier_blood<<","<<red_sum[i%5]<<" "<<name<<" in red headquarter"<<endl;} if(j==1){cout<<setw(3)<<setfill('0')<<i<<" "<<color<<" "<<name<<" "<<i+1<<" "<<"born with strength "<<soldier_blood<<","<<blue_sum[bll(i)]<<" "<<name<<" in blue headquarter"<<endl;} } }; void shengming(const int &a,const string &b){ string temp; //初始化司令部总生命 redlife=a; bluelife=a; int str2int(string str); //初始化每种士兵血量 for(int i=0;i<b.size();++i){ if(isdigit(b[i]) || i==b.size()-1){ if(i==b.size()-1){ temp+=b[i]; bloodd.push_back(str2int(temp)); break; } temp+=b[i]; }else if(!isgraph(b[i]) || i==b.size()-1){ if(i==b.size()-1){ temp+=b[i]; bloodd.push_back(str2int(temp)); break; } bloodd.push_back(str2int(temp)); temp=""; } } blood[0]=bloodd[2]; blood[1]=bloodd[3]; blood[2]=bloodd[4]; blood[3]=bloodd[1]; blood[4]=bloodd[0]; } int str2int(string str){ int sum=0; for(int i=str.size()-1;i>-1;--i){ sum+=((int)str[i]-48)*(pow(10,(str.size()-i-1))); } return sum; } int main(){ int a,c,pan=0,i=0,red_i=0,blue_i=0; vector<gs> red_all; vector<gs> blue_all; string b; cin>>a>>c; cin.get(); getline(cin,b); shengming(c,b); cout<<"Case:"<<a<<endl; while(pan==0){ gs *p1=new gs(0,i); gs *p2=new gs(1,i); if (redlife<0 && red_i==0){cout<<setw(3)<<setfill('0')<<i<<" red headquarter stops making warriors"<<endl;red_i=1;} if (bluelife<0 && blue_i==0){cout<<setw(3)<<setfill('0')<<i<<" blue headquarter stops making warriors"<<endl;blue_i=1;} if(redlife>0){ p1->out(); } if(bluelife>0){ p2->out(); } red_all.push_back(*p1); blue_all.push_back(*p2); delete p1; delete p2; ++i; if(redlife<0 && bluelife<0){ pan=1; } } return 0; }