这是我的代码,但是一直出现wrong answer,希望大神能帮忙指出程序的错误。
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
char red_name[5][10]={"iceman","lion","wolf","ninja","draagonn"};
char blue_name[5][10]={"lion","dragon","ninja","iceman","wolf"};
char sr_name[5][10]={"dragon","ninja","iceman","lion","wolf"};
int life_r;
int life_b;
class soilder{
public:
char *name; /*指针类型的*/
int life_cos;
int num;
soilder(char *s,int n,int a=0){
name=s;;
life_cos=n;
num=a;
}
char* pr_name(void){
return name; //返回指针地址
}
};
void shuchu(soilder a,int t,char z[]){
cout<<setw(3)<<setfill('0')<<t<<' ';
cout<<z<<' '<<a.name<<' '<<t+1<<" born with strength "<<a.life_cos<<",";
cout<<a.num<<' '<<a.name<<" in "<<z<<" headquarter"<<endl;
}
void jieshu(int t,char z[]){
cout<<setw(3)<<setfill('0')<<t<<' ';
cout<<z<<" headquarter stops making warriors"<<endl;
}
int bj(int j,int life,soilder w[5]){
/*
是为了比较看现在的生命值按照顺序还能制造第几个战士
*/
for(int i=j+1;i<5;i++){
if(life>w[i].life_cos)
return i;
}
for(int i=0;i<=j;i++){
if(life>w[i].life_cos)
return i;
}
return 10; /*代表如果没有任何战士可以制造,就返回一个和索引无关的数*/
}
int main(){
int lun;
cin>>lun;
int lunshu=1;
while(lun!=0){
cin>>life_r;
life_b=life_r;
int n[5];
bool r_stop=false,b_stop=false;
char color[2][5]={"red","blue"};
int time;
for(int i=0;i<5;i++){
cin>>n[i];
}
// 初始化红蓝军的战士名单
soilder red_worrior[5]= {soilder(sr_name[2],n[2]),soilder(sr_name[3],n[3]),soilder(sr_name[4],n[4]),soilder(sr_name[1],n[1]),soilder(sr_name[0],n[0])}; //56743
soilder blue_worrior[5]={soilder(sr_name[3],n[3]),soilder(sr_name[0],n[0]),soilder(sr_name[1],n[1]),soilder(sr_name[2],n[2]),soilder(sr_name[4],n[4])}; //63457
cout<<"Case:"<<lunshu<<endl;
int r=0;
int b=0;
for(time=0;;time++){
//避免索引越界
if(r>4) r=0;
if(b>4) b=0;
//red
if(life_r>=red_worrior[r].life_cos){//和当前的战士生命源比较
red_worrior[r].num++;
life_r-=red_worrior[r].life_cos;
shuchu(red_worrior[r],time,color[0]);
r++;
}
else if(bj(r,life_r,red_worrior)==10 && r_stop==false){ //已经不够制造任何一种战士
jieshu(time,color[0]);
r_stop=true;
}
else if(r_stop==false){ //向后排序发现仍然有可以制造的战士并返回战士的索引值
r=bj(r,life_r,red_worrior);
red_worrior[r].num++;
life_r-=red_worrior[r].life_cos;
shuchu(red_worrior[r],time,color[0]);
r++;
}
/*blue */
if(life_b>=blue_worrior[b].life_cos){
blue_worrior[b].num++;
life_b-=blue_worrior[b].life_cos;
shuchu(blue_worrior[b],time,color[1]);
b++;
}
else if(bj(b,life_b,blue_worrior)==10 && b_stop==false){
jieshu(time,color[1]);
b_stop=true;
}
else if(b_stop==false){
b=bj(b,life_b,blue_worrior);
blue_worrior[b].num++;
life_b-=blue_worrior[b].life_cos;
shuchu(blue_worrior[b],time,color[1]);
b++;
}
/*判断是否结束*/
if(r_stop==true && b_stop==true){
break;
}
}
lun--;
lunshu++;
}
return 0;
}
我编译输出都没有问题啊。还没有学到string用法,所以想请教一下大家。