UVa 725 - Division(枚举简单题)

今日无事,写篇日记。这是我写的第一道uva题(uva是算法竞赛入门经典里使用例题的题目来源),正值暑假,前几天看了书上中文题目和解析,玩了几日san11,又重拾起acm,今天晚上写了一下还是花了点时间。

题目:给你一个数字n,用0~9,10个数字组成两个五位数,使得他们的商为n,按顺序输出所有结果。 

1.这里的除法不是c中的除(/)而是整除,一开始以为是整除,但那样79546 / 01283 = 62是一组解,79546+1 / 01283 = 62也是一组解了。

2.复杂度:枚举分子然后分子乘该数,而不是每位数都枚举,复杂度从10!变为10000-1234(本来我想每位枚举的即10!/5!实现起来不会!)

3.判断一起放在函数里

for(int i=1234;i<100000;i++){
if(judge(i,n*i)){//这一句本来有条件判断n*i<10000就不执行,但这样没有条理,应该放到judge函数里
flag=1;
printf("%05d / %05d = %d\n",n*i,i,n);
}
}

4.题目有输出有一个坑点,udbug的hint里看到

Print a blank line between test cases - not after each test case. (So, there should not be a blank line printed after the last test case.) Printing a blank line after each test case will result in a verdict of wrong answer on UVa Online Judge.

最后不能有空行

5.使用udebug 和 vitual judge,这个学期困于找不到bug又没有错误数据,本来想做一个app通过比较输出来找错误的数据,然而水平有限又在学大物,现在发现了已有的udebug(虽然只提供几个OJ)。hust的vitual judge可以解决只有FQ才能上的uva

6.文件输出 freopen("test.out","w",stdout); 输出数据在test.out里,学习了

#include<stdio.h>
int judge(int a,int b){
    if(b>=100000||b<10000)
    return false;
    int used[10];
    for(int i=0;i<10;i++){
        used[i]=0;
    }
    if(a<10000)
    used[0]=1;
    while(a){
        used[a%10]=1;
        a/=10;
    }
    while(b){
        used[b%10]=1;
        b/=10;
    }
    for(int i=0;i<10;i++){
        if(used[i]==0)
        return false;
    }
    return true;
}

int main(){
    //freopen("test.out","w",stdout);
    int n,flagl=0,flag;
    while(scanf("%d",&n)&&n!=0){
        if(flagl++)
        printf("\n");
        
        flag=0;
        for(int i=1234;i<100000;i++){
            if(judge(i,n*i)){
                flag=1;
                printf("%05d / %05d = %d\n",n*i,i,n);
            }
        }
        if(!flag)
        printf("There are no solutions for %d.\n",n);
        
        
    }
    getchar();
    return 0;
}

 

posted on 2016-08-20 22:13  SijingLin  阅读(432)  评论(0编辑  收藏  举报

导航