001:返回什麼纔好呢
輸入
多組數據,每組一行,是整數 m 和 n
輸出
先輸出一行:
123
然後,對每組數據,輸出兩行,第一行是m,第二行是n
樣例輸入
2 3
4 5
樣例輸出
123
2
3
4
5
代碼如下:
#include <iostream>
using namespace std;
class A {
public:
int val;
A(int
// 在此處補充你的代碼
x = 123) :val(x) {}
A& GetObj() { return *this; }
};
int main()
{
int m, n;
A a;
cout << a.val << endl;
while (cin >> m >> n) {
a.GetObj() = m;
cout << a.val << endl;
a.GetObj() = A(n);
cout << a.val << endl;
}
return 0;
}
002:Big & Base 封閉類問題
輸入
多組數據,每組一行,是一個整數
輸出
對每組數據,輸出兩行,每行把輸入的整數打印兩遍
樣例輸入
3
4
樣例輸出
3,3
3,3
4,4
4,4
代碼如下:
#include <iostream>
#include <string>
using namespace std;
class Base {
public:
int k;
Base(int n) :k(n) { }
};
class Big
{
public:
int v;
Base b;
// 在此處補充你的代碼
Big(int x) :v(x), b(x) {}
};
int main()
{
int n;
while (cin >> n) {
Big a1(n);
Big a2 = a1;
cout << a1.v << "," << a1.b.k << endl;
cout << a2.v << "," << a2.b.k << endl;
}
}
003:編程填空:統計動物數量
輸入
無
輸出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
樣例輸入
None
樣例輸出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
代碼如下:
#include <iostream>
using namespace std;
// 在此處補充你的代碼
class Animal {
public:
static int number;
Animal() { number++; }
virtual ~Animal() { number--; }
};
class Dog:public Animal {
public:
static int number;
Dog() { number++; }
~Dog() { number--; }
};
class Cat :public Animal {
public:
static int number;
Cat() { number++; }
~Cat() { number--; }
};
int Animal::number = 0, Dog::number = 0, Cat::number = 0;
void print() {
cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}
int main() {
print();
Dog d1, d2;
Cat c1;
print();
Dog* d3 = new Dog();
Animal* c2 = new Cat;
Cat* c3 = new Cat;
print();
delete c3;
delete c2;
delete d3;
print();
}
004:這個指針哪來的
輸入
無
輸出
10
樣例輸入
無
樣例輸出
10
代碼如下:
#include <iostream>
using namespace std;
struct A
{
int v;
A(int vv) :v(vv) { }
// 在此處補充你的代碼
const A* getPointer() const { return this; }
};
int main()
{
const A a(10);
const A* p = a.getPointer();
cout << p->v << endl;
return 0;
}
005:魔獸世界之一:備戰
總時間限制: 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分開始到雙方司令部停止製造武士爲止的所有事件按順序輸出。
一共有兩種事件,其對應的輸出樣例如下:
武士降生
輸出樣例: 004 blue lion 5 born with strength 5,2 lion in red headquarter
表示在4點整,編號爲5的藍魔lion武士降生,它降生時生命值爲5,降生後藍魔司令部裏共有2個lion武士。(爲簡單起見,不考慮單詞的複數形式)注意,每製造出一個新的武士,都要輸出此時司令部裏共有多少個該種武士。司令部停止製造武士
輸出樣例: 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<cstdio>
using namespace std;
int time, strength[5];
class wushi {
const char* name[5] = { "dragon","ninja","iceman","lion","wolf" };
int XB, ID, HP;//23410,30124
public:
wushi(int xb, int id, int hp, char* s) :XB(xb), ID(id), HP(hp) {
printf("%03d %s %s %d born with strength %d,%d %s in %s headquarter\n", time, s, name[XB], time + 1, HP, ID, name[XB], s);
}
};
class RED {
char camp[5] = "red";
int yuan, num; bool over;
int nxt[5] = { 2,3,4,1,0 }, id[5] = { 1,1,1,1,1 };
public:
RED(int M) :yuan(M), num(0), over(false) {}
void Create() {
if (over)return;
int cnt = 0;
while (yuan < strength[nxt[num]]) {
cnt++; num = (num + 1) % 5;
if (cnt == 5)break;
}
if (cnt == 5) {
over = true; return;
}
yuan -= strength[nxt[num]];
wushi(nxt[num], id[nxt[num]], strength[nxt[num]], camp);
id[nxt[num]]++; num = (num + 1) % 5;
}
bool getOver() { return over; }
};
class BLUE {
char camp[5] = "blue";
int yuan, num; bool over;
int nxt[5] = { 3,0,1,2,4 }, id[5] = { 1,1,1,1,1 };
public:
BLUE(int M) :yuan(M), num(0), over(false) {}
void Create() {
if (over)return;
int cnt = 0;
while (yuan < strength[nxt[num]]) {
cnt++; num = (num + 1) % 5;
if (cnt == 5)break;
}
if (cnt == 5) {
over = true; return;
}
yuan -= strength[nxt[num]];
wushi(nxt[num], id[nxt[num]], strength[nxt[num]], camp);
id[nxt[num]]++; num = (num + 1) % 5;
}
bool getOver() { return over; }
};
int main(void) {
int T, M;
scanf("%d", &T);
for (int i = 1; i <= T; i++) {
time = 0;
scanf("%d", &M);
for (int i = 0; i < 5; i++)scanf("%d", &strength[i]);
RED red(M); BLUE blue(M);
int flag1 = 0, flag2 = 0;
printf("Case:%d\n", i);
while (1) {
if (red.getOver() && blue.getOver())break;
red.Create();
if (red.getOver() && !flag1) { printf("%03d red headquarter stops making warriors\n", time); flag1 = 1; }
blue.Create();
if (blue.getOver() && !flag2) { printf("%03d blue headquarter stops making warriors\n", time); flag2 = 1; }
time++;
}
}
return 0;
}
選擇題