回 帖 发 新 帖 刷新版面

主题:第58次编程比赛题目——简单猜数字

题目描述:
猜数字游戏大家玩过没?经典的规则是给出一个四位数,然后你去猜。
如那个数是1357,你猜1234的话,就给出1A1B,这是什么意思呢?
nA表示有n个数的位置猜对了,nB表示有n个数猜对了,但位置不对,
如果你再猜2351当然给出2A1B了,如果你猜2468,那就是0A0B了。
现在,把四位数扩展到n位,由数字扩展到字符,输入两个字符串,你
判断出猜对位置的和猜对了但位置不对的。如abcdefg和aceg123,结
果就是1A3B

输入:
多组测试数据,每组占一行,每行有两个字符串,串长小于10000,用空格分隔开

输出:
输出这两个字符串比较的结果

样例输入:
1357 1234
abcdefg aceg123
AaAa AAaa
121212 2121212211
Qq qqGame

样例输出:
1A1B
1A3B
2A2B
0A6B
1A0B

难度:very easy

要求:
只要把你完整的代码发出来即可,不要输出多余题目没有的内容

其它:
代码基本框架(仅仅作为参考,你可以不使用这个框架自己写):
//C语言
#include <stdio.h>
#define  MAX_CHAR_LEN   10001
int main()
{
    char str1[MAX_CHAR_LEN],str2[MAX_CHAR_LEN];
    while(scanf("%s %s",&str1, &str2) != EOF)
    {
        //这里写你的计算代码
    }
    return 0;
}

//C++语言
#include <iostream>
using namespace std;
const int MAX_CHAR_LEN = 10001;
int main()
{
    char str1[MAX_CHAR_LEN],str2[MAX_CHAR_LEN];
    while(cin>>str1>>str2)
    {
        //这里写你的计算代码
    }
    return 0;
}

开始时间7月28日19:00,结束时间7月30日22:00
对题目有疑问的,请到[url]http://www.programfan.com/club/post-244340.html[/url]提出,谢谢

回复列表 (共24个回复)

沙发

发一个自己的code:
[code=c]#include <stdio.h>
#define MAX_CHAR_LEN 10001
int main()
{
    char ch1[MAX_CHAR_LEN],ch2[MAX_CHAR_LEN];
    while(scanf("%s %s",&ch1, &ch2) != EOF)
    {
        int nct[256]={0};
        int n;
        int na=0, nb=0;
        char *cp=ch1;
        for(;*cp;cp++)
        {
            nct[*cp]++;
        }
        cp=ch2;
        for(;*cp;cp++)
        {
            if(nct[*cp])
            {
                nb++;
                nct[*cp]--;
            }
        }
        for(n=0;ch1[n]&&ch2[n];n++)
        {
            if(ch1[n]==ch2[n])
            {
                na++;
            }
        }
        printf("%dA%dB\n", na, nb-na);
    }
    return 0;
}
[/code]

板凳

飞燕, 这题不是做过吗?

3 楼

今天终于下班了...程序员的工作真的很累..做VC程序员更累...但乐在其中...
   技术是不用拿来卖弄的...不过像你们初学者也应该做这种烂题目....一个好的程序员也是从这种烂题目学习而来的..

4 楼

不知:
12121212 21
是0A2B呢还是0A8B,我的算法就是0A8B啦.
不管那么多啦
#include <stdio.h>
#define  MAX_CHAR_LEN   10001
int main()
{
    char str1[MAX_CHAR_LEN],str2[MAX_CHAR_LEN];
    while(scanf("%s %s",&str1, &str2) != EOF)
    {
        //这里写你的计算代码
        int a=0,b=0,i=0,j=0;
        while(*(str1+i)!='\0') 
        {
            j=0;
            while(*(str2+j)!='\0')
            {
                if(*(str1+i)==*(str2+i))
                {a++;break;}
                if(i!=j&&*(str1+i)==*(str2+j)) 
                {b++;break;}
                j++;
            }
                i++;
        }
        printf("%dA%dB\n",a,b);
    }
    return 0;
}

5 楼

