题目描述:

给出n个正整数,任取两个数分别作为分子和分母组成最简真分数,编程求共有几个这样的组合。

输入:

输入有多组,每组包含n(n<=600)和n个不同的整数,整数大于1且小于等于1000。
当n=0时,程序结束,不需要处理这组数据。

输出:

每行输出最简真分数组合的个数。

样例输入:
7
3 5 7 9 11 13 15
3
2 4 5
0
样例输出:
17
2 此题两两选择,若最大公约数为1,则是最简真分数 代码如下
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; int num[]; int calGCD(int a, int b) {
int sum = a + b;
a = max(a, b);
b = sum - a;
if (b == ) {
return a;
}
return calGCD(b, a%b);
} int main() {
int n;
while (scanf("%d", &n) != EOF && n != ) {
for (int i = ; i < n; i++) {
scanf("%d", &num[i]);
}
int ans = ;
for (int i = ; i < n; i++) {
for (int j = i + ; j < n; j++) {
if (calGCD(num[i], num[j]) == ) {
ans++;
}
}
}
printf("%d\n", ans);
}
return ;
}

注意求最大公约数的代码,可以写的十分简洁,

辗转相除法

 int gcd(int a, int b) {
if (b == ) return a;
return gcd(b, a%b);
}

最新文章

  1. VS的安装
  2. Python学习【第八篇】Set集合
  3. HD1046An Easy Task
  4. mysql卸载
  5. Android之parseSDKContent failed
  6. ftp nfs samba比较
  7. Java内存模型(转载)
  8. ovs+dpdk numa感知特性验证
  9. Html5笔记之第五天
  10. uva11806
  11. Django数据库操作中You are trying to add a non-nullable field &#39;name&#39; to contact without a default错误处理
  12. Python version 2.7 required, which was not found in the registry解决方法
  13. Vue系列之 =&gt; 组件中的data和methods
  14. 代码统计工具cloc
  15. 用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 热更新Live2D
  16. Python学习--Selenium模块简单介绍(1)
  17. Kivy 中文教程 实例入门 简易画板 (Simple Paint App):0. 项目简介 &amp; 成果展示
  18. Js组件的一些写法
  19. MyBatis-Plus 3.0.7.1
  20. MyBatis学习(一)---配置文件,Mapper接口和动态SQL

热门文章

  1. LR脚本中常用函数使用介绍
  2. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C A Weakness and Poorness (三分)
  3. noip模拟赛#15
  4. 一些常用的HTML标签
  5. 在CNN中使用Tensorflow进行数据增强
  6. 使用Process组件访问本地进程
  7. mysql crash cource 书中实例
  8. c++作业:递归调用,例题4.5 求第五个人的年龄
  9. 禅与 Objective-C 编程艺术(Zen and the Art of the Objective-C Craftsmanship)
  10. IOS7的变化