zoukankan      html  css  js  c++  java
  • 数学问题 | 连续质因数分解:1096

    这题真的是触及到了我的知识盲区,写了一个16分的答案,看了答案(开长整型找不到结果的特殊判断)之后改成了18分,还是没有AC。终于,我仔细一看标准代码,发现这题不简单。

    wa代码:

    #include <stdio.h>
    #include <memory.h>
    #include <math.h>
    #include <string>
    #include <vector>
    #include <set>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <map>
    
    #define I scanf
    #define OL puts
    #define O printf
    #define F(a,b,c) for(a=b;a<c;a++)
    #define FF(a,b) for(a=0;a<b;a++)
    #define FG(a,b) for(a=b-1;a>=0;a--)
    #define LEN 1010
    #define MAX (1<<30)-1
    #define V vector<int>
    
    typedef long long ll;
    
    using namespace std;
    
    
    
    int main(){
        int n;
        n=6;
    //    I("%lld",&n);
        int t=n,i;
        vector<int> ans;
        vector<int> cur;
        bool flag=0;
        for(i=2;i<=sqrt(n);i++){
            if(t%i==0){
                cur.push_back(i);
                t/=i;
                flag=1; 
            }else{
                if(flag){    //断了 
                    t=n;
                    if(cur.size()>ans.size()){// || (cur.size()>0&&ans.size()>0&& cur.size()==ans.size() && cur[0]<ans[0])
                        ans=cur;
                    }
                    cur.clear();
                }
                flag=0; 
            }
        }
        if(cur.size()>ans.size()){
            ans=cur;
        }
        int sz=ans.size();
        if(sz){
            O("%d
    ",sz);
            FF(i,sz){
                O("%d",ans[i]);
                if(i!=sz-1) O("*");
            }    
        }else O("%d
    %d",1,n);
    
        return 0;
    }
    View Code

    抄了一遍答案代码(没力气默了),感觉写的比我的垃圾强多了。

    AC代码:

    #include <stdio.h>
    #include <memory.h>
    #include <math.h>
    #include <string>
    #include <vector>
    #include <set>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <map>
    
    #define I scanf
    #define OL puts
    #define O printf
    #define F(a,b,c) for(a=b;a<c;a++)
    #define FF(a,b) for(a=0;a<b;a++)
    #define FG(a,b) for(a=b-1;a>=0;a--)
    #define LEN 1010
    #define MAX (1<<30)-1
    #define V vector<int>
    
    typedef long long ll;
    
    using namespace std;
    
    
    
    int main(){
        ll n;
        n=630;
        I("%lld",&n);
        ll i;
        int ansI=0,ansLen=0;
        for(i=2;i<=sqrt(n);i++) if(n%i==0){
            ll tmp=1,j=i;
            while(1){
                tmp*=j;
                if(n%tmp) break;
                if(j-i+1>ansLen){
                    ansI=i;
                    ansLen=j-i+1;
                }
                j++;
            }
        }
        if(ansLen){
            O("%d
    ",ansLen);
            for(ll i=0;i<ansLen;i++){
                O("%lld",ansI+i);
                if(i<ansLen-1) O("*");
            }
        }else O("%d
    %d",1,n);
    
        return 0;
    }
  • 相关阅读:
    tar.xz文件如何解压
    warnings and errors when executing : make -j4
    ubuntu关机
    Linux系统kernel编译替换升级
    安装linux内核
    二叉树的度数和节点数的关系
    刷题--将搜索二叉树转换成双向链表
    刷题--二叉搜索树与双向链表
    刷题--删除链表中重复的节点
    四舍五入输出
  • 原文地址:https://www.cnblogs.com/TQCAI/p/8568831.html
Copyright © 2011-2022 走看看