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

kuaidi.ping-jia.net  作者:佚名   更新日期:2024-09-11
用c语言编程:abcde/fghij=n,其中a~j为数字0~9的不同排列.n的值从2到79。

我看了下你这逻辑是对的啊

然后我也按你的逻辑写了代码,输出正常啊。。。

#include
int main()
{
int p,n,m,i,j,a[10],jud;
scanf("%d",&p);//读入要测试的数
n=1234;
for(;n<=98765;n++)//n穷举到最大的98765
{
jud=0;
m=n; // 不能破坏循环变量n的值
for(i=4;i>=0;i--)
{
a[i]=m%10;
m=m/10;//将n的5位数字分别存在数组中
}
if(n%p!=0)
continue;//如果n不能被p整除,则跳到下个循环
else
{
m=n/p; // 分母需要重新赋值,不能与分子相同
for(i=9;i>=5;i--)
{
a[i]=m%10;
m=m/10;//若能整除,则把5个数字分别存在数组中
}
}
for(i=0;i<=8;i++)
{
for(j=i+1;j<=9;j++)
if(a[i]==a[j])
{
jud=1;
break;//判断a[0]~a[9]中有无重复数字
}
if(jud==1)
break;//若有重复数字,则跳出循环
}
if(jud==0)//若没有重复数字,则输出这组数据
{
for(i=0;i<=4;i++)
printf("%d",a[i]);
printf("/");
for(i=5;i<=9;i++)
printf("%d",a[i]);
printf("=%d
",p);
}
}
return 0;
}

之前已在另一个题里作答了(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; /* minimal possible fghij */
    abcde_max = 98765; /* maximal possible abcde */
     
    t = clock(); /* timer counting starts */
    for (n = n_min; n <= n_max; n++)
    {
        fghij = fghij_min;
        do {
            abcde = n * fghij;
                
            if (isunique(abcde, fghij) == 0)
            {            
               printf(" %05d = %05d * %d
", abcde, fghij, n);
               count ++;
            }
             
            fghij ++;   
        } while (abcde < abcde_max);
         
    }
    
    t = clock() - t;  /* end of timer */
    printf("Total found: %d
", count);
    printf("Time elapsed: %d clicks (%g seconds).
", 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);
}

  • c语言 输入一个字符一个数,s[10]=“ABCDE”。如:输入*和数组3,则输出...
    答:include <stdio.h> include <string.h> int main(){ char s[10]="ABCDE";printf("请输入一个字符和一个数字,以逗号分隔:");int a;char b;scanf("%c,%d",&b,&a);int len = strlen(s);if (a>10){ a=10;} s[a-1] = b;for (int i=0; i<10; i++){ printf("%c",...
  • 用c语言编程:abcde/fghij=n,其中a~j为数字0~9的不同排列.n的值从2到...
    答:n_max; size_t abcde_max; size_t fghij_min; clock_t t; n_min = 2; n_max = 79; fghij_min = 1234; /* minimal possible fghij */ abcde_max = 98765; /* maximal possible abcde */ t = clock(); /* timer counting starts */ for (n = n...
  • c语言编程 输入学生的成绩等级abcde,输出相应的成绩范围
    答:include "stdio.h"int main(void){ char a[][7]={"90~100","80~89","70~79","60~69","0~59"},ch; printf("Please enter the class code(Error is quit)...\n"); while(scanf(" %c",&ch)==1) if(ch|=0x20,ch>='a' && ch<='e') printf("%s\n",...
  • C语言,输出百分制成绩等级ABCDE,对应关系:A 91~100 B 81~90 C 71~80...
    答:int main(void){ int mark;puts("请输入百分制分数(0~100):");scanf("%d",&mark);puts("对应的ABCDE等级为:");if(mark>90 && mark<101)putchar('A');else if(mark>80 && mark<91)putchar('B');else if(mark>70 && mark<81)putchar('C');else if(mark>59 && mark<71)p...
  • c语言中,字符串"abcde"在内存中占多少
    答:占用6个字节的位置,其中前面的abcde占用5个字节的位置,最后不可见的'\0'占一个字符。你也可以用sizeof得到其大小。include<stdio.h>int main(void){printf("%d",(int)sizeof("abcde"));return 0;}
  • 用c语言求从A、B、C、D、E中一次取3个字母进行排列的方法的总数和情况...
    答:char s[6]="ABCDE";for (i=0;i<5;i++){ for (j=0;j<5;j++){ for (k=0;k<5;k++){ if ( i !=j && i != k && j!=k){ printf("%c%c%c\n",s[i],s[j],s[k]);n++;} } } } printf("n=%d\n",n);return 0;} 得到:ABC ABD ABE ACB ACD ...
  • 用c语言实现 ABCDE按照全排列输出所有结果
    答:void main(){ char i,j,k,m,n;for(i='A';i<='E';i++)for(j='A';j<='E';j++)for(k='A';k<='E';k++)for(m='A';m<='E';m++)for(n='A';n<='E';n++)if(i!=j&&i!=k&&i!=m&&i!=n&&j!=k&&j!=m&&j!=n&&k!=m&&k!=n&&m!=n)printf("%c %c ...
  • 怎么通过c语言的for循环按顺序列出: ABCDEF ABCDE ABCD ABC AB A...
    答:include <stdio.h>int main(){int i, j;for (i = 6; i>0; --i){for (j = 0; j < i; ++j){printf("%c", 'A' + j);}printf("\n");}return 0;}运行:
  • C语言 main() {char a[]={"ABCDE"}; char *p=a; *(p+2)+=2; printf...
    答:因为p+2指向a[2],然后+=2,所以a[2]的值变为E。p+1指向a[1],输出是从a[1]开始输出字符串,所以答案是 BEDE
  • c语言题目,输出以下图形 A ABC ABCDE ABCDEFG ABCDEFGHI …… 共十行...
    答:include <stdio.h> main(){ int i,j;for(i=0;i<10;i++){ for(j=0;j<2*i+1;j++){ printf("%c",65+j);} printf("\n");} } 应该能看得懂吧,不懂再追问~~