BestCoder Round 38-1002 Greatest Greatest Common Divisor
제목:
Greatest Greatest Common Divisor
Accepts: 271
Submissions: 1138
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
Pick two numbers ai,aj(i≠j) from a sequence to maximize the value of their greatest common divisor.
Input
Multiple test cases. In the first line there is an integer T , indicating the number of test cases. For each test cases, the first line contains an integer n , the size of the sequence. Next line contains n numbers, from a1 to an . 1≤T≤100,2≤n≤105,1≤ai≤105 . The case for n≥104 is no more than 10 .
Output
For each test case, output one line. The output format is Case # x : ans , x is the case number, starting from 1 , ans is the maximum value of greatest common divisor.
Sample Input
2
4
1 2 3 4
3
3 6 9
Sample Output
Case #1: 2
Case #2: 3
제목:
주어진 시퀀스 에서 임의의 두 수의 공약수 의 최대 치 를 구하 십시오.
hack 때 다른 사람의 코드 를 봤 는데 생각 이 별로 차이 가 없 는 것 같 습 니 다. 대중 해법 인 것 같 습 니 다.
하나의 수의 인 자 는 그의 제곱 근 까지 만 매 거 할 수 있 고, 다른 한 쪽 은 나 누 면 된다.그리고 각 인자 의 개 수 를 하나의 배열 로 저장 합 니 다.마지막 으로 큰 것 부터 작은 것 까지 어떤 인자 가 2 보다 크 면 바로 답 이다.
코드:
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
int store[100010],status[100010];
int main()
{
int n,t,maxx,tmp;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
memset(status,0,sizeof(status));
scanf("%d",&n);
for(int j=0;j<n;j++)
{
scanf("%d",&store[j]);
}
for(int k=0;k<n;k++)
{
tmp=store[k];
for(int x=1;x*x<=tmp;x++)
{
if(tmp%x==0)
{
status[x]++;
status[tmp/x]++;
}
}
}
for(int x=100000;x>=1;x--)
{
if(status[x]>=2)
{
maxx=x;
break;
}
}
printf("Case #%d: %d
",i,maxx);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기본 산술 계산 방법 (개 찾기 면접 문제 중 하나)당시 면접 관 은 두 개의 숫자 를 주 고 그 숫자 를 계산 하 는 방법 이 몇 가지 가 있 느 냐 고 물 었 다. 나 는 당시 에 네 가지 만 대답 했다. 나 는 지금 총 결 해 보 겠 다. awk 로 계산 하기 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.