两个题的区别在于,一个是给出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;
}