• L1-006. 连续因子


    一个正整数N的因子中可能存在若干连续的数字。例如630可以分解为3*5*6*7,其中5、6、7就是3个连续的数字。给定任一正整数N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。

    输入格式:

    输入在一行中给出一个正整数N(1<N<231)。

    输出格式:

    首先在第1行输出最长连续因子的个数;然后在第2行中按“因子1*因子2*……*因子k”的格式输出最小的连续因子序列,其中因子按递增顺序输出,1不算在内。

    输入样例:

    630
    

    输出样例:

    3
    5*6*7
    
    思路:对题意没有理解清楚,所有调试了一会,这个题不是说所有因子,而是能相乘等于N的因子。好比说12这个数,
    正确的输出应该是2 2*3,而不是3 2*3*4;然后还得注意极值情况,比如0输出1 0、1输出1 1,2输出1 2等等。
    #include<stdio.h>
    #include<set>
    #include<vector>
    #include<functional>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int num; cin >> num;
        vector<int>vec;
        set<int>se;
        vec.push_back(num);
        for (int i = 2; i <= (int)sqrt(num); i++)
        {
            if (num%i == 0){
                if (se.find(i) == se.end()){
                    vec.push_back(i);
                    se.insert(i);
                }
                if (se.find(num / i) == se.end()){
                    vec.push_back(num / i);
                    se.insert(num / i);
                }
            }
        }
        
        sort(vec.begin(), vec.end());
    
        int cnt = 1, max = 1, maxp = 0;
        for (int i = 1; i < vec.size(); i++)
        {
            if (vec.at(i) == vec.at(i - 1) + 1)
                cnt++;
            else cnt = 1;
    
            if (max < cnt){
                max = cnt;
                maxp = i - max + 1;
            }
        }
    
        cout << max << endl;
        for (int i = maxp; i < maxp + max; i++)
        {
            if (i == maxp)
                cout << vec[i];
            else cout << "*" << vec[i];
        }
        if (max != 0) cout << endl;
        
        return 0;
    }
    未AC代码
    #include<stdio.h>
    #include<set>
    #include<vector>
    #include<functional>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int num; cin >> num;
    
        int max = 0, maxp = 0;
        for (int i = 2; i <= (int)sqrt(num); i++)
        {
            for (int j = i, cnt = 1; cnt*j <= num; cnt *= j, j++)
            {
                if (num % (cnt*j) != 0)
                    break;
                //cout << cnt << " " << j << endl;
                
                if (max < j - i + 1){
                    max = j - i + 1;
                    maxp = i;
                }
            }
        }
        
        if (max == 0){
            cout << 1 << endl;
            cout << num << endl;
            return 0;
        }
    
        cout << max << endl;
        for (int i = maxp; i < max + maxp; i++)
        {
            if (i != maxp)cout << "*";
            cout << i;
        }
        cout << endl;
        return 0;
    }

    方法来自:https://www.cnblogs.com/zhien-aa/p/5635582.html

    
    
  • 相关阅读:
    面对苹果的抄袭指责,小米到底有没有抄袭?
    如何用Ajax传一个数组数据
    为何日本人如此重视孩子的早餐问题
    常见编程语言对REPL支持情况小结
    坚持未必都是美德,也可能是无知
    PHP 5.4语法改进与弃用特性
    解决CI框架的Disallowed Key Characters错误提示
    如何抓取开了gzip的网页
    CodeIgniter自带的数据库类使用介绍
    Python内部变量与外部变量
  • 原文地址:https://www.cnblogs.com/zengguoqiang/p/8570463.html
Copyright © 2020-2023  润新知