• Uva725 除法


    Uva725 除法

    题目描述:

    输入正整数n,按从小到大的顺序输出所有形如(abcde/fghij=n)的表达式,其中(a-j)恰好为数字0-9的一个排列(可以有前导0)。

    思路:

    枚举(fghij),然后计算出(abcde),看所有的数字是否重复。这里主要是要注意判断0-9数字分别出现一次时的效率问题。

    代码实现:
    #include <iostream>
    #include <map>
    #include <algorithm>
    #define LL long long
    using namespace std;
    
    int main(){
    	int n;int kase=0;
    	while(cin >> n && n){
    		int ok = 0;
    		
    		if(kase > 0) printf("
    ");
    		++kase;
    		for(int i = 1234; i < 99999; ++i){
    			LL res = (LL)i * n;
    			int ii = i;
    			map<int, int> num;
    			if(res > 98765) continue;  //提前终止条件 
    			int k = 0;
    			for( ; k < 5; ++k){       //分别取出abcde和efghi各位的数字
    				if(!num.count(ii%10)) { num[ii%10] = 1; ii/=10;}
    				else break; //如果某个数字已经出现,那么提前退出。
    				if(!num.count(res%10)) { num[res%10] = 1; res/=10;}
    				else break;
    			}
    		//	cout << "k = " << k << "
    ";
    			if(k == 5){   //只有当k==5时才说明有满足条件的这样一组算式
    				ok = 1;
    				if(i < 10000) printf("%ld / 0%d = %d
    ", (LL)i*n, i, n);
    				else printf("%ld / %d = %d
    ", (LL)i*n, i, n); 
    			}
    		}            //如果遍历完都还没有解,就输出no solution
    		if(!ok) printf("There are no solutions for %d.
    ", n);
    	}
    }
    
    ps:

    这里是用map来记录各位数字,更快的做法是,将(abcde)(efghi)的各位数字分别取出放进一个数组里面,最后扫一遍这个数组,如果某个数字出现次数不等于1,那么算式就不能构成一个排列。

  • 相关阅读:
    AC自己主动机
    curl命令具体解释
    javascript事件和事件处理
    STM32 水晶不摇
    Play Modules Morphia 1.2.9a 之 Aggregation and Group aggregation
    菜板 动态规划
    姿势体系结构的详细解释 -- C
    有情无情胜, 大家是不是谁谁谁的意外
    S性能 Sigmoid Function or Logistic Function
    iOS 中国排序
  • 原文地址:https://www.cnblogs.com/patrolli/p/11370210.html
Copyright © 2020-2023  润新知