zoukankan      html  css  js  c++  java
  • C语言 > 分解质因数

    题目内容:
    每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数。比如,6可以被分解为2x3,而24可以被分解为2x2x2x3。

    现在,你的程序要读入一个[2,100000]范围内的整数,然后输出它的质因数分解式;当读到的就是素数时,输出它本身。

    提示:可以用一个函数来判断某数是否是素数。

    输入格式:
    一个整数,范围在[2,100000]内。

    输出格式:
    形如:
    n=axbxcxd

    n=n
    所有的符号之间都没有空格,x是小写字母x。abcd这样的数字一定是从小到大排列的。

    输入样例:
    18

    输出样例:
    18=2x3x3
    时间限制:500ms内存限制:32000kb

    #include <stdio.h>
    
    int test(int n); //测试是否为素数
    
    int main() {
        int n;
        scanf("%d", &n);
    
        if (test(n)) {
            printf("%d=%d", n, n);
        } else {
            printf("%d=", n);
            int i = 2;
            while (n > 1) {
                if (n % i == 0) {
                    printf("%d", i);
                    n /= i;
                    if (n != 1) {
                        printf("x");
                    }
                } else {
                    i++;
                }
            }
        }
    
        return 0;
    }
    
    int test(int n) {
    
        for (int i = 2; i < n; i++) {
            if (n % i == 0) {
                return 0;
            }
        }
        return 1;
    }
  • 相关阅读:
    find the most comfortable road
    Rank of Tetris
    Segment set
    Codeforces Round #380 (Div. 2)D. Sea Battle
    A Bug's Life
    Is It A Tree?
    N皇后问题
    符号三角形
    2016 ICPC总结
    Sudoku Killer
  • 原文地址:https://www.cnblogs.com/cccj/p/7667956.html
Copyright © 2011-2022 走看看