本文主要是介绍【UVALive】6163 Myth Busters 类24点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门:【UVALive】6163 Myth Busters
题目分析:
可以使用括号,还有加减乘除,问能否用四个0~9的数凑出10。。。我了个去。。渣渣不会写拿来当模拟写了,写的人都要吐了。。。。
先处理出所有两个数和起来的情况,然后用两个数和起来的情况再加上一个数处理出所有三个数和起来的情况,最后用两个两个数或者一个三个数加一个数的继续推出四个数的,最后按照不降序保存。
这个能一边AC真是奇葩了。。。。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define CLR( a , x ) memset ( a , x , sizeof a )const int MAXN = 1005 ;int two[10][10][10] ;
int three[10][10][10][2000] ;
int four[10][10][10][10] ;
int n ;
int num[4] ;void solve () {int cnt = 0 ;REP ( i , 0 , n ) {REP ( j , 0 , 4 )scanf ( "%1d" , &num[j] ) ;sort ( num , num + 4 ) ;if ( four[num[0]][num[1]][num[2]][num[3]] )++ cnt ;}if ( cnt == n )printf ( "TRUE\n" ) ;elseprintf ( "BUSTED\n" ) ;
}int unique ( int a[] , int n ) {int cnt = 1 ;sort ( a + 1 , a + n + 1 ) ;FOR ( i , 2 , n )if ( a[i] != a[cnt] )a[++ cnt] = a[i] ;return cnt ;
} void fun () {CLR ( two , 0 ) ;CLR ( three , 0 ) ;REP ( i , 0 , 10 )REP ( j , 0 , 10 ) {int& cnt = two[i][j][0] ;two[i][j][++ cnt] = i + j ;two[i][j][++ cnt] = i * j ;two[i][j][++ cnt] = i - j ;two[i][j][++ cnt] = j - i ;if ( i )two[i][j][++ cnt] = j / i ;if( j )two[i][j][++ cnt] = i / j ;}REP ( i , 0 , 10 )REP ( j , 0 , 10 )two[i][j][0] = unique ( two[i][j] , two[i][j][0] ) ;int ans = 0 ;REP ( i , 0 , 10 )REP ( j , 0 , 10 )FOR ( k , 1 , two[i][j][0] )REP ( l , 0 , 10 ) {int& cnt = three[i][j][l][0] ;three[i][j][l][++ cnt] = two[i][j][k] + l ;three[i][j][l][++ cnt] = two[i][j][k] * l ;three[i][j][l][++ cnt] = two[i][j][k] - l ;three[i][j][l][++ cnt] = l - two[i][j][k] ;if ( l )three[i][j][l][++ cnt] = two[i][j][k] / l ;if ( two[i][j][k] )three[i][j][l][++ cnt] = l / two[i][j][k] ;ans = max ( ans , cnt ) ;//printf ( "%d\n" , ans ) ;}REP ( i , 0 , 10 )REP ( j , 0 , 10 )REP ( k , 0 , 10 )three[i][j][k][0] = unique ( three[i][j][k] , three[i][j][k][0] ) ;REP ( i , 0 , 10 )REP ( j , i , 10 )REP ( k , j , 10 ) {int cnt = three[i][j][k][0] ;int num = 0 ;FOR ( l , 1 , three[i][k][j][0] )three[i][j][k][++ cnt] = three[i][k][j][l] ;FOR ( l , 1 , three[j][i][k][0] )three[i][j][k][++ cnt] = three[j][i][k][l] ;FOR ( l , 1 , three[j][k][i][0] )three[i][j][k][++ cnt] = three[j][k][i][l] ;FOR ( l , 1 , three[k][i][j][0] )three[i][j][k][++ cnt] = three[k][i][j][l] ;FOR ( l , 1 , three[k][j][i][0] )three[i][j][k][++ cnt] = three[k][j][i][l] ;three[i][j][k][0] = cnt ;}REP ( i , 0 , 10 )REP ( j , i , 10 )REP ( k , j , 10 )three[i][j][k][0] = unique ( three[i][j][k] , three[i][j][k][0] ) ;
}void go () {CLR ( four , 0 ) ;REP ( i , 0 , 10 )REP ( j , i , 10 )REP ( k , j , 10 )FOR ( x , 1 , three[i][j][k][0] )REP ( l , 0 , 10 ) {int a = three[i][j][k][x] , b = l ;int tmp[4] = { i , j , k , l } ;sort ( tmp , tmp + 4 ) ;if ( a + b == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;else if ( a - b == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;else if ( a * b == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;else if ( b - a == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;else if ( a && b / a == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;else if ( b && a / b == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;}REP ( i , 0 , 10 )REP ( j , i , 10 )REP ( k , 0 , 10 )REP ( l , k , 10 )FOR ( x , 1 , two[i][j][0] )FOR ( y , 1 , two[k][l][0] ) {int a = two[i][j][x] , b = two[k][l][y] ;int tmp[4] = { i , j , k , l } ;sort ( tmp , tmp + 4 ) ;if ( a + b == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;else if ( a - b == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;else if ( a * b == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;else if ( b - a == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;else if ( a && b / a == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;else if ( b && a / b == 10 )four[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 1 ;}
}int main () {fun () ;go () ;while ( ~scanf ( "%d" , &n ) && n )solve () ;return 0 ;
}
这篇关于【UVALive】6163 Myth Busters 类24点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!