PAT (Advanced Level) 1096. Consecutive Factors (20)

如果是素数直接输出1与素数,否则枚举长度和起始数即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

long long n;

bool prime(long long x)
{
    if(x==1) return 0;
    if(x==2) return 1;
    for(long long i=2; i*i<=x; i++)
    {
        if(x%i==0) return 0;
    }
    return 1;
}

int main()
{
    scanf("%lld",&n);
    int f=0;

    if(prime(n))
    {
        printf("1\n");
        printf("%lld\n",n);
    }
    else
    {
        for(int len=10; len>=1; len--)
        {
            long long num=1;
            for(long long i=2;; i++)
            {
                if(i==2)
                {
                    for(long long j=i; j<=i+(long long)len-1; j++)
                    {
                        num=num*j;
                        if(num>n) break;
                    }
                }
                else num=num/(i-1)*(i+len-1);

                if(num>n) break;
                if(n%num==0)
                {
                    f=1;
                    printf("%d\n",len);
                    for(long long j=i; j<=i+len-1; j++)
                    {
                        printf("%lld",j);
                        if(j<i+len-1) printf("*");
                    }
                    printf("\n");
                    break;
                }
            }
            if(f==1) break;
        }
    }
    return 0;
}
时间: 2024-10-26 03:54:50

PAT (Advanced Level) 1096. Consecutive Factors (20)的相关文章

1096. Consecutive Factors (20)——PAT (Advanced Level) Practise

题目信息 1096. Consecutive Factors (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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 consecu

PAT 1096. Consecutive Factors (20)

1096. Consecutive Factors (20) 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

PAT Advanced 1096 Consecutive Factors (20分)

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 maxim

1096. Consecutive Factors (20)

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 maxim

PAT甲题题解-1096. Consecutive Factors(20)-(枚举)

题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可以分解2*3*4*6*7*8,最大的连续个数为3,因为存在两个,输出最小的那个即2*3*4. 首先,一个数如果是合数,那么它的因子必定不会超过sqrt(n)或者sqrt(n)+1.如果为质数,那么只可能为自己,因为题目说了不包括1. 我们先将2~sqrt(n)+1中为n的因子存到factor数组中,

PAT (Advanced Level) 1100. Mars Numbers (20)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; char a[20][6]={ "tret","jan"

PAT (Advanced Level) 1081. Rational Sum (20)

简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; struct FenShu { long l

PAT (Advanced Level) 1084. Broken Keyboard (20)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=100000; char s[maxn],t[maxn],u[m

PAT (Advanced Level) 1112. Stucked Keyboard (20)

找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int k;