/*
算法用的是归并排序.
环境:VC++ 6.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 10001

int cmp(const char *a, const char *b)
{
    return *b - *a;
}

int main(void)
{
    char    str1[SIZE];
    char    str2[SIZE];
    int    len1;
    int    len2;
    int    i;
    int    j;
    int    A;
    int    B;

    while (scanf("%s%s", str1, str2) != EOF)
    {
        len1 = strlen(str1);
        len2 = strlen(str2);
        A = B = 0;
        for (i = 0; str1[i] && str2[i]; i++)
        {
            if (str1[i] == str2[i])
            {
                A++;
                str1[i] = str2[i] = 0;
            }
        }
        qsort(str1, len1, sizeof(char), cmp);
        qsort(str2, len2, sizeof(char), cmp);
        for (i = j = 0; str1[i] && str2[j];)
        {
            if (str1[i] > str2[j])
                i++;
            else if (str1[i] < str2[j])
                j++;
            else
            {
                i++;
                j++;
                B++;
            }
        }
        printf("%dA%dB\n", A, B);
    }

    return 0;
}

6 楼


#include <iostream>
#include <cstring>

using namespace    std;
const int MAX_CHAR_LEN = 10001;

void solve(string str1, string str2)
{
    int a=0,b=0,marks[300];
    memset(marks,0,sizeof(marks));
    for(int i=0,len=str1.size(); i<len; i++){
        if(str1[i]==str2[i]){
            a++;
        }else{
            if(marks[str1[i]]<0){
                b++;
            }
            marks[str1[i]]++;
            if(marks[str2[i]]>0){
                b++;
            }
            marks[str2[i]]--;
        }
    }
    cout<<a<<'A'<<b<<'B'<<endl;
}

int    main()
{
    char str1[MAX_CHAR_LEN],str2[MAX_CHAR_LEN];
    while(cin>>str1>>str2)
    {
        //这里写你的计算代码
        solve(str1,str2);
    }
    return 0;
}

7 楼

好久没参加了,来捧个人场
串太长可能会吃不消~~~~~

#include <stdio.h>
#include <string.h>
#define  MAX_CHAR_LEN   10001
int main()
{
    char str1[MAX_CHAR_LEN],str2[MAX_CHAR_LEN];
    int strpos1,strpos2; 
    int lenstr1,lenstr2;
    int midlenstr;
    int A_num,B_num;
    gets(str1);
    gets(str2);
    /*begin competetion programme*/
    A_num=0;
    B_num=0;
    lenstr1=strlen(str1);
    lenstr2=strlen(str2);
    midlenstr=(lenstr1>lenstr2?lenstr2:lenstr1);
    for(strpos1=0;strpos1<midlenstr;strpos1++)
        if(str1[strpos1]==str2[strpos1]) 
        {
            A_num++;
            str1[strpos1]='\0';
            str2[strpos1]='\0';
        }
    for(strpos2=0;strpos2<lenstr2;strpos2++)
    {
        if(str2[strpos2]=='\0') continue;
        for(strpos1=0;strpos1<lenstr1;strpos1++)
            if(str2[strpos2]==str1[strpos1])
            {
                B_num++;
                str1[strpos1]='\0';
                break;
            }
    }
    /*end competetion programme*/ 
    printf("%d A,%d B\n",A_num,B_num);
    return 0;
}

8 楼

[code]
#include <iostream>
#include <list>
#include <algorithm>
#include <cstring>

using namespace std;
const int MAX_CHAR_LEN = 10001;

int main()
{
    char str1[MAX_CHAR_LEN],str2[MAX_CHAR_LEN];
    while(cin>>str1>>str2)
    {
        int Sz = int(strlen(str1));
        list<char> Str1(str1, str1 + Sz);        
        list<char> Str2(str2, str2 + Sz);
        list<char>::iterator Iter1 = Str1.begin();
        list<char>::iterator Iter2 = Str2.begin();
        int A = 0;
        while(Iter1 != Str1.end() && Iter2 != Str2.end())
        {
            if(*Iter1 == *Iter2)
            {
                ++ A;
                Iter1 = Str1.erase(Iter1);
                Iter2 = Str2.erase(Iter2);
                continue;
            }
            ++ Iter1;
            ++ Iter2;
        }
        Iter1 = Str1.begin();
        int B = 0;
        while(Iter1 != Str1.end())
        {
            Iter2 = find(Str2.begin(), Str2.end(), *Iter1);
            if(Iter2 != Str2.end())
            {
                ++ B;
                Str2.erase(Iter2);
            }
            ++ Iter1;
        }
        cout << A << "A" << B << "B" << endl;
    }
    return 0;
}
[/code]

9 楼

想了半天,结果用最笨的方法做的.......
等着看高手的答案

#include <stdio.h>
#include <string.h>
#define  MAX_CHAR_LEN   10001
int main()
{
    int m,n,i,j,min,numA,numB;
    char str1[MAX_CHAR_LEN],str2[MAX_CHAR_LEN];
    while(scanf("%s %s",&str1, &str2) != EOF)
    {
        m=strlen(str1);
        n=strlen(str2);
        if(m<=n)    { min=m; }
        else        { min=n; }

        numA=0;
        numB=0;
        for(i=0;i<min;i++)
        {
            if(str1[i]==str2[i])    
            {
                numA++;
                for(j=i;j<m;j++)
                    str1[j]=str1[j+1];
                str1[j]='\0';
                m--;
                for(j=i;j<n;j++)
                    str2[j]=str2[j+1];
                str2[j]='\0';
                n--;
                i--;
                min--;
            }
        }

        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
                if(str1[i]==str2[j])    
                {
                    numB++;
                    break;
                }
        }

        printf("%dA%d\n",numA,numB);

    }
    return 0;
}

10 楼

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
void main(){    
    char rnd[10];
    char cusNum[5];
    char templ[10]={'0','1','2','3','4','5','6','7','8','9'};
    int status[4]={0};//有这个数字但位置不对则1,有数字位置也对则2;
    int i,j;
    int num;
    int posRight=0,numRight=0;
    srand((unsigned)time(NULL));

    for(i=0;i<4;++i)
        rnd[i]=templ[(int)(1+rand())%9];
    rnd[i]='\0';
    puts(rnd);
    printf("请猜数字(4位数):");
    gets(cusNum);

    for(i=0;i<4;++i){
        for(j=0;j<4;++j){
            if(cusNum[i]==rnd[j]&&i!=j)
                status[i]=1;        
            if(cusNum[i]==rnd[j]&&i==j)
                status[i]=2;
        }
    }
    
    for(i=0;i<4;++i){
        if(status[i]==1)
            numRight++;
        else if(status[i]==2)
            posRight++;
    }
    if(posRight==4)
        printf("你猜对了!\n");
    else
        printf("%dA%dB",posRight,numRight);


}

我来回复

您尚未登录,请登录后再回复。点此登录或注册