【问题标题】:Program that count prime factorazation of numbers计算数字素数分解的程序
【发布时间】:2020-03-19 20:12:47
【问题描述】:

我想编写一个程序,用计数器而不是scanf 查找数字的素数分解。我已经实现了一些代码,但没有得到我想要的结果!

#include<stdio.h>
#define MAX 1000

int main()
{
    int num;
    int counter;
    int factor = 2;

    for (counter = 2; counter <= MAX; counter++) {
        printf("factorazation of number %d is", counter);

        while (factor<counter) {
            int power = 0;
            if (counter%factor == 0) {

                //  int power=0;
                while (counter%factor == 0) {
                    counter = counter / factor;
                    power++;
                }
                printf("%d^%d\n", factor, power);
                if (counter != 1)
                    printf("X");


            }
            factor++;
        }

        if (counter != 1)
            printf("%d^1.\n", factor);

        //  printf("factorazation of number %d is",counter);
    }
}

【问题讨论】:

  • 你想要什么结果!你为什么用!结束你的句子?

标签: c counter primes prime-factoring factor-analysis


【解决方案1】:

你至少有两个错误。

1) 您需要在 for 循环中将 factor 设置回 2

2) 在循环中同时修改counter 时,不能使用counter 获取下一个数字。您需要使用两个变量。

喜欢:

int current;   
for (current= 2; current<= MAX; current++) {
    factor = 2;         // set factor back to 2
    counter = current;  // make a copy of current into counter
                        // so that it's okay to modify counter inside the loop
    printf("factorazation of number %d is", counter);

此外,您还需要修正 \n 的使用才能获得漂亮的打印效果。

【讨论】:

  • 非常感谢这是我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
相关资源
最近更新 更多