模拟 --- 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.
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:

模拟题。

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=1;
int flag[MAX];
char str[MAX][20];
void read()
{
    memset(flag,0,sizeof(flag));
    for(int i=1;i<=n;i++)
    {
        scanf("%s",str[i]);
        int len=strlen(str[i]);
        for(int j=0;j<len;j++)
        {
            if(str[i][j]==‘,‘)
            {
                flag[i]=1;
                break;
            }
            else if(str[i][j]==‘/‘)
            {
                flag[i]=2;
                break;
            }
        }
    }
}

int gcd(int a,int b)
{
    if(b==0)
        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=1,mu=1;
   for(int i=1;i<=n;i++)
   {
        int a,b;
       if(flag[i]==0)   //  6
       {
           sscanf(str[i],"%d",&num);
           zi+=mu*num;
           continue;
       }
       else if(flag[i]==1)    //  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==0)
       {
           zi=zi/mu;
           mu=1;
           continue;
       }
   }
   zi-=mu;
   if(gcd(zi,mu)!=1)
   {
       int tmp=gcd(zi,mu);
       zi/=tmp;
       mu/=tmp;
   }
   if(zi==0||mu==0)
   {
       puts("0");
       return ;
   }
   if(zi>=mu)
   {
       if(zi%mu==0)
       {
           printf("%d\n",zi/mu);
           return ;
       }
       else
       {
           int integer=0;
           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 0;
}

  

模拟 --- hdu 12878 : Fun With Fractions,布布扣,bubuko.com

时间: 2024-10-08 10:04:19

模拟 --- hdu 12878 : Fun With Fractions的相关文章

模拟/hdu 1008 Elevator

题意 开始电梯在0层 给出n个指令,每个代表下一步停到哪层 每往上一层需要6秒,往下一层需要4秒,停止需要5秒 求总时间 分析 数据很小,模拟即可喵- Accepted Code 1 /* 2 PROBLEM:hdu1007 3 AUTHER:Nicole Lam 4 MEMO:模拟 5 */ 6 7 8 #include<cstdio> 9 using namespace std; 10 11 int main() 12 { 13 int n; 14 scanf("%d"

[模拟] hdu 4452 Running Rabbits

题意: 两个人一个人在(1,1),一个人在(N,N) 给每个人每秒移动的速度v,和一个s代表移动s秒后左转方向 特别注意的是如果撞墙,要反弹回去,方向改变 比如在(1,1),往左走一步到(1,0) 其实就是走到了(1,2) 然后如果两个人见面那么交换方向并且不再左转! 思路: 直接模拟.. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath&qu

[模拟] hdu 5308 I Wanna Become A 24-Point Master

题意: 给你n个数,每个数都是n, 然后把这n个数加减乘除,每个数都必须用一次,且只能一次,并且运算完的数也都得用一次. 也就是做n-1次,问能不能得到24,输出过程 xi ? xj  第i个数与第j个数做?运算 这种形式 思路: 因为n有可能很大,那么肯定需要一种完美的构造 最简单的方法是构造出4*6 构造出4需要5个数,构造出6需要7个数 也就是最少需要12个数,剩下的数就是相减为0,然后乘就好了 但是需要注意的就是13是不满足这个规律的 所以1~13特判一下,14以上的按规律输出就好了~

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

Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Mr. Frog recently studied how to add two fractions up, and he came up with an evil ide

hdu 5640 King&#39;s Cake(模拟)

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

hdu 4930 Fighting the Landlords (模拟)

Fighting the Landlords Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 160    Accepted Submission(s): 52 Problem Description Fighting the Landlords is a card game which has been a heat for ye

HDU 4028 The time of a day STL 模拟题

暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define ll __int64 #define N 42 ll n,m,ans;

HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 312    Accepted Submission(s): 219 Problem Description One day, you, a clever boy, feel bored in your math class, and then fall

【模拟】HDU 5752 Sqrt Bo

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5752 题目大意: 定义f(n)=⌊√n⌋,fy(n)=f(fy-1(n)),求y使得fy(n)=1.如果y>5输出TAT.(n<10100) 题目思路: [模拟] 5层迭代是232,所以特判一下层数是5的,其余开根号做.注意数据有0. 队友写的. 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h&g