问题描述
- C语言 带分数问题 求大神帮忙看看
-
/*------------------------------------------------------------------------------------------------------------------标题:带分数100 可以表示为带分数的形式:100 = 3 + 69258 / 714还可以表示为:100 = 82 + 3546 / 197注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。题目要求:从标准输入读入一个正整数N (N<1000*1000)程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。例如:用户输入:程序输出:再例如:用户输入:程序输出:left=1时,down每调试到一定的数值后就返回了,不知道为什么,求大神帮忙看看---------------------------------------------------------------------------------------------------------------------------*/#include<stdio.h>#include<math.h>int check(int n);int check_allHave();int qiu_wei_shu(int n);int flag[10]={0};int main(){ int N=0; // N=left+up/down int left=0; int up=0; int down=0; int count=0; //计数 int wei_shu_left=0; int temp[10]={0}; scanf(""%d""&N); for(left = 1;left < N;left++){ if(check(left)) continue; //不合理 wei_shu_left = qiu_wei_shu(left); for(int i = 0;i < 10;i++) temp[i] = flag[i]; for(down = 1;down < pow(109-wei_shu_left-1);down++){ //down最多 9-wei_shu_left-1 位数 up = (N - left) * down; for(int i = 0;i < 10;i++) //重置为left循环时的flag为新一轮down做准备 flag[i] = temp[i]; if(check(down) || check(up)) continue; //不合理 else if(check_allHave() == 0) count++; } } printf(""%dn""count); return 0; } int check(int n){ while(n){ flag[n%10]++; n/=10; } if(flag[0]) return 1; //判断是否有0 for(int i = 1;i <= 9;i++){ //判断1~9是否重复 if(flag[i] > 1) return 1; } return 0;}int check_allHave() //最后判断1~9是否都包含了{ for(int i = 1;i <= 9;i++){ if(flag[i] != 1) return 1; } return 0; } int qiu_wei_shu(int n){ int wei_shu=0; while(n){ n%10; n/=10; wei_shu++; } return wei_shu;}
解决方案
带分数的概念不是这样的吧,分数部分必须是分子小于分母才对
解决方案二:
100真能分解成11组?我怎么只能得到4组?
105只有5组……
解决方案三:
N = a+b/c
N = 100 a = 3 b = 69258 c= 714
>> N = 100 a = 81 b = 5643 c= 297
>> N = 100 a = 81 b = 7524 c= 396
>> N = 100 a = 82 b = 3546 c= 197
>> N = 105 a = 72 b = 6534 c= 198>> N = 105 a = 87 b = 3456 c= 192>> N = 105 a = 87 b = 9612 c= 534>> N = 105 a = 92 b = 5681 c= 437>> N = 105 a = 92 b = 6734 c= 518
解决方案四:
N = 100 a = 3 b = 69258 c= 714 N = 100 a = 81 b = 5643 c= 297 N = 100 a = 81 b = 7524 c= 396 N = 100 a = 82 b = 3546 c= 197 N = 100 a = 91 b = 5742 c= 638 N = 100 a = 91 b = 5823 c= 647 N = 100 a = 91 b = 7524 c= 836 N = 100 a = 94 b = 1578 c= 263 N = 100 a = 96 b = 1428 c= 357 N = 100 a = 96 b = 1752 c= 438 N = 100 a = 96 b = 2148 c= 537 N = 105 a = 72 b = 6534 c= 198 N = 105 a = 87 b = 3456 c= 192 N = 105 a = 87 b = 9612 c= 534 N = 105 a = 92 b = 5681 c= 437 N = 105 a = 92 b = 6734 c= 518 N = 105 a = 98 b = 3647 c= 521
时间: 2024-10-01 13:29:22