zoukankan      html  css  js  c++  java
  • Number-guessing Game

    Number-guessing Game

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 51   Accepted Submission(s) : 31
    Problem Description

    Larry likes playing the number-guessing game.

    Two players are needed in a game. Suppose they are X and Y, and X presents a number for Y to guess. Firstly, X chooses a number with four different digits, keeping it in mind, and tells Y to start guessing. Every time Y has guessed, X should give out *A*B to show Y how close to the number his answer is. Here the symbol * stands for a number, and the number before A is the number of digits in Y's answer with both correct value and position. The number before B is the number of digits in Y's answer with correct value but incorrect position.

    For example, if X chooses the number 5204, and Y guesses 4902, then X should give out 1A2B, in which 1A corresponds for digit 0 with both correct value and position and 2B corresponds for digit 2 and 4 with correct value but incorrect position. Then Y will go on guessing according to 1A2B that X presents him until he gets the totally correct number 5204 (when X shows him 4A0B).

    Now you are given two numbers, and what you need to do is just testing how close they are.

     
    Input

    The first line of the input is an integer T which indicates the number of test cases. For each test case, input two numbers. Each number contains four different digits.

     
    Output

    For each test case, output *A*B stands for how close the two numbers are.  

     
    Sample Input
    2 5204 4902 0123 3210
     
    Sample Output
    1A2B 0A4B
     
    Source
    PKU
     
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 int main()
     5 {
     6     int T,a[4],b[4],i,sign,j,signs;
     7     scanf("%d",&T);
     8     while(T--)
     9     {
    10         scanf("%d%d",&j,&i);
    11         a[0]=j/1000;
    12         a[1]=(j/100)%10;
    13         a[2]=(j%100)/10;
    14         a[3]=j%10;
    15         b[0]=i/1000;
    16         b[1]=(i/100)%10;
    17         b[2]=(i%100)/10;
    18         b[3]=i%10;
    19         for(i=0,sign=0,signs=0;i<4;i++)
    20         {
    21             if(a[i]==b[i])
    22             {
    23                 sign++;
    24             }
    25             else
    26             {
    27                 for(j=0;j<4;j++)
    28                 if(b[i]==a[j])
    29                 {
    30                     signs++;
    31                 }
    32             }
    33         }
    34         printf("%dA%dB
    ",sign,signs);
    35     }
    36     return 0;
    37 }
    View Code
    转载请备注:
    **************************************
    * 作者: Wurq
    * 博客: https://www.cnblogs.com/Wurq/
    * Gitee: https://gitee.com/wurq
    **************************************
  • 相关阅读:
    C#通过正则表达式统计词频的一个方法
    本地服务器远程连接其它数据库
    拼字符串成为时间,和两个计算时间点的中间值
    删除文件夹里的图片,打印删除日志
    行转列SQL语句
    加载出一个有层次的下拉框
    查询结果列传行
    【Java&Python双管齐下复健002】回文数和反转数
    【Java&Python双管齐下复健001】冒泡排序和质数判断
    【LeetCode记录】初级算法:数组之删除排序数组中的重复项
  • 原文地址:https://www.cnblogs.com/Wurq/p/3750267.html
Copyright © 2011-2022 走看看