L1-009 N个数求和 (20分)[PTA][gcd][lcm][模拟]

24 篇文章 0 订阅

本题的要求很简单,就是求N个数字的和。麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和也必须是有理数的形式。

输入格式:

输入第一行给出一个正整数N(≤100)。随后一行按格式a1/b1 a2/b2 ...给出N个有理数。题目保证所有分子和分母都在长整型范围内。另外,负数的符号一定出现在分子前面。

输出格式:

输出上述数字和的最简形式 —— 即将结果写成整数部分 分数部分,其中分数部分写成分子/分母,要求分子小于分母,且它们没有公因子。如果结果的整数部分为0,则只输出分数部分。

输入样例1:

5
2/5 4/15 1/30 -2/60 8/3

输出样例1:

3 1/3

输入样例2:

2
4/3 2/3

输出样例2:

2

输入样例3:

3
1/3 -1/6 1/8

输出样例3:

7/24

ac代码: 

//  main.cpp
//  猫猫头
//
//  Created by Mintink on 2020/1/12.
//  Copyright © 2020 Mintink. All rights reserved.
//

#include<iostream>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<vector>
#include<queue>
#include<array>
#include <stdlib.h>
#include <stdio.h>
#include<cstdio>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
//tuple<int,string,int>p[55];
//pair<int,string>male[55],female[55];
priority_queue<ll,vector<ll>,greater<ll> >qa;//升序,大根堆
priority_queue<ll>qd;//降序,小根堆
//const int mod=10e9+7;
typedef long long  ll;
//vector<int>v;
/*
//Max common factor
ll gcd(ll a,ll b)
{
    ll m;
    m=a%b;
    while(m!=0)
    {
        a=b;
        b=m;
        m=a%b;
    }
    return b;
}*/
int arr[10];
int a[11];
int main()
{
    string tele;
    cin>>tele;
    int number=0;
    for(int i=0;i<11;i++)
    {
        arr[tele[i]-'0']++;
        /*if(arr[tele[i]-'0']==1)
            number++;*/
    }
    cout<<"int[] arr = new int[]{";
    //int out=0;
    for(int i=9;i>=0;i--)
    {
        if(arr[i])
        {
            a[number++]=i;
            /*cout<<i;
            out++;
            if(out<number)
                cout<<",";
            else
                cout<<"};\n";*/
        }
    }
    for(int i=0;i<number-1;i++)
    {
        cout<<a[i]<<",";
    }
    cout<<a[number-1]<<"};\n";
    cout<<"int[] index = new int[]{";
    for(int i=0;i<11;i++)
    {
        for(int j=0;j<number;j++)
        {
            if(a[j]==(tele[i]-'0'))
            {
                cout<<j;
                if(i<10)
                    cout<<",";
                else
                    cout<<"};\n";
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值