永发信息网

用c语言编程:abcde/fghij=n,其中a~j为数字0~9的不同排列.n的值从2到79。统计这样的组合一共有多少种

答案:3  悬赏:30  手机版
解决时间 2021-02-14 10:05
答案:281 要求:用时越少越好,使用以下方式可以测出程序运行时间(大数据量才可看出程序差别优劣)#include printf("Time used %.6f\n",(double)clock()/CLOCKS_PER_SEC);
最佳答案
之前已在另一个题里作答了(question/563123081)。 这里再贴一下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int isunique(const size_t abcde, const size_t fghij);
int compare(const void * a, const void * b);

int main(int argc, char** argv) 
{
    size_t count = 0;
    int abcde, fghij, n;
    size_t n_min, n_max;
    size_t abcde_max;
    size_t fghij_min;
    clock_t t;

    n_min = 2;
    n_max = 79;
    fghij_min = 1234; 
    abcde_max = 98765; 

    t = clock(); 
    for (n = n_min; n <= n_max; n++)
    {
        fghij = fghij_min;
        do {
            abcde = n * fghij;

            if (isunique(abcde, fghij) == 0)
            {            
               printf(" %05d = %05d * %d\n", abcde, fghij, n);
               count ++;
            }

            fghij ++;   
        } while (abcde < abcde_max);

    }

    t = clock() - t;  
    printf("Total found: %d\n", count);
    printf("Time elapsed: %d clicks (%g seconds).\n", t, ((double)t)/CLOCKS_PER_SEC); 

    return 0;
}

int isunique(const size_t a, const size_t b)
{
    char buffer[10];
    int c1, c2;

    if ((a > 99999) || (b > 99999))
       return -2;

    c1 = snprintf(buffer, 5, "%05d", a);       
    c2 = snprintf(buffer+c1, 5, "%05d", b);

    qsort(buffer, sizeof(buffer)/sizeof(buffer[0]), sizeof(char), compare);

    return strncmp(buffer, "0123456789", 10);    
}

int compare(const void * a, const void * b)
{
    return ( *(char*)a - *(char*)b );    
}在我电脑上的输出结果:
 13458 = 06729 * 2
...
 98736 = 01452 * 68
Total found: 281
Time elapsed: 407 clicks (0.407 seconds).

最近这类题似乎比较多。。。。
全部回答
不是281种,是992941种,一共是10! 种组合 #include<stdio.h> #include <time.h> main() {int a,b,c,d,e,f,g,h,i,j,k=0; float n=0; printf("kaishi:\n"); for(a=0;a<10;a++) for(b=0;b<10;b++) {if(b==a) continue; else for(c=0;c<10;c++) {if(c==b||c==a) continue; else for(d=0;d<10;d++) {if(d==a||d==b||d==c) continue; else for(e=0;e<10;e++) {if(e==a||e==b||e==c||e==d) continue; else for(f=0;f<10;f++) {if(f==a||f==b||f==c||f==d||f==e||a<=f) continue; else for(g=0;g<10;g++) {if(g==a||g==b||g==c||g==d||g==e||g==f||(a*10000+b*1000)<2*(f*10000+g*1000)) continue; else for(h=0;h<10;h++) {if(h==a||h==b||h==c||h==d||h==e||h==f||h==g||(a*10000+b*1000+c*100)<2*(f*10000+g*1000+h*100)) continue; else for(i=0;i<10;i++) {if(i==a||i==b||i==c||i==d||i==e||i==f||i==g||i==h||(a*10000+b*1000+c*100+d*10)<2*(f*10000+g*1000+h*100+i*10)) continue; else for(j=0;j<10;j++) {if(j==a||j==b||j==c||j==d||j==e||j==f||j==g||j==h||j==i) continue; else {n=(a*10000+b*1000+c*100+d*10+e)/(f*10000+g*1000+h*100+i*10+j); if(n>=2.0&&n<=79.0) k++; if(k<100000) printf("%f\n",n);}}}}}}}}}} printf("K=%d\n",k); printf("Time used %.6f\n",(double)clock()/CLOCKS_PER_SEC); }
根据题意,abcde最大的可能值为98765;而fghij最小的可能值是01234, 因此结合n=2~79, 作两个循环判断所有的取值; 判断的标准就是:把abcde和fghij按字符串合并,排序后和字符串0123456789比较即可。代码如下: #include  #include  #include  int isunique(const size_t abcde, const size_t fghij); int compare(const void * a, const void * b); int main(int argc, char** argv)  {     size_t count = 0;     int abcde, fghij, n;     size_t n_min, n_max;     size_t abcde_max;     size_t fghij_min;     n_min = 2;     n_max = 79;     fghij_min = 1234;      abcde_max = 98765;      for (n = n_min; n <= n_max; n++)     {         fghij = fghij_min;         do {             abcde = n * fghij;             if (isunique(abcde, fghij) == 0)             {                            printf(" %05d = %05d * %d\n", abcde, fghij, n);                count ++;             }             fghij ++;            } while (abcde < abcde_max);     }     printf("total found: %d\n", count);     return 0; } int isunique(const size_t a, const size_t b) {     char buffer[10];     int c1, c2;     if ((a > 99999) || (b > 99999))        return -2;     c1 = snprintf(buffer, 5, "%05d", a);            c2 = snprintf(buffer+c1, 5, "%05d", b);     qsort(buffer, sizeof(buffer)/sizeof(buffer[0]), sizeof(char), compare);     return strncmp(buffer, "0123456789", 10);     } int compare(const void * a, const void * b) {     return ( *(char*)a - *(char*)b );     }输出:  13458 = 06729 * 2 ...  98736 = 01452 * 68 total found: 281
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
线性代数在自动化中的应用
宾县司法局宾安司法所在什么地方啊,我要过去
刚用完延时湿巾,1个小时后想做第二次,影响吗
guava jar的源码怎么处理
发动机脱缸一般多少钱,有车友可以回答一下这
为什么外国家长不让自己的朋友当着小孩的面,
三和木门怎么去啊,有知道地址的么
沈阳医大二院周日有大夫吗
已知等腰三角形的两边长分别为8cm和10cm,求周
外孙子能给外公外婆上坟
今天刚换完机油感觉怎么动力不如以前了
从河北师大汇华学院北院到石家庄火车站北站坐
旧时意沧桑过,后面
永昌雅居怎么去啊,有知道地址的么
东莞市师范生没有考上公办教师,报到证报道地
推荐资讯
求一种玩具名称 由一个圆盘和轴组成 用链条拉
我的电脑配置是a10-7850k,8g2133内存,128g固
我们不是孤军奋战,下一句是什么
复源亭我想知道这个在什么地方
单选题若点M(1,m)和点N(2,n)都在函数y=-x+1
角弓邮政支局这个地址在什么地方,我要处理点
跑步机上的字母:time?
踊跃汽车用品批发通榆店怎么去啊,有知道地址
会当凌绝顶,一览众山晓是说什么名胜古迹
中兴大道/G70(路口)我想知道这个在什么地方
一个关于审计独立性的问题 小刘是一家会计师
藏语我爱你怎么写图片
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?