明知道这是一场意外 2016-02-28 13:12 采纳率: 66.7%
浏览 1483
已采纳

刷ACM的小伙伴快进HDU 5428The Factor(*)

链接在这
Problem Description

There is a sequence of n positive integers. Fancycoder is addicted to learn their product, but this product may be extremely huge! However, it is lucky that FancyCoder only needs to find out one factor of this huge product: the smallest factor that contains more than 2 factors(including itself; i.e. 4 has 3 factors so that it is a qualified factor). You need to find it out and print it. As we know, there may be none of such factors; in this occasion, please print -1 instead.

Input

The first line contains one integer T (1≤T≤15), which represents the number of testcases.

For each testcase, there are two lines:

  1. The first line contains one integer denoting the value of n (1≤n≤100).

  2. The second line contains n integers a1,…,an (1≤a1,…,an≤2×109), which denote these n positive integers.

Output

Print T answers in T lines.

Sample Input

2
3
1 2 3
5
6 6 6 6 6

Sample Output

6
4

Source

BestCoder Round #54 (div.2)

 有一个数列,FancyCoder沉迷于研究这个数列的乘积相关问题,但是它们的乘积往往非常大。幸运的是,FancyCoder只需要找到这个巨大乘积的最小的满足如下规则的因子:这个因子包含大于两个因子(包括它本身;比如,4有3个因子,因此它是满足这个要求的一个数)。你需要找到这个数字并输出它。但是我们知道,对于某些数可能没有这样的因子;在这样的情况下,请输出-1.

我的代码如下:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define LL long long
using namespace std;
LL a[111],b[111];
int main()
{
    int T;
    cin>>T;
    int i,n;
    while(T--)
    {
        cin>>n;
        int signum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%ld",&a[i]);
           for(int j=2;j<=sqrt(a[i]);j++)
            {
                if(a[i]%j==0){
                    b[++signum]=j;
                }break;
            }
            if(a[i]!=1&&a[i]%2!=0||a[i]==2){
                b[++signum]=a[i];
            }

        }
        if(signum<2)
            cout<<-1<<endl;
        else{
            sort(b+1,b+signum+1);
            long long desnum=b[1]*b[2];
            cout << desnum <<endl;
        }
    }
    return 0;
}

结果一直显示Wrong Answer,研究了一天了,求大神解答

  • 写回答

6条回答 默认 最新

  • u52983610 2016-02-29 02:48
    关注

    稍微修改了下你的代码,accepted了

     #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #define LL long long
    using namespace std;
    LL a[111],b[111111];
    int main()
    {
        int T;
        cin>>T;
        int i,n;
        while(T--)
        {
            cin>>n;
            int signum=0;
            for(i=1;i<=n;i++)
            {
                scanf("%ld",&a[i]);
                LL tempa=a[i];//保存a[i]被除后的剩余
                int countf=0;//只保留a[i]最小的两个素因子
               for(int j=2;j<=sqrt(a[i]);j++)
                {
                    if(tempa%j==0){//第一个或第二个素因子
                        tempa=tempa/j;//剩余
                        b[++signum]=j;//保存
                        b[++signum]=tempa;//保存和这个因子配对的大于sqrt(a[i])的素因子(例子:26 37,结果应该是26,但是13是不会循环到的)
    
                        countf++;
                        if(countf==2){//判断是否是第二个素因子
                            break;
                                            }
                        if(tempa%j==0){//如果上面是第一个素因子,试下剩余是否仍然包含这个素因子,比如 3 8,2是8的第一个,但是剩余4仍然有2
                            b[++signum]=j;
                             b[++signum]=tempa;
    
                            break;
                        }
                    } 
                }
                if(a[i]!=1&&countf==0){//如果不是1而且没有素因子,就是素数,直接加入
                    b[++signum]=a[i];
                }
    
            }
            if(signum<2)
                cout<<-1<<endl;
            else{
                sort(b+1,b+signum+1);
                long long desnum=b[1]*b[2];
                cout << desnum <<endl;
            }
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器