一道c语言的练习题

2025-01-01 19:24:52
推荐回答(2个)
回答1:

#include "stdio.h"

int Judge(const char *a,const char *b){

int s,n;

for(s=n=0;n<10;s+=a[n]==b[n++]);

return s;

}

int main(int argc,char *argv[]){

const char std[]="ABCDBBCBBC";

char ans[10],i,t;

printf("Please enter your answer(orderly sequence of ABCD)...\n");

while(1){

if(scanf("%10s",ans)==1)

for(i=0;i<10;i++)

if(ans[i]!='A' && ans[i]!='B' && ans[i]!='C' && ans[i]!='D')

break;

if(i>=10)

break;

printf("Input error, redo:\nPlease enter your answer(orderly sequence of ABCD)...\n");

}

printf("\nCorrect answer:\n");

for(t=Judge(std,ans),i=0;i<10;i++)

if(std[i]==ans[i])

printf("%2d",i+1);

printf("\nTOTAL: %d\n",10*t);

return 0;

}

代码格式和运行样例图片:

回答2:

public int Judge(char *input)
{
int i, sum;
char ans[11] = "ABCDBBCBBC";
for (i = 0, sum = 0, i < 10 && input[i] != '\0', i++)
{
if (input[i] == ans[i])
sum++;
}
return sum;
}