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.
Addition of rational
numbers is defined as follows. Note that the right hand side of this equality
will not necessarily be in lowest terms.

A
rational number for which the numerator is greater than or equal to the
denominator can be displayed in mixed format, which includes a whole number part
and a fractional part.
For example, 51/3 is a mixed format representation of
the rational number 16/3. Your task is to write a program that reads a sequence
of rational numbers and displays their sum.

Input

Input will consist of specifications for a series of tests. Information for
each test begins with a line containing a single integer 1 <= n < 1000
indicating how many values follow. A count of zero terminates the input.
The
n following lines each contain a single string with no embedded whitespace .
Each string represents a rational number, which could be in any of the following
forms and will not necessarily be in lowest terms (w, n, and d are integers: 0
<= w,n < 1000, 1 <= d < 1000).
• w,n/d: a mixed number equivalent
to the rational number (w*d + n) / d.
• n/d: a rational number with a zero
whole number part
• w: a whole number with a zero fractional
part

Output

Output should consist of one line for each test comprising the test number
(formatted as shown) followed by a single space and the sum of the input number
sequence. The sum should be displayed in lowest terms using mixed number format.
If either the whole number part or the fractional part is zero, that part should
be omitted. As a special case, if both parts are zero, the value should be
displayed as a single 0.

Sample Input
2
1/2
1/3
3
1/3
2/6
3/9
3
1
2/3
4,5/6
0
Sample Output
Test 1: 5/6
Test 2: 1
Test 3: 6,1/2
Problem Source
HNU Contest 

Mean:

给你n个数,其中包含分数、整数,对这n个数求和。

analyse:

按照题目意思模拟即可,主要考察coding能力.

Time complexity:O(n)

Source code:

//Memory   Time
//  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的更多相关文章

  1. [模拟] hdu 4452 Running Rabbits

    意甲冠军: 两个人在一个人(1,1),一个人(N,N) 要人人搬家每秒的速度v.而一个s代表移动s左转方向秒 特别值得注意的是假设壁,反弹.改变方向 例如,在(1,1),采取的一个步骤,以左(1,0) ...

  2. [ACM_模拟] HDU 1006 Tick and Tick [时钟间隔角度问题]

    Problem Description The three hands of the clock are rotating every second and meeting each other ma ...

  3. 优先队列 + 模拟 - HDU 5437 Alisha’s Party

    Alisha’s Party Problem's Link Mean: Alisha过生日,有k个朋友来参加聚会,由于空间有限,Alisha每次开门只能让p个人进来,而且带的礼物价值越高就越先进入. ...

  4. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  5. HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  6. HDU 5102 The K-th Distance(模拟)

    题意:输入一棵树,输出前k小的点对最短距离dis(i,j)的和. 模拟,官方题解说得很清楚了.不重复了. http://bestcoder.hdu.edu.cn/ 需要注意的是,复杂度要O(n+k), ...

  7. hdu 5071(2014鞍山现场赛B题,大模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 思路:模拟题,没啥可说的,移动的时候需要注意top的变化. #include <iostr ...

  8. HDU 5510---Bazinga(指针模拟)

    题目链接 http://acm.hdu.edu.cn/search.php?action=listproblem Problem Description Ladies and gentlemen, p ...

  9. HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现 ...

随机推荐

  1. 5天玩转C#并行和多线程编程 —— 第五天 多线程编程大总结

    5天玩转C#并行和多线程编程系列文章目录 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel 5天玩转C#并行和多线程编程 —— 第二天 并行集合和PLinq 5天玩转C#并行和多线程编 ...

  2. Javascript事件模型系列(三)jQuery中的事件监听方式及异同点

    作为全球最知名的js框架之一,jQuery的火热程度堪称无与伦比,简单易学的API再加丰富的插件,几乎是每个前端程序员的必修课.从读<锋利的jQuery>开始,到现在使用jQuery有一年 ...

  3. 如何用Unity GUI制作HUD

    若知其所以然,自然知其然. HUD是指平视显示器,就是套在脸上,和你的眼睛固定在一起,HUD的意思就是界面咯,一般我们说HUD特指把3D空间中的界面的某些信息(比如血条,伤害之类)的贴在界面上,对应3 ...

  4. 移动APP的自动化测试

    开发移动应用,最耗时耗力的就是手动测试APP的每个功能点或修复bug.有人就会提议App的业务逻辑可以使用nUnit或xUnit测试单元来辅助完成.那用户界面要如何测试?众所周知,移动设备多种多样,数 ...

  5. WPF入门教程系列六——布局介绍与Canvas(一)

    从这篇文章开始是对WPF中的界面如何布局做一个较简单的介绍,大家都知道:UI是做好一个软件很重要的因素,如果没有一个漂亮的UI,功能做的再好也无法吸引很多用户使用,而且没有漂亮的界面,那么普通用户会感 ...

  6. 彻底理解跨域解决方案JSONP

    什么是同源策略? 同源策略,它是由Netscape提出的一个著名的安全策略.现在所有支持JavaScript 的浏览器都会使用这个策略. 所谓同源是指,域名,协议,端口相同.当一个浏览器的两个tab页 ...

  7. 分析优秀的.NET 文档设计工具Vsdocman 7.1 软件保护技术

    Vsdocman是一个优秀的.NET源代码注释编写工具,方便的以GUI的方式设计.NET源代码的注释. 我们知道.NET源代码的注释是Xml格式的注释,在生成程序集时,只需用选中生成Xml注释,Vis ...

  8. 如何查看某个查询用了多少TempDB空间

        最近帮助客户调优的过程中,发现客户的TempDB存在非常大的压力,经过排查是发现某些语句对TempDB的巨量使用所导致.     在SQL Server中,TempDB主要负责供下述三类情况使 ...

  9. struts2默认action

    struts.xml文件的某个package中添加<default-action-ref name="error"></default-action-ref> ...

  10. Struts2第一个例子Hello World!

    1.首先用eclipse新建一个动态web项目Struts2Demo1: (把Default output floder由build\classes改成WebContent\WEB-INF\class ...