题目大意:
给出两个数,前一个数是X想的数,后一个数是Y猜的数,输出格式是A*B,A前面表示位置正确,值也正确的数量,B前的*表示值正确但是位置的数量
解题思路:
对比+遍历即可
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char str1[5],str2[5];
int t,i,j;
int countp,countv;
scanf("%d",&t);
while(t--)
{
countp=countv=0;
scanf("%s%s",str1,str2);
for(i=0;i<4;i++)
{
if(str1[i]==str2[i])
{
countp++;
}
else
{
for(j=0;j<4;j++)
{
if(str1[i]==str2[j]&&(i!=j))
{
countv++;
}
}
}
}
printf("%dA%dB\n",countp,countv);
}
return 0;
}
本文介绍了一种猜数字游戏的算法实现,通过对比和遍历的方法来判断猜测者所猜数字的位置与数值是否准确。使用C语言编程,详细展示了如何统计位置正确且数值相等的数字数量以及数值正确但位置不对的数字数量。
1074

被折叠的 条评论
为什么被折叠?



