[1027] 火焰纹章

[题目传送] http://djks.nbut.edu.cn:8090/JudgeOnline/problem.php?id=1027

[思路]

这题被多名小伙伴举报,举报理由是“有毒”,这种大模拟题,涉及了查询和修改等等操作的程序,往往需要大量的数据存储,一旦操作不好,就会操作错误。尽量使用数据封装的属性,不要一堆数组,是这种题做对的关键。

好吧,然后说说这题题目没有说清楚的几个地方,比如攻击,如果攻击了不存在的人(被攻击者不存在,下标超过n),或者是你想要实施攻击动作的这个攻击者不存在,也输出操作失败。然后医生医疗操作的时候,看看给你的下标是不是不存在啊 ! = = 。。。

注意以上的几点,应该不会有错了,如果有错,1.请认真读题 2.请认真读题 3.请用类或者结构体减轻下自己眼睛的负担吧。

[代码]

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4  
 5 using namespace std;
 6  
 7 class AMY{
 8 private:
 9     int man[110];
10     int doc;
11     int atck;
12     int life;
13     int num;
14 public:
15     AMY(int _n , int _H , int _A , int _d){
16         num = _n;
17         life = _H;
18         atck = _A;
19         doc = _d;
20         for(int i = 1 ; i <= num ; i++) man[i] = life;
21     }
22     AMY(){}
23     ~AMY(){}
24     //////////////////////
25     friend bool Doing (AMY & A , AMY & B , int a , int b){
26         int n = A.num;
27         if(a > n || a < 1 || b > n || b < 1) return 0;
28         if(a == A.doc) return 0;
29         if(B.man[b] <= 0) return 0;
30         //////////////
31         int at = A.atck;
32         B.man[b] -= at;
33         return 1;
34     }
35     bool save(int a){
36         if(a < 1 || a > num) return 0;
37         if(man[a] <= 0) return 0;
38         if(man[doc] <= 0) return 0;
39         man[a] += life;
40         return 1;
41     }
42     int show(){
43         int ans = 0;
44         for(int i = 1 ; i <= num ; i++){
45             if(man[i] > 0) ans++;
46         }
47         return ans;
48     }
49 };
50  
51 void op_1(AMY & WO , AMY & DI){
52     int op2 , a , b; cin >> op2 >> a >> b;
53     bool flag = 0;
54     if(op2 == 0) flag = Doing(WO,DI,a,b);
55     else flag = Doing(DI,WO,a,b);
56     ///////////
57     if(!flag) cout << "Failed operation" << endl;
58     return ;
59 }
60  
61 void op_2(AMY & WO , AMY & DI){
62     int op2 , a ; cin >> op2 >> a;
63     bool flag = 0;
64     if(op2 == 0) flag = WO.save(a);
65     else flag = DI.save(a);
66     /////////
67     if(!flag) cout << "Failed operation" << endl;
68     return ;
69 }
70  
71 void op_3(AMY & WO , AMY & DI){
72     int get1 = WO.show();
73     int get2 = DI.show();
74     cout << get1 << " " << get2 << endl;
75     return ;
76 }
77  
78 int main(){
79     int T;
80     while(cin >> T) while(T--){
81         int n , h , a , d1 ,d2;
82         cin >> n >> h >> a >> d1 >> d2;
83         AMY WO(n,h,a,d1);
84         AMY DI(n,h,a,d2);
85         //////////////
86         int q; cin >> q;
87         while(q--){
88             int op1; cin >> op1;
89             if(op1 == 1) op_1(WO,DI);
90             else if(op1 == 2) op_2(WO,DI);
91             else if(op1 == 3) op_3(WO,DI);
92         }
93     }
94     return 0;
95 }
原文地址:https://www.cnblogs.com/ticsmtc/p/5350697.html