• sicily 1050 Numbers & Letters


    Description

    In the early 80’s, a popular TV show on Dutch television was ‘Cijfers en Letters’ (Numbers and Letters). This game consisted of two game elements, in which the main goal was to outclass your opponent. Letters is a game in which you are given a number of letters with which you should form the longest Dutch word possible. Since Dutch is a very hard language to learn we will postpone implementation of this game element until after the contest.  
    For the second game element ‘Numbers’ 5 different numbers are chosen, together with a target number. The aim is to use some arithmetic on ( some of) the five numbers to form the target number. Each number can be used only once. It might not be possible to form the target number given the input numbers, in that case the largest number smaller than the target number that can be calculated should be given. The only mathematical operations allowed are: +, -, *, /.  All intermediate results should be integers, so division is not always allowed (e.g. (2*2)/4 is OK, but 2*(2/4) is not).  
    Examples:  
    - If the 5 numbers are 1, 2, 3, 7 and 100 and the target number is 573, the target number can be reached as follows: (((100-1)*2)-7)*3. -If the 5 numbers are 3, 26, 78, 12 and 17, and the target number is 30, the target number can be reached as follows: (78*3)-(12*17).  
    - If the 5 numbers are 67, 69, 58, 22, 2, and the target number is 929, the target number cannot be reached, but the largest number smaller than the target number that can be reached is 923 = (22-(67-58))*(69+2).  
    Your assignment is to write a program that calculates the best approximation from below of the target number using arithmetic on the 5 given numbers. Note that if it is not possible to reach the exact number, you should give the largest reachable number below the target number.

    Input

    The first line contains the number of runs, N. The next N lines consist of six numbers separated by a space. The first 5 numbers Mi, 1≤Mi≤100, are the numbers you can use to calculate the target number. The sixth number is the target number T, 0≤T≤1000.

    Output

    The output consists of N rows, each containing the best approximation of the target number using the 5 given numbers.

    Sample Input

    3
    1 2 3 7 100 573
    3 26 78 12 17 30
    67 69 58 22 2 929

    Sample Output

    573
    30 
    923

    分析:

    深搜法或回溯法即可,不断地任取两个数做加减乘除得到新的数,又和剩下的数组成新的数据进行新的搜索,反复如此,观察是否有数字满足题目要求。如果满足要求则输出答案,注意除零的判断和乘法的限定。

    代码:

     1 // Problem#: 1050
     2 // Submission#: 1878130
     3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
     4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
     5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
     6 #include <iostream>
     7 #include <cstring>
     8 using namespace std;
     9 
    10 int q,m,num;
    11 int buffer[6];
    12 bool visit[6];
    13 
    14 void dfs(int k ){
    15     for( int i=1; i<=5; i++ ){
    16         if( !visit[i] && buffer[i]<=num && num-buffer[i]<q){
    17             q = num - buffer[i];
    18             m = buffer[i];
    19         }
    20     }
    21     for( int i=1 ; i<=5 ; i++ ){
    22         if( !visit[i] ){
    23             for( int j=i+1 ; j<=5 ; j++ ){
    24                 if( !visit[j] ){
    25                     int tmp = buffer[i];
    26                     buffer[i] = buffer[i] + buffer[j];
    27                     visit[j] = true;
    28                     dfs(k-1);
    29                     visit[j] = false;
    30                     buffer[i] = tmp;
    31                     buffer[i] = buffer[i] - buffer[j];
    32                     visit[j] = true;
    33                     dfs(k-1);
    34                     visit[j] = false;
    35                     buffer[i] = tmp;
    36                     buffer[i] = buffer[j] - buffer[i];
    37                     visit[j] = true;
    38                     dfs(k-1);
    39                     visit[j] = false;
    40                     buffer[i] = tmp;
    41                     buffer[i] = buffer[i] * buffer[j];
    42                     visit[j] = true;
    43                     if( k!=2 || (k==2 && tmp>=0 && tmp<=100000) ) dfs(k-1);
    44                     visit[j] = false;
    45                     buffer[i] = tmp;
    46                     if( buffer[j]!=0 && buffer[i]%buffer[j]==0 ){
    47                         buffer[i] = buffer[i] / buffer[j];
    48                         visit[j] = true;
    49                         dfs(k-1);
    50                         visit[j] = false;
    51                         buffer[i] = tmp;
    52                     }
    53                     if( buffer[i]!=0 && buffer[j]%buffer[i]==0 ){
    54                         buffer[i] = buffer[j]/buffer[i];
    55                         visit[j] = true;
    56                         dfs(k-1);
    57                         visit[j] = false;
    58                         buffer[i] = tmp;
    59                     }
    60                 }
    61             }
    62         }
    63     }
    64 }
    65 
    66 int main(){
    67     int n;
    68     cin >> n;
    69     while( n-- ){
    70         for (int i=1; i<=5; i++)
    71             cin >> buffer[i];
    72         cin >> num;
    73         memset(visit,false,sizeof(visit));
    74         q = 1000;
    75         dfs(5);
    76         cout << m << endl;
    77     }
    78     return 0;
    79 }                                 
  • 相关阅读:
    Python 2 与 python 3的区别
    语法基础题
    Python运算符_ 2018-07-26
    Python 各种语句:2018-07-27
    解决在Python中使用Win32api报错的问题,No module named win32api
    在CenOS7.5里安装Redis
    下载Redis
    重置CentOS6.5的登录口令
    如何在CentOS里切换操作系统所用的语言,中英文切换
    在Ubuntu里安装Mysql5.7.23
  • 原文地址:https://www.cnblogs.com/ciel/p/2876812.html
Copyright © 2020-2023  润新知