Codevs 1229 Number Games

Source: Internet
Author: User

1229 Number Games

http://codevs.cn/problem/1229/

Title Description DescriptionLele was bored in class recently, so he invented a number game to pass the time. The game is like this, first of all, he took out a few pieces of paper, respectively, 0 to 9 of any number (can be repeated to write a number), then he asked the classmate casually write two numbers x and K. The thing Lele to do is to re-spell the cards, make the number T, and T + X is the positive integer multiple of K.   Sometimes, when a lot of paper, Lele often can't spell in a class, but he wants to know the answer, so, he would like to ask you to help write a program to calculate the answer. Enter a description Input Description
    1. The first line contains two integers N and M (0<n<9,0<m<2000), each representing the number of pieces of paper and the number of queries.
    2. The second line contains n integers representing the numbers written on the paper, each of which may take 0~9.
    3. Next there are M-line queries, each asking for two integers x and K (0<=x<10^9,0<k<100).
Output description Output Description
    1. For each query, if you can use these pieces of paper to spell out the answer to the T, the output of the result T. If there are multiple results, the minimum t that meets the requirements is output.
    2. If it cannot be spelled out, the output is "None".
Sample input Sample Input

4 3

1 2 3 4

5 7

33 6

12 8

Sample output Sample Output

1234

None

1324

Easy to judge can first let the X-mode k, and then burst the whole arrangement, judging each, the data comparison of water, over the

Because the permutation is placed in each element of the array, the final need is the entire number,

It can be ① to convert it to an integer using a recursive method:

int work (i)

{if (!i) return 0;

return I+work (i) *10;

}

②, can also store the time with characters to store numbers, and finally with SSCANF (s, "%d", &n), the string s to N, note s to start with subscript 0

My code uses the second method:

#include <cstdio>#include<iostream>#include<algorithm>using namespacestd;inta[Ten];Chars[Ten];intn,m,x,k,d;BOOLv[Ten],ok;CharWorkintp) {    Switch(A[p]) { Case 1:return '1'; Break;  Case 2:return '2'; Break;  Case 3:return '3'; Break;  Case 4:return '4'; Break;  Case 5:return '5'; Break;  Case 6:return '6'; Break;  Case 7:return '7'; Break;  Case 8:return '8'; Break;  Case 9:return '9'; Break;  Case 0:return '0'; Break; }}BOOLjudge () {sscanf (s),"%d",&d); if((d+x)%k==0)return true; return false;}voidDfsinth) {    if(OK)return; if(h>N) {OK=judge (); return; }     for(intI=1; i<=n;i++)    {        if(!V[i]) {S[h-1]=work (i);//The numbers are converted to characters because the S table below is going to start at 0, so h-1. Note You cannot use S[h-1]=char (i) to get the ASCLL code for Iv[i]=true; DFS (H+1); V[i]=false; }    }}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Sort (a+1, a+n+1);  for(intI=1; i<=m;i++) {scanf ("%d%d",&x,&k); OK=false; X%=K; DFS (1); if(OK) printf ("%d\n", D); Elseprintf"none\n"); }}

Codevs 1229 Number Games

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.