24h購物| | PChome| 登入
2013-07-12 14:14:33| 人氣588| 回應0 | 上一篇 | 下一篇

[UVA][字串分析] 11148 - Moliu Fractions

推薦 0 收藏 0 轉貼0 訂閱站台

Problem A: Moliu Fractions

Time limit: 10 seconds


[Vocabulary] moliu (adj) - (Cantonese) Something is moliu if it is senseless, stupid, boring etc.

As the title suggests, this task is very dull, so... those who hate boring tasks may consider skipping this one. ☺

The reason why this task is so boring is that it is taken from my younger brother's maths homework. He is currently learning "multiplication of fractions", and his teacher has given him loads of related exercises. And... as you know, all the questions in the exercises are so dull in the following aspects:

  1. They are unrealistic and meaningless; what's the meaning of finding the weight of 3/8 bar of soap? Or 5/7 cup of cola?
  2. The answers are nearly always ugly. Values like 13/24 kilograms are not uncommon.
  3. Most important of all, you don't even need to look into the problem statements in order to get the solutions! Just pick out all the numbers stated, multiply them together, and you get the correct answer!!!

For the above reasons, instead of wasting our time in solving such boring questions by hand, why don't we just let the computer to do the work? Please write a program that solves the questions by the method of "pick out numbers then multiply"!

Input and Output

The first line of input contains an integer T. Each of the next T lines is a question to which your program should compute the answer as described above.

This task deals with three types of operands, namely: integers, proper fractions and mixed fractions. As an example, the first question in the sample input reads:

In each question there are at least 2 and at most 4 operands, all of which are well-formatted as shown in sample input. There is always a space (ASCII #32) immediately after each operand. All operands are non-negative, so your result will also be greater than or equal to 0. There are no improper fractions in the input, and your output should also not contain any. Of course, you will have to give your results in the simplest form. If your result can be expressed as an integer, you should not output it as a proper fraction or a mixed fraction. You do not have to worry about integer overflow (after all, the maths exercises are targeted to kids :-).

Sample Input

2
What is the product of 2 , 6/55 and 3-1/3 ?
A cookie weighs exactly 18-2/5 g. What is the weight of a packet of 12 cookies?

Sample Output

8/11
220-4/5

Problemsetter: Mak Yan Kei

題目要求把字串內的所有分數抓出來相乘。不用擔心 overflow。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;
int gcd(int x, int y) {
    int t;
    while(x%y)
        t = x, x = y, y = t%y;
    return y;
}
int main() {
    int testcase;
    char ss[105];
    int i;
    scanf("%d", &testcase);
    while(getchar() != '\n');
    while(testcase--) {
        string s;
        getline(cin, s);
        for(i = 0; i < s.length(); i++) {
            if(s[i] >= 'a' && s[i] <= 'z')
                s[i] = ' ';
            else if(s[i] >= 'A' && s[i] <= 'Z')
                s[i] = ' ';
        }
        stringstream sin(s);
        int p = 1, q = 1;
        while(sin >> s) {
            if(s[0] < '0' || s[0] > '9')
                continue;
            strcpy(ss, s.c_str());
            int a, b, c;
            if(sscanf(ss, "%d-%d/%d", &a, &b, &c) == 3) {
                b = b+a*c;
                p *= b, q *= c;
            } else if(sscanf(ss, "%d/%d", &b, &c) == 2) {
                p *= b, q *= c;
            } else if(sscanf(ss, "%d", &a) == 1) {
                p *= a;
            }
            int g = gcd(p, q);
            p /= g, q /= g;
        }
        if(p == 0)
            printf("0");
        int flag = 0;
        if(p/q) printf("%d", p/q), flag = 1;
        if(p%q) {
            if(flag)    putchar('-');
            printf("%d/%d", p%q, q);
        }
        puts("");
    }
    return 0;
}

台長: Morris
人氣(588) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][math] 10787 - Modular Equations
此分類上一篇:[UVA][greedy] 11158 - Elegant Permuted Sum

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文