zoukankan      html  css  js  c++  java
  • L1-006 连续因子 (20分)

    题意分析

    题目中已经将意思说的很清楚了,就是输出一个数的最长连续因子的个数,并且输出是哪几个因子相乘。可以将题目从这两个角度进行分析:

    • N为素数时,最长连续因子的个数为1,即它自己。
    • N不为素数时,即N为合数时,暴力模拟即可,将连续的数进行累积,直到累积后的结果不能被N整除为止,这样就能够不断更新最长连续因子的个数,预保留第一个数,就可以在最终输出是能够直接输出这几个连续因子。

    AC代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<bitset>
    #include<cassert>
    #include<cctype>
    #include<cmath>
    #include<cstdlib>
    #include<ctime>
    #include<deque>
    #include<iomanip>
    #include<list>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    int N;
    bool isprime(int n)
    {
        if(n == 2)
            return true;
        for(int i = 2; i <= sqrt(n); i++)
        {
            if(n % i == 0)
                return false;
        }
        return true;
    }
    int main()
    {
    //    freopen("input.txt", "r", stdin);
    //    freopen("output.txt", "w", stdout);
        scanf("%d", &N);
        if(isprime(N))
        {
            cout << 1 << endl;
            cout << N << endl;
        }
        else
        {
            int maxlen = 0, x;
            for(int i = 2; i <= sqrt(N); i++)
            {
                if(N % i == 0)
                {
                    int sx = i;
                    int j;
                    for(j = i + 1; j <= sqrt(N); j++)
                    {
                        sx *= j;
                        if(N % sx != 0)
                            break;
                    }
                    if(maxlen < j - i)
                    {
                        maxlen = j - i;
                        x = i;
                    }
                }
            }
            cout << maxlen << endl;
            for(int i = x; i <= x + maxlen - 1; i++)
            {
                if(i != x)
                    cout << "*";
                cout << i;
            }
            cout << endl;
        }
    }
    
    
    
  • 相关阅读:
    《分布式系统关注点——数据一致性(上篇)》阅读笔记
    2.23寒假学习记录
    2.22寒假学习记录
    2.21寒假学习记录
    2.20寒假学习记录
    2.19寒假学习记录
    2.18寒假学习记录
    2.17寒假学习记录
    2.17周一毕设改进计划
    2.16寒假学习记录
  • 原文地址:https://www.cnblogs.com/KeepZ/p/12202532.html
Copyright © 2011-2022 走看看