gpt4 book ai didi

c - 输出?质因数分解

转载 作者:行者123 更新时间:2023-11-30 19:18:49 26 4
gpt4 key购买 nike

所以我有这个程序,需要它将输出写为“96 is 2^5 x 3^1”而不是 2x2x2x2x2x3。

    int main() {

int i, n;

// Get the user input.
printf("Please enter a number.\n");
scanf("%d", &n);

// Print header.
printf("The prime factorization of %d is ", n);

// Loop through, finding prime factors.
int cur_factor = 2;
while (cur_factor < n) {

// Found a factor.
if (n%cur_factor == 0) {
printf("%d x ", cur_factor);
n = n/cur_factor;

}

// Going to the next possible factor.
else
cur_factor++;
}

// Prints last factor.
printf("%d.\n", cur_factor);

return 0;

我需要做什么?谢谢

最佳答案

while (cur_factor < n) {

// Found a factor.
while (n%cur_factor == 0) {

n = n/cur_factor;
count++;
}
while(count--)
printf("%d x ", cur_factor);
count=0;

// Going to the next possible factor.
else
cur_factor++;
}

关于c - 输出?质因数分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26197091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com