zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) Practice 1096 Consecutive Factors (20分)

    1.题目

    Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

    Input Specification:

    Each input file contains one test case, which gives the integer N (1<N<2​31​​).

    Output Specification:

    For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]*factor[2]*...*factor[k], where the factors are listed in increasing order, and 1 is NOT included.

    Sample Input:

    630
    

    Sample Output:

    3
    5*6*7

    2.题目分析

    先判断是不是素数,是的话就输出个数为1,因子为素数本身,不是的话就从2开始2 3 4……判断是否能被n取余得0,到不能为止记录个数与位置,再从3开始,4开始……,当相乘的总因子>n/2时退出循环。

    3.代码

    #include<iostream>
    #include<cmath>
    using namespace std;
    bool isprime(int x)
    {
    	if (x == 0 || x == 1)return false;
    	for (int i = 2; i <= sqrt(x); i++)
    		if (x%i == 0)return false;
    	return true;
    }
    
    int main()
    {
    	int n, max = -1, maxi;
    	scanf("%d", &n);
    	int start = 2,mul=2;
    	int count = 0;
        if(isprime(n)){printf("1
    ");printf("%d",n);return 0;}
    	while (1)
    	{
    		 count = 0;
    		int temp = start;
    		int temp2 = mul;
    		while (n%temp2 == 0)
    		{
    			count++;
    			temp2 = temp2*(temp + 1); temp++;
    		}
    		if (max < count-1) { max = count-1; maxi = temp-1; }
    		start++; mul++;
    		if (temp2 > n/2)break;
    	}
    	printf("%d
    ", max+1);
    	for (int i = maxi-count; i<maxi; i++)
    	{
    		printf("%s%d", i == maxi - count ? "" : "*",  i+1);
    	}
    }
  • 相关阅读:
    3个百度网盘下载实用小技巧
    decodeURIComponent 解码函数
    background属性怎么添加2个或多个背景图
    本地运行vue项目webpack提示 Compiled successfully
    var和let区别简述
    picture元素的使用
    博客园自定义鼠标icon
    background-size:100% 100% 和 background-size:cover的区别简述
    ScreenToGif——gif动图工具使用说明
    离职个人社保补缴——程序员也应该知道的社保基础知识科普
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788843.html
Copyright © 2011-2022 走看看