HDU 5742 It's All In The Mind

本文提供了一道算法题的解答思路与实现代码,题目要求最大化前两个元素并最小化其余元素,通过输入特定数量的整数及其部分预定义值,最终输出简化后的分数形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单题。a[1],a[2]尽量大,其余尽量小。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;

const int maxn=120;
int T,L,n,m,a[maxn];

int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); }

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(a,-1,sizeof a);
        for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); a[x]=y; }
        L=100; for(int i=1;i<=2;i++) { if(a[i]==-1) a[i]=L; else L=a[i]; }
        L=0; for(int i=n;i>=3;i--) { if(a[i]==-1) a[i]=L; else L=a[i]; }
        int p=0,q=0; p=a[1]+a[2]; for(int i=1;i<=n;i++) q=q+a[i];
        printf("%d/%d\n",p/gcd(p,q),q/gcd(p,q));
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zufezzt/p/5697108.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值