uva 725 Division

来源:互联网 发布:mac桌面上的文件不见了 编辑:程序博客网 时间:2024/05/22 06:43

此题很简单,就是暴力枚举,当然也需要有策略,只需要遍历所有的分子就可以了,用set存起来,就行了


////  main.cpp//  uva 725 - Division////  Created by XD on 15/7/26.//  Copyright (c) 2015年 XD. All rights reserved.//#include <iostream>#include <string>#include <queue>#include <stack>#include <stdio.h>#include <stdlib.h>#include <math.h>#include<vector>#include <string.h>#include <string>#include <algorithm>#include <set>#include <map>#include <cstdio>using namespace std ;set<int > g[80]  ;bool iscorrect(int t[],int j){    int flag[10]  ;    memset(flag, 0, sizeof(flag)) ;    for (int i = 0; i < 5; i++) {        flag[t[i]]= 1 ;    }    while (j !=0 ) {        int temp = j %10 ;        j =j /10 ;        if (flag[temp] ==   1) {            return false ;        }        else{            flag[temp] = 1 ;        }    }    for (int i = 0; i < 10; i++) {        if (flag[i]==0) {            return false ;        }    }    return  true ;}int getT(int t[]){    int  weight= 1 ;    int result = 0 ;    for (int i = 0; i < 5; i++) {        result = result + t[i] * weight ;        weight*=10 ;    }    return result ;}int main() {    for (int n = 2; n < 80; n++) {        g[n].clear() ;        int t[5];        for (int i = 0 ; i < 10; i++) {t[0]=i ;            for (int j = i+1 ; j < 10; j++) { t[1]= j ;                for (int  k = j + 1; k < 10; k++) {t[2]= k ;                    for (int l = k + 1; l < 10; l++) {t[3] = l  ;                        for (int m = l + 1; m < 10; m++) {                              t[4]=m ;                            do{                                int T = getT(t) ;                                if (iscorrect(t ,T * n )) {                                    g[n].insert(T) ;                                }                            }while (next_permutation(t, t+5)) ;                        }                    }                }            }        }    }    int n ;    int pre = 0 ;    while (scanf("%d" ,&n)==1 && n!=0) {        if (pre!= 0) {            printf("\n") ;        }        pre = n ;        if (g[n].size()== 0) {            printf("There are no solutions for %d.\n",n) ;        }        for (set<int >::iterator it = g[n].begin(); it!=g[n].end(); it++ ) {            printf("%d / %05d = %d\n" ,*it * n , *it , n ) ;        }            }        return 0;}


0 0
原创粉丝点击