九度oj 题目1465:最简真分数
2024-10-21 04:55:38
- 题目描述:
-
给出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);
}
最新文章
- VS的安装
- Python学习【第八篇】Set集合
- HD1046An Easy Task
- mysql卸载
- Android之parseSDKContent failed
- ftp nfs samba比较
- Java内存模型(转载)
- ovs+dpdk numa感知特性验证
- Html5笔记之第五天
- uva11806
- Django数据库操作中You are trying to add a non-nullable field &#39;name&#39; to contact without a default错误处理
- Python version 2.7 required, which was not found in the registry解决方法
- Vue系列之 =>; 组件中的data和methods
- 代码统计工具cloc
- 用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 热更新Live2D
- Python学习--Selenium模块简单介绍(1)
- Kivy 中文教程 实例入门 简易画板 (Simple Paint App):0. 项目简介 &; 成果展示
- Js组件的一些写法
- MyBatis-Plus 3.0.7.1
- MyBatis学习(一)---配置文件,Mapper接口和动态SQL
热门文章
- LR脚本中常用函数使用介绍
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C A Weakness and Poorness (三分)
- noip模拟赛#15
- 一些常用的HTML标签
- 在CNN中使用Tensorflow进行数据增强
- 使用Process组件访问本地进程
- mysql crash cource 书中实例
- c++作业:递归调用,例题4.5 求第五个人的年龄
- 禅与 Objective-C 编程艺术(Zen and the Art of the Objective-C Craftsmanship)
- IOS7的变化