zoukankan      html  css  js  c++  java
  • 1007: 数的分解----课程作业

     

    1007: 数的分解

    Time Limit: 1 Sec  Memory Limit: 32 MB

    Description

    大于1的正整数n可以分解为:n=x1*x2*…*xm。 (xi != 1 )
    例如,当n=12 时,共有8 种不同的分解式: 
    12=12; 12=6*2; 12=4*3; 12=3*4; 12=3*2*2; 12=2*6; 12=2*3*2; 12=2*2*3。 
    当n为质数时,至少也有一种分解法:n = n。
    对于给定的正整数n,计算n共有多少种不同的分解式。

    Input

    多组数据,可能会有超过10000组。

    每组数据输入一行整数N。(0 < N < = 3000)

    Output

    每组数据输出一行整数M,表示对整数N有多少种分解方法。

    Sample Input

    4
    3
    8
    12

    Sample Output

    2
    1
    4
    8

    HINT

    4 = 2 * 2 = 4


    3 = 3


    8 = 2 * 4 = 2 * 2 * 2 = 4 * 2 = 8

    好久没有写waterOJ的题目博客了。。。最近作业略多,,都是赶在上课前才写的。。。

    这道题比较水,真的不要想太多。。。

    首先我们举个例子吧:

    12=12; 12=6*2; 12=4*3; 12=3*4; 12=3*2*2; 12=2*6; 12=2*3*2; 12=2*2*3。

    首先我们可以知道12的因子有 2 3 4 6 12,那么怎么去找到像 3*2*2这样的并且还有不同顺序的呢?其实只要看两个的就可以了,比如12=3*4=3*2*2,那么就得出了一种三个的组合的,也就是说,我们只要判断当前的因子当中还有没有能再分解出更小的因子的组合,如果可以的话就再分解。这样就能得到所有的组合了,那么用递归就很好实现了。

    下面是代码:

     1 #include <iostream>
     2 #include <stdio.h>
     3 using namespace std;
     4 
     5 int num = 1;//计数器,计算符合条件的组合个数
     6 void partitonNum(int partiton) {
     7     for (int i = 2; i <= partiton / 2; i++) {
     8         if (partiton % i == 0) {
     9             num++;
    10             partitonNum(partiton / i);//判断能不能再分解成更小因子的组合
    11         }
    12     }
    13 }
    14 
    15 int main(int argc, char const *argv[])
    16 {
    17     int partiton;
    18     while (scanf("%d", &partiton) != EOF) {
    19         num = 1;
    20         partitonNum(partiton);
    21         printf("%d
    ", num);
    22     }
    23     return 0;
    24 }
  • 相关阅读:
    List of the best open source software applications
    Owin对Asp.net Web的扩展
    NSwag给api加上说明
    'workspace' in VS Code
    unable to find valid certification path to requested target
    JMeter的下载以及安装使用
    exception disappear when forgot to await an async method
    Filter execute order in asp.net web api
    记录web api的request以及response(即写log)
    asp.net web api的源码
  • 原文地址:https://www.cnblogs.com/xiezhw3/p/3494787.html
Copyright © 2011-2022 走看看