uva725 Division

题目描述:

abcde / fghij =N

a,b···j 为0~9任意一个数,且互相不同

任意给一个n(2<=n<=79),输出满足条件的可能

思路1:10!只有不到400w,直接暴力枚举即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

int a[]= {1,0,2,3,4,5,6,7,8,9};
int ansx[4000000];
int ansy[4000000];
int v[4000000];
int main()
{
    int t=0;
    do
    {
        ++t;
        int x=a[0]*10000+a[1]*1000+a[2]*100+a[3]*10+a[4];
        int y=a[5]*10000+a[6]*1000+a[7]*100+a[8]*10+a[9];
        if(x%y==0)
        {
            v[t]=x/y;
            ansx[t]=x;
            ansy[t]=y;
        }
        else
            v[t]=-1;
    }
    while(next_permutation(a,a+10));
    int n;
    int flag=false;
    while(scanf("%d",&n)==1)
    {
        if(n==0)
            break;
        if(flag)///wrong answer
        printf("\n");
        bool f=false;
        for(int i=1; i<=t; i++)
            if(v[i]==n)
            {
                printf("%d / ",ansx[i]);
                if(ansy[i]>9999)
                    printf("%d = ",ansy[i]);
                else
                    printf("0%d = ",ansy[i]);
                printf("%d\n",n);
                f=true;
            }
        if(!f)
            printf("There are no solutions for %d.\n",n);
        flag=true;
    }
    return 0;
}

收获:最后换行符不能多输出,否则wrong answer

思路2:可以只枚举后一位,10!/ 5!,不到10w ,通过判断前面是否符合条件即可

/*
    暴力:对于每一个数都判断,是否数字全都使用过一遍
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <queue>
using namespace std;

const int MAXN  = 1e4 + 10;
const int INF = 0x3f3f3f3f;
int vis[10];

bool ok(int x, int y)
{
    memset (vis, 0, sizeof (vis));
    for (int i=1; i<=5; ++i)
    {
        vis[x%10]++;    vis[y%10]++;
        if (vis[x%10] > 1 || vis[y%10] > 1)    return false;
        x /= 10;    y /= 10;
    }

    return true;
}

int main(void)        //UVA 725 Division
{
    //freopen ("UVA_725.in", "r", stdin);

    int n, cnt = 0;
    while (scanf ("%d", &n) == 1)
    {
        if (n == 0)    break;
        if (cnt++)    puts ("");

        int one = 0;
        for (int i=1234; i<=100000/n; ++i)
        {
            if (i * n > 98765)    break;
            if (ok (i, i*n) == true)
            {
                printf ("%05d / %05d = %d\n", n*i, i, n);    one++;
            }
        }

        if (!one)    printf ("There are no solutions for %d.\n", n);
    }

    return 0;
}

/*
There are no solutions for 61.
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-18 03:57:04

uva725 Division的相关文章

UVA725 Division【枚举】

  Division Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij = N wher

UVA725 UVALive5362 Division

Regionals 1990 >> North America - East Central NA 这可以说是最快枚举程序,比网上现有的暴力枚举程序要快. 问题链接:UVA725 UVALive5362 Division.基础练习题,用C语言编写程序. 题意简述:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果.如果没有找到则输出"There are no solutions for N."

UVA-725除法-Division

分析:  枚举0-9的所有排列?没这个必要,只需要枚举fghij就可以计算出abcde(=fghij * n),然后判断是否所有的数字都不相同即可.不仅程序简单,而且枚举量也从10!=3628800降低至不到1万,而且当abcde的位数不等于5的时候,就可以终止枚举了(记住n是大于等于2的哟!) AC代码如下:用时为1573MS. #include<cstdio> #include<cstring> #include<iostream> #include<cmat

Division UVa725

题意:输入一个n(2<=n<=79) 找出是否从存在abcde/fghij=n的表达式 直接枚举,对于每一个fghij,判断abcde,所有数字不相等就可以(每个数字都出现),但要注意枚举的范围,还有格式,格式真的头疼. 代码如下 #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define INF 0x7fffffffffffffff typedef long long ll; const double PI=3.14159265

light oj 1214 - Large Division

1214 - Large Division   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Given two integers, a and b, you should check whether a is divisible by b or not. We know that an integer a is divisible by an integer b if and only if

fzuoj1607Greedy division

题目链接: 点我点我 题目:  Problem 1607 Greedy division Accept: 436    Submit: 1590 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description Oaiei has inherited a large sum of wealth recently; this treasure has n pieces of golden coins. Unfortunate

HDU 3480 Division(斜率优化+二维DP)

Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Total Submission(s): 3984    Accepted Submission(s): 1527 Problem Description Little D is really interested in the theorem of sets recently. There’s a pro

BZOJ 1385: [Baltic2000]Division expression

题目 1385: [Baltic2000]Division expression Time Limit: 5 Sec  Memory Limit: 64 MB Description 除法表达式有如下的形式: X1/X2/X3.../Xk 其中Xi是正整数且Xi<=1000000000(1<=i<=k,K<=10000) 除法表达式应当按照从左到右的顺序求,例如表达式1/2/1/2的值为1/4.但可以在表达式中国入括号来改变计算顺序,例如(1/2)/(1/2)的值为1.现给出一个除

枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)

1 //Uva725 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cstdio> 6 using namespace std; 7 8 void evalu(int n) 9 { 10 const int maxn = 1024 + 10; 11 char num[10]; //将数字用字符保存 12 int flag[10]; //判断每个数,是否重