程序代码:
#include <stdio.h>
#include <string.h>
#define MIN(a, b) (a<b) ? a : b
#define MAX(a, b) (a>b) ? a : b
char getAnswer(char str[4][100]);
int main(void) {
int i;
//以下为检验数据
char strA[4][100] = {"A. 1", "B. 112345678901", "C. 1123456789012", "D. 11234567890123"};
char strB[4][100] = {"A. 1", "B. 12", "C. 112345678901", "D. 1123456789012"};
char strC[4][100] = {"A. 1", "B. 12", "C. 123", "D. 1234"};
char strD[4][100] = {"A. 1", "B. 12", "C. 123", "D. 1231234567890"};
printf("答案是: %c\n", getAnswer(strA));
printf("答案是: %c\n", getAnswer(strB));
printf("答案是: %c\n", getAnswer(strC));
printf("答案是: %c\n", getAnswer(strD));
return 0;
}
char getAnswer(char str[4][100]) {
int i, p, q, s = 10;
char answer, maxIdx, minIdx;
struct ABCD {
char idx;
int len;
int flag;
};
struct ABCD abcd[4];
for(i = 0; i < 4; i++) {
abcd[i].idx = 'A' + i;
abcd[i].len = strlen(str[i]);
}
p = MIN((MIN(strlen(str[0]), strlen(str[1]))) , (MIN(strlen(str[2]), strlen(str[3]))));
q = MAX((MAX(strlen(str[0]), strlen(str[1]))) , (MAX(strlen(str[2]), strlen(str[3]))));
for(i = 0; i < 4; i++) {
if(abcd[i].len == p) minIdx = abcd[i].idx;
}
for(i = 0; i < 4; i++) {
if(abcd[i].len == q) maxIdx = abcd[i].idx;
}
for(i = 0; i < 4; i++) {
if(abcd[i].len <= p + 10) abcd[i].flag = 0;
else abcd[i].flag = 1;
s += abcd[i].flag;
}
switch(s) {
case 10 :
answer = 'C';
break;
case 11 :
answer = maxIdx;
break;
case 12 :
answer = 'B';
break;
case 13 :
answer = minIdx;
break;
default:
printf("error\n");
}
return (answer);
}