来颗方糖 2020-03-21 17:41
浏览 279

coursera c++程序设计 魔兽世界之一:备战

这是我的代码,但是一直出现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用法,所以想请教一下大家。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?
    • ¥100 求三轴之间相互配合画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题