zoukankan      html  css  js  c++  java
  • PAT 天梯赛 L1-006. 连续因子 【循环】

    题目链接

    https://www.patest.cn/contests/gplt/L1-006

    思路

    输出的连续因子 的乘积 也要是这个数的因子 就每个数先找它的单因子
    然后每个单因子往上一个一个遍历 当 n % sum != 0 的时候 退出来 然后 比较 此答案 和 原答案的大小 如果 更大 就替换
    要注意 质数的情况

    AC代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <cstring>
    #include <map>
    #include <stack>
    #include <set>
    #include <cstdlib>
    #include <ctype.h>
    #include <numeric>
    #include <sstream>
    
    using namespace std;
    typedef long long LL;
    
    const double PI  = 3.14159265358979323846264338327;
    const double E   = 2.718281828459;
    const double eps = 1e-6;
    
    const int MAXN = 0x3f3f3f3f;
    const int MINN = 0xc0c0c0c0;
    const int maxn = 1e5 + 5;
    const int MOD  = 1e9 + 7;
    
    int a[maxn];
    
    int main()
    {
        int n;
        cin >> n;
        int i, j;
        int vis = sqrt(n) + 1;
        memset(a, 0, sizeof(a));
        int temp;
        for (i = 2, j = 0; i <= vis; i++)
        {
            if (n % i == 0)
                a[j++] = i;
        }
        int len = j;
        int max = 0, ans;
        LL sum;
        for (i = 0; i < len; i++)
        {
            sum = a[i];
            vis = 1;
            for (j = 1; ; j++)
            {
                sum *= (a[i] + j);
                if (n % sum != 0)
                    break;
                else
                    vis++;
            }
            if (vis > max)
            {
                max = vis;
                ans = a[i];
            }
        }
        if (len == 0)
        {
            cout << 1 << endl;
            cout << n << endl;
        }
        else
        {
            printf("%d
    ", max);
            for (i = 0; i < max; i++)
            {
                if (i)
                    printf("*");
                printf("%d", ans + i);
            }
            cout << endl;
        }
    }
  • 相关阅读:
    C++格式化输入输出
    算法的时间复杂度和空间复杂度
    C++编程中const和#define的区别
    C#中结构体和类的区别
    SQL之删除触发器
    Windows添加和取消右键管理员权限
    SQL之trigger(触发器)
    SQL VIEW(视图)
    二分查找的实现
    C++中this指针
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433267.html
Copyright © 2011-2022 走看看