永发信息网

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

答案:3  悬赏:30  手机版
解决时间 2021-02-14 10:05
  • 提问者网友:临风不自傲
  • 2021-02-13 12:20
答案:281 要求:用时越少越好,使用以下方式可以测出程序运行时间(大数据量才可看出程序差别优劣)#include printf("Time used %.6f\n",(double)clock()/CLOCKS_PER_SEC);
最佳答案
  • 五星知识达人网友:枭雄戏美人
  • 2021-02-13 13:14
之前已在另一个题里作答了(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).

最近这类题似乎比较多。。。。
全部回答
  • 1楼网友:迟山
  • 2021-02-13 15:38
不是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); }
  • 2楼网友:忘川信使
  • 2021-02-13 14:22
根据题意,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
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