因式分解,但是不知道哪里错了
#include <stdio.h>int main()
{
int num,x,y,z,sum;
printf("input num:");
scanf("%d",&num);
for(x=1;x<=num;x++)
{
for(y=1;y<=num;y++)
{
for(z=1;z<=num;z++)
{ sum=x*y*z;
if(num==sum)
printf("%d=%d*%d*%d\n",num,x,y,x);
}
}
}
return 0;
} #include <stdio.h>
#include <math.h>
int main(void) {
int num;
scanf("%d", &num);
for (int i = 1; i < sqrt(num) + .5; ++i) {
if (!(num % i)) {
printf("%2d = %2d * %2d\n", num, i, num / i);
}
}
return 0;
}90
90 =1 * 90
90 =2 * 45
90 =3 * 30
90 =5 * 18
90 =6 * 15
90 =9 * 10 本帖最后由 傻眼貓咪 于 2022-5-3 10:55 编辑
#include <stdio.h>
#include <math.h>
int main(void) {
int num, a, b, c;
scanf("%d", &num);
for (a = 1; a < sqrt(num) + .5; ++a) {
if (!(num % a)) {
printf("%d = %d * %d\n", num, a, num / a);
for (b = 2; b < sqrt(num / a) + .5; ++b) {
if (!((num / a) % b) && (a <= (num / a) && a <= b) && ((num / a) != 1)) {
c = (num / a) / b;
printf("%d = %d * %d * %d\n", num, a, b, c);
}
}
for (b = 2; b < sqrt(a) + .5; ++b) {
if (!(a % b) && (a / b) != b) {
c = a / b;
printf("%d = %d * %d * %d\n", num, b, c, num / a);
}
}
}
}
return 0;
}90
90 = 1 * 90
90 = 1 * 2 * 45
90 = 1 * 3 * 30
90 = 1 * 5 * 18
90 = 1 * 6 * 15
90 = 1 * 9 * 10
90 = 2 * 45
90 = 2 * 3 * 15
90 = 2 * 5 * 9
90 = 3 * 30
90 = 3 * 3 * 10
90 = 3 * 5 * 6
90 = 5 * 18
90 = 6 * 15
90 = 2 * 3 * 15
90 = 9 * 10 printf("%d=%d*%d*%d\n",num,x,y,x);
。。。打印写错了,xyx? 本帖最后由 jackz007 于 2022-5-3 12:01 编辑
因数分解难道不应该分解为素数之积?
#include <stdio.h>
void factor(int d[] , int n)
{
int i , x , y ;
for(i = 0 , x = 2 , y = n ; x < y + 1 ;) {
if(! (y % x)) {
d[++ i] = x ;
y /= x ;
} else {
x ++ ;
}
}
d = i ;
}
int main(void)
{
int d , i , num ;
printf("input num : ") ;
scanf("%d" , & num) ;
factor(d , num) ;
printf("%d = %d" , num , d) ;
for(i = 1 ; i < d ; i ++) printf(" x %d" , d) ;
printf("\n") ;
}
编译、运行实况:
g++ -o x2 x2.c
D:\\>x2
input num : 90
90 = 2 x 3 x 3 x 5
D:\\>x2
input num : 19
19 = 19
D:\\>x2
input num : 17
17 = 17
D:\\>x2
input num : 5
5 = 5
D:\\>x2
input num : 360
360 = 2 x 2 x 2 x 3 x 3 x 5
D:\\> jackz007 发表于 2022-5-3 11:57
因数分解难道不应该分解为素数之积?
编译、运行实况:
谢谢你!这个论坛真好,我在其他地方问都没人回我,也没人帮我看。。谢谢你!{:10_250:} 风车呼呼呼 发表于 2022-5-3 10:47
。。。打印写错了,xyx?
谢谢!我在其他地方问都没人帮我看。。谢谢你!{:10_250:} 傻眼貓咪 发表于 2022-5-3 10:15
谢谢你愿意帮我看!好感动!{:10_250:} addendum777 发表于 2022-5-3 15:58
谢谢你愿意帮我看!好感动!
不客气,共同学习学习{:10_257:}
页:
[1]