回 帖 发 新 帖 刷新版面

主题:第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个回复)

21 楼

#include "stdafx.h"

#include<iostream>
#include <time.h>
#include <stdio.h>
using namespace std;
int main()
{
 char str1[1001],str2[1001];
 while(cin>>str1>>str2){
     int a=0,b=0,c=0;
     int i=0;
     while(1){
         
         if(str1[i]=='\0'||str2[i]=='\0')break;
         if(str1[i]==str2[i]) {
             str1[i]='@';
             str2[i]='@';
             a++;
         }
         i++;
     }
     
     int q,p;
     q=0;
     while(1){
         if(str1[q]=='\0') break;
         p=0;
         while(1){
             if(p==q){
                 p++;continue;
             }
             if(str2[p]=='\0')break;
             if(str1[q]==str2[p]) {
                 if(str1[q]=='@') ;
                 else b++;
             }
             p++;
         }
         q++;
     }
    
     cout<<a<<"A"<<b<<"B";
     
     
 }
}

22 楼

#include "stdafx.h"

#include<iostream>
#include <time.h>
#include <stdio.h>
using namespace std;
int main()
{
 char str1[1001],str2[1001];
 while(cin>>str1>>str2){
     int a=0,b=0,c=0;
     int i=0;
     while(1){
         
         if(str1[i]=='\0'||str2[i]=='\0')break;
         if(str1[i]==str2[i]) {
             str1[i]='@';
             str2[i]='@';
             a++;
         }
         i++;
     }
     
     int q,p;
     q=0;
     while(1){
         if(str1[q]=='\0') break;
         p=0;
         while(1){
             if(p==q){
                 p++;continue;
             }
             if(str2[p]=='\0')break;
             if(str1[q]==str2[p]) {
                 if(str1[q]=='@') ;
                 else b++;
             }
             p++;
         }
         q++;
     }
    
     cout<<a<<"A"<<b<<"B";
     
     
 }
}

23 楼

#include <iostream>
#include <string>
using namespace std;
const int MAX_CHAR_LEN = 10001;
int main()
{
    char str1[MAX_CHAR_LEN],str2[MAX_CHAR_LEN];
    int num_A = 0, num_B = 0 ;
    while(cin>>str1>>str2)
    {
        for( int i=0 ; i < strlen(str1) ; i++)
        {
            for(int j=0 ; j < strlen(str2) ; j++)
            {
                if(str2[j] == str1[i])
                {
                    if( i == j )
                        num_A++;
                    else
                        num_B++;
                }

            }
        }
            cout << num_A << "A" << num_B << "B\n" ;
        
    }
    return 0;
}



这个程序没有考虑到以下情况:
1.如果字符串中有相同的字符,则会重复计算;
2.程序的输入输出不符合要求。

以上两点实在想不通该怎么解决,请各位高手赐教。

24 楼

第一次参加比赛 多多指教
#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)
    {
    int A=0;int B=0;
    for(int k=0;(str1[k]!='\0')||(str2[k]!='\0');++k)
    if(str1[k]==str2[k])
    ++A;
    
    for(int i=0;i<=strlen(str1);++i)
    for(int j=0;j<=strlen(str2);++j)
    {
    if(str1[i]==str2[j])
    {++B;
    break;}
    }
    B=B-A-1;
    
    cout<<A<<'A'<<B<<'B';
    }
    return 0;
}

我来回复

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