Đăng nhập để hỏi chi tiết


Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
char name[20];
int strength;
int agility;
int health;
} Fighter;
void initializeFighter(Fighter *fighter, const char *name) {
strcpy(fighter->name, name);
fighter->strength = rand() % 101;
fighter->agility = rand() % 101;
fighter->health = rand() % 101;
}
void printFighter(Fighter fighter) {
printf("Tên: %s, Sức mạnh: %d, Nhanh nhẹn: %d, Máu: %d\n",
fighter.name, fighter.strength, fighter.agility, fighter.health);
}
Fighter battle(Fighter *fighter1, Fighter *fighter2) {
int score1 = fighter1->strength + fighter1->agility + fighter1->health;
int score2 = fighter2->strength + fighter2->agility + fighter2->health;
if (score1 > score2) {
fighter1->strength *= 1.02;
fighter1->agility *= 1.02;
fighter1->health *= 1.02;
fighter2->strength *= 1.01;
fighter2->agility *= 1.01;
fighter2->health *= 1.01;
return *fighter1;
} else {
fighter1->strength *= 1.01;
fighter1->agility *= 1.01;
fighter1->health *= 1.01;
fighter2->strength *= 1.02;
fighter2->agility *= 1.02;
fighter2->health *= 1.02;
return *fighter2;
}
}
int main() {
srand(time(NULL));
Fighter fighters[3];
initializeFighter(&fighters[0], "Đấu thủ 1");
initializeFighter(&fighters[1], "Đấu thủ 2");
initializeFighter(&fighters[2], "Đấu thủ 3");
for (int i = 0; i < 3; i++) {
printf("Trận đấu giữa %s và %s:\n", fighters[i].name, fighters[(i + 1) % 3].name);
Fighter winner = battle(&fighters[i], &fighters[(i + 1) % 3]);
printf("Người thắng: %s\n\n", winner.name);
}
printf("Chỉ số cuối cùng của các đấu thủ:\n");
for (int i = 0; i < 3; i++) {
printFighter(fighters[i]);
}
return 0;
}
Hãy giúp mọi người biết câu trả lời này thế nào?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct{
float power;
float speed;
float hp;
} Fighter;
void reward(Fighter *f, float percent){
f->power += f->power * percent / 100;
f->speed += f->speed * percent / 100;
f->hp += f->hp * percent / 100;
}
void arena(Fighter *a, Fighter *b){
float sa = a->power + a->speed + a->hp;
float sb = b->power + b->speed + b->hp;
if(sa > sb){
reward(a,2);
reward(b,1);
}else{
reward(b,2);
reward(a,1);
}
float bonus = rand()%5;
reward(a,bonus);
reward(b,bonus);
}
int main(){
Fighter A,B,C;
int n;
srand(time(NULL));
scanf("%f%f%f",&A.power,&A.speed,&A.hp);
scanf("%f%f%f",&B.power,&B.speed,&B.hp);
scanf("%f%f%f",&C.power,&C.speed,&C.hp);
scanf("%d",&n);
for(int i=0;i<n;i++){
arena(&A,&B);
arena(&A,&C);
arena(&B,&C);
}
printf("A: %.2f %.2f %.2f\n",A.power,A.speed,A.hp);
printf("B: %.2f %.2f %.2f\n",B.power,B.speed,B.hp);
printf("C: %.2f %.2f %.2f\n",C.power,C.speed,C.hp);
return 0;
}Hãy giúp mọi người biết câu trả lời này thế nào?
Bảng tin