模拟 --- hdu 12878 : Fun With Fractions
| Fun With Fractions |
| Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB |
| Total submit users: 152, Accepted users: 32 |
| Problem 12878 : No special judgement |
| Problem description |
|
A rational number can be represented as the ratio of two integers, referred to as the numerator (n) and the denominator (d) and written n/d. A rational number's representation is not unique. For example the rational numbers 1/2 and 2/4 are equivalent. A rational number representation is described as "in lowest terms" if the numerator and denominator have no common factors. Thus 1/2 is in lowest terms but 2/4 is not. A rational number can be reduced to lowest terms by dividing by the greatest common divisor of n and d. |
| Input |
|
Input will consist of specifications for a series of tests. Information for |
| Output |
|
Output should consist of one line for each test comprising the test number |
| Sample Input |
2 |
| Sample Output |
Test 1: 5/6 |
| Problem Source |
| HNU Contest |
Mean:
给你n个数,其中包含分数、整数,对这n个数求和。
analyse:
按照题目意思模拟即可,主要考察coding能力.
Time complexity:O(n)
Source code:
// K MS
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1005
#define LL long long
using namespace std;
int n,kase=;
int flag[MAX];
char str[MAX][];
void read()
{
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++)
{
scanf("%s",str[i]);
int len=strlen(str[i]);
for(int j=;j<len;j++)
{
if(str[i][j]==',')
{
flag[i]=;
break;
}
else if(str[i][j]=='/')
{
flag[i]=;
break;
}
}
}
}
int gcd(int a,int b)
{
if(b==)
return a;
else return gcd(b,a%b);
}
int lcm(int a,int b)
{
int x=gcd(a,b);
return a*b/x;
}
void solve()
{
int num;
int zi=,mu=;
for(int i=;i<=n;i++)
{
int a,b;
if(flag[i]==) // 6
{
sscanf(str[i],"%d",&num);
zi+=mu*num;
continue;
}
else if(flag[i]==) // 6,5/3
{
sscanf(str[i],"%d,%d/%d",&num,&a,&b);
zi+=mu*num;
}
else // 5/3
{
sscanf(str[i],"%d/%d",&a,&b);
}
int newmu=lcm(mu,b);
int newa=(newmu/b)*a;
int newzi=(newmu/mu)*zi;
zi=newzi+newa;
mu=newmu;
if(zi%mu==)
{
zi=zi/mu;
mu=;
continue;
}
}
zi-=mu;
if(gcd(zi,mu)!=)
{
int tmp=gcd(zi,mu);
zi/=tmp;
mu/=tmp;
}
if(zi==||mu==)
{
puts("0");
return ;
}
if(zi>=mu)
{
if(zi%mu==)
{
printf("%d\n",zi/mu);
return ;
}
else
{
int integer=;
while(zi>mu)
{
zi-=mu,integer++;
}
printf("%d,%d/%d\n",integer,zi,mu);
return ;
}
}
else
printf("%d/%d\n",zi,mu);
}
int main()
{
// freopen("cin.txt","r",stdin);
// freopen("cout.txt","w",stdout);
while(~scanf("%d",&n),n)
{
read();
printf("Test %d: ",kase++);
solve();
}
return ;
}
模拟 --- hdu 12878 : Fun With Fractions的更多相关文章
- [模拟] hdu 4452 Running Rabbits
意甲冠军: 两个人在一个人(1,1),一个人(N,N) 要人人搬家每秒的速度v.而一个s代表移动s左转方向秒 特别值得注意的是假设壁,反弹.改变方向 例如,在(1,1),采取的一个步骤,以左(1,0) ...
- [ACM_模拟] HDU 1006 Tick and Tick [时钟间隔角度问题]
Problem Description The three hands of the clock are rotating every second and meeting each other ma ...
- 优先队列 + 模拟 - HDU 5437 Alisha’s Party
Alisha’s Party Problem's Link Mean: Alisha过生日,有k个朋友来参加聚会,由于空间有限,Alisha每次开门只能让p个人进来,而且带的礼物价值越高就越先进入. ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))
Fraction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5102 The K-th Distance(模拟)
题意:输入一棵树,输出前k小的点对最短距离dis(i,j)的和. 模拟,官方题解说得很清楚了.不重复了. http://bestcoder.hdu.edu.cn/ 需要注意的是,复杂度要O(n+k), ...
- hdu 5071(2014鞍山现场赛B题,大模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 思路:模拟题,没啥可说的,移动的时候需要注意top的变化. #include <iostr ...
- HDU 5510---Bazinga(指针模拟)
题目链接 http://acm.hdu.edu.cn/search.php?action=listproblem Problem Description Ladies and gentlemen, p ...
- HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现 ...
随机推荐
- AMD加载器实现笔记(四)
继续这一系列的内容,到目前为止除了AMD规范中config的map.config参数外,我们已经全部支持其他属性了.这一篇文章中,我们来为增加对map的支持.同样问题,想要增加map的支持首先要知道m ...
- 关于Xcode5的离线帮助
关于Xcode的离线帮助文档,网上找到的许多都是Xcode4的资料,Xcode5貌似将文档搬到了Help菜单里,而不是原先的<Window> - <Organizer> - & ...
- Java提高篇(三八)-----Java集合细节(四):保持compareTo和equals同步
在Java中我们常使用Comparable接口来实现排序,其中compareTo是实现该接口方法.我们知道compareTo返回0表示两个对象相等,返回正数表示大于,返回负数表示小于.同时我们也知道e ...
- js实现快速排序
非原创: var quickSort = function(arr){ if(arr.length<=1){return arr;} var pivotIndex = Math.floor(ar ...
- 赴美工作常识(Part 2 - 申请)
在<Part 1 - 签证>的评论中有人提到,说我还没说如何申请职位就说签证的事情了.一方面,签证的周期决定了你申请职位的时间,错过关键时间点的话就可能错过重要的机会.另一方面,传统意义上 ...
- [异常解决] ubuntu上安装虚拟机遇到的问题(vmware坑了,virtual-box简单安装,在virtual-box中安装精简版win7)
利用周末时间将整个电脑格式化,换成了ubuntu系统- 所谓:扫清屋子再请客! 但是有些软件只在win上有,于是还是考虑装个虚拟机来个——逐步过度策略,一点点地从win上转移到linux上 我的系统是 ...
- 案例研究:CopyToAsync
返回该系列目录<基于Task的异步模式--全面介绍> 把一个流拷贝到另一个流是有用且常见的操作.Stream.CopyTo 方法在.Net 4中就已经加入来满足要求这个功能的场景,例如在一 ...
- 在Windows下安装Memcached
Windows下的Memcache安装: 需要运行命令行工具cmd 请以管理员权限运行 开始->附件->命令提示符,以管理员身份运行 假如当前C:\windows\system32,输入c ...
- 日志分析系统——Hangout源码学习
这两天看了下hangout的代码,虽然没有运行体验过,但是也算是学习了一点皮毛. 架构浅谈 Hangout可以说是java版的Logstash,我是没有测试过性能,不过据说是kafka这边性能要高出L ...
- 用VC编译lua源码,生成lua语言的解释器和编译器
用VC编译lua源码,生成lua语言的解释器和编译器 1.去网址下载源码 http://www.lua.org/download.html 2.装一个VC++,我用的是VC6.0 3.接下来我们开始编 ...
