动态规划?是不是瓶子的颜色数目在减少?
问题一:字符 数组拷贝注意初始化
问题二:注意CBG的出现顺序,
问题三:注意最后输出字母顺序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* bin struct*/
struct bin{
int packs[3];
};
/* compare two bins for i,j colors:res */
int comp(struct bin* a, struct bin* b, int i, int j, char* res){
int t1 = a->packs[i] + b->packs[j];
int t2 = a->packs[j] + b->packs[i];
char ic = (char)(i+'0');
char jc = (char)(j+'0');
if(t1 < t2){
strncat(res, &jc, 1);
strncat(res, &ic, 1);
return t1;
}
strncat(res, &ic, 1);
strncat(res, &jc, 1);
return t2;
}
/* show */
void show(int j, char* k[]){
char* out = k[j];
int i;
for(i=0; i < 3;i++){
if(out[i] == '0')
printf("B");
else if(out[i] == '1')
printf("C");
else if(out[i]=='2')
printf("G");
}
}
int main(int argc, char* argv[]){
int kres[3] = {-1};
char* out[3];
int i;
struct bin b1, b2, b3;
while(scanf("%d %d %d %d %d %d %d %d %d",
&(b1.packs[0]),&(b1.packs[2]),&(b1.packs[1]),
&(b2.packs[0]),&(b2.packs[2]),&(b2.packs[1]),
&(b3.packs[0]),&(b3.packs[2]),&(b3.packs[1])) != EOF){
for(i=0; i<3;i++){
char res[4] = {'\0'};
int oth = b2.packs[i] + b3.packs[i];
int foo = b1.packs[(i+1)%3] + b1.packs[(i+2)%3];
res[0] = (char)(i+'0');
int sm = (i+1)%3;
int bg = (i+2)%3;
int sum = sm+bg;
sm = (sm<bg)?sm:bg;
bg = sum-sm;
int kes = comp(&b2, &b3, sm, bg, res);
kres[i] = oth + foo + kes;
out[i] = (char*)malloc(4);
strncpy(out[i], res, 4);
}
int small= 0;
for(i=0; i < 3; i++){
if(kres[i] < kres[small])
small = i;
}
show(small, out);
printf(" %d\n", kres[small]);
free(out[0]);
free(out[1]);
free(out[2]);
}
return 0;
}