Maximum GCD (stringstream)题解
Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possiblepair of these integers.
Input
The first line of input is an integer N (1 < N < 100) that determines the number of test cases.The following N lines are the N test cases. Each test case contains M (1 < M < 100) positiveintegers that you have to find the maximum of GCD.
Output
For each test case show the maximum GCD of every possible pair.
Sample Input
3
10 20 30 40
7 5 12
125 15 25
Sample Output
20
1
25
思路:
暴力gcd,之前小紫看到的stringstream可以拿出来用了,很方便啊,这里就用到了这个技巧
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define INF 0x3f3f3f3f
#define ll long long
const int N=16005;
const ll MOD=998244353;
using namespace std;
ll gcd(ll a,ll b){
return b==0? a:gcd(b,a%b);
}
int main(){
int n;
ll num[110],x,MAX=-100000000;
string s;
scanf("%d",&n);
getchar();
while(n--){
getline(cin,s);
int sum=0;
MAX=-100000000;
stringstream ss(s);
while(ss>>x){
num[sum++]=x;
}
for(int i=0;i<sum;i++){
for(int j=i+1;j<sum;j++){
MAX=max(MAX,gcd(num[i],num[j]));
}
}
cout<<MAX<<endl;
}
return 0;
}
Maximum GCD (stringstream)题解的更多相关文章
- UVA 11827 Maximum GCD
F - Maximum GCD Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Given the ...
- 邝斌带你飞之数论专题--Maximum GCD UVA - 11827
Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible p ...
- Maximum GCD(UVA 11827)
Problem:Given the N integers, you have to find the maximum GCD (greatest common divisor) of every po ...
- Codeforces Round #651 (Div. 2) A Maximum GCD、B GCD Compression、C Number Game、D Odd-Even Subsequence
A. Maximum GCD 题意: t组输入,然后输入一个n,让你在区间[1,n]之间找出来两个不相等的数a,b.求出来gcd(a,b)(也就是a,b最大公约数).让你求出来最大的gcd(a,b)是 ...
- Maximum GCD(fgets读入)
Maximum GCD https://vjudge.net/contest/288520#problem/V Given the N integers, you have to find the m ...
- UVA - 11827 - Maximum GCD,10200 - Prime Time (数学)
两个暴力题.. 题目传送:11827 Maximum GCD AC代码: #include <map> #include <set> #include <cmath> ...
- UVA11827 Maximum GCD
/* UVA11827 Maximum GCD https://vjudge.net/contest/153365#problem/V 数论 gcd 水题,然而读入比较坑 * */ #include ...
- Codeforces Round #651 (Div. 2) A. Maximum GCD(数论)
题目链接:https://codeforces.com/contest/1370/problem/A 题意 有 $n$ 个数大小分别为 $1$ 到 $n$,找出两个数间最大的 $gcd$ . 题解 若 ...
- Codeforces Round #651 (Div. 2) A. Maximum GCD (思维)
题意:在\(1\)~\(n\)中找两个不相等的数使得他们的\(gcd\)最大. 题解:水题,如果\(n\)是偶数,那么一定取\(n\)和\(n/2\),\(n\)是奇数的话,取\(n-1\)和\((n ...
随机推荐
- 青蛙的约会---poj1061(扩展欧几里德)
题目链接:http://poj.org/problem?id=1061 就是找到满足 (X+mt)-(Y+nt) = Lk 的 t 和 k 即可 上式可化简为 (n-m)t + Lk = X-Y;满足 ...
- Python开发【Tornado】:搭建文件下载服务、音频文件播放
Tornado 如何做文件下载 要求:浏览器输入url地址,直接弹窗提示下载 Tornado服务端,搭建文件下载服务 #!/usr/bin/env python # -*- coding:utf-8 ...
- Linux IO多路复用之epoll网络编程及源码(转)
原文: 前言 本章节是用基本的Linux基本函数加上epoll调用编写一个完整的服务器和客户端例子,可在Linux上运行,客户端和服务端的功能如下: 客户端从标准输入读入一行,发送到服务端 服务端从网 ...
- ubuntu 下关闭apache服务自动启动
最近在自己的ubuntu安装了apache服务器,每次开机的时候通过: ps -A 命令发现apache服务总是自动启动,如下: 本来自己的电脑内存就小,现在也不用这个服务,所以想让apa ...
- struts2 OGNL(Object-Graph Navigation Language) 井号,星号,百分号
1.“#”主要有三种用途: 访问OGNL上下文和Action上下文,#相当于ActionContext.getContext():可以访问这几个ActionContext中的属性. parameter ...
- mysql 数据操作 多表查询 多表连接查询 外链接之左连接 右连接
1.外链接之左连接:优先显示左表全部记录 left join 在内连接的基础上保留左表的记录 即便左表有一条记录和右表没有关系,也把他留下 mysql> select * from empl ...
- [sql]mysql管理手头手册,多对多sql逻辑
各类dbms排名 cs模型 mysql字符集设置 查看存储引擎,字符集 show variables like '%storage_engine%'; show VARIABLES like '%ma ...
- Choose the best route(迪杰斯特拉)
通过做这题,发现了自己的问题很大,做题不是贴代码,而是要了解思想:这题考的是有一个起点的集合,求起点集合到一个终点的最短距离, 本来想用Floy的但一看map[1000][1000]超时,有向图,逆序 ...
- python3专业版安装及破解
1.网址 https://www.jetbrains.com/pycharm/download/#section=windows,打开页面,点击下载专业版 2.这是下载好的文件,双击运行即可. //详 ...
- 使用jmeter进行websocket协议压测
第一步:添加websocket sampler组件 可以使用plugins manager进行添加,首先下载plugins manager组件: 下载路径: https://jmeter-plugi ...