A Rational Sequence (UVALive 7786 && UVALive 7096)

两个题的区别在于,一个是给出n求第n个是多少。一个是已知这个数,求它的下一个数。

在做第二题的时候,因为我做过第一题有一种先入为主的思想,以为求它的下一个数就是先求出它是这个数塔里的第n个,然后求n+1个即可。完全错误,直接找规律求解即可。

7786

Sample Input

4

1 1

2 4

3 11

4 1431655765

Sample Output

1 1/1

2 1/3

3 5/2

4 2178309/1346269

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<deque>
#include<queue>
#include<vector>
#include<stack>
#include<climits>
#include<map>
#include<ctime>
#define INF 0x3f3f3f3f
typedef long long LL;
using namespace std;

map<LL,LL> mp1;
map<LL,LL> mp2;


void f(int n)
{
    if(n==1)
    {
        mp1[1]=1;
        mp2[1]=1;
    }
    else
    {
        if(n%2==0)
        {
            f(n/2);
            mp1[n]=mp1[n/2];
            mp2[n]=mp1[n/2]+mp2[n/2];
        }
        if(n%2==1)
        {
            f(n/2);
            mp2[n]=mp2[n/2];
            mp1[n]=mp1[n/2]+mp2[n/2];
        }
    }
}


int main()
{
    int t,k,n;
    cin>>t;
    while(t--)
    {
        cin>>k>>n;
        f(n);
        cout<<k<<" "<<mp1[n]<<"/"<<mp2[n]<<endl;
    }
    return 0;

}

7096

Sample Input

5

1 1/1

2 1/3

3 5/2

4 2178309/1346269

5 1/10000000 

Sample Output

1 1/2

2 3/2

3 2/5

4 1346269/1860498

5 10000000/9999999

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
typedef long long LL;
using namespace std;
int main()
{
    int t,s;
    cin>>t;
    int k,x,y,xx,yy,xxx,yyy,ans;
    char c;
    while(t--)
    {
        cin>>k>>x>>c>>y;
        if(y==1)
        {
            cout<<k<<" "<<"1"<<c<<x+y<<endl;
        }
        else
        {
            if(x<y)
            {
                cout<<k<<" "<<y<<c<<y-x<<endl;
            }
            else
            {
                ans=x/y;
                x=x%y;

                xx=y;
                yy=y-x;

                xxx=xx;
                yyy=yy+(y*ans);

                cout<<k<<" "<<xxx<<c<<yyy<<endl;
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值