G题:
这道题就有一个地方需要注意
输入的两个区间端点不一定是从小到大输入的,因此要先对这两个数排一下序
但注意最后不要将排序过的区间输出,要输出原来的。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<set>
const int INF =0x3f3f3f3f;
const int maxn=10010;
using namespace std;
int judge(int i)
{
int cnt=1;
while(i!=1)
{
if(i%2) i=3*i+1;
else i/=2;
cnt++;
}
return cnt;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
int x=min(a,b);
int y=max(a,b);
int ans=0;
for(int i=x;i<=y;i++)
{
int vis=judge(i);
ans=max(ans,vis);
}
printf("%d %d %d\n",a,b,ans);
}
return 0;
}
I 题:
第一种做法:暴力,打表
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<set>
const int INF =0x3f3f3f3f;
const int maxn=40010;
using namespace std;
bool ans[3][maxn];
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
int p,e,i,d;
int n(0);//计录循环数的
while(scanf("%d%d%d%d",&p,&e,&i,&d)!=EOF&&p>=0)
{
memset(ans,0,sizeof(ans));
p=p%23;//得到在一个周期内高峰第几天
e=e%28;
i=i%33;
for(int x=p;x<maxn;x+=23)//把各自的高峰天打张表
ans[0][x]++;
for(int x=e;x<maxn;x+=28)
ans[1][x]++;
for(int x=i;x<maxn;x+=33)
ans[2][x]++;
for(int x=d+1;x<maxn;x++)
if(ans[0][x]==1&&ans[1][x]==1&&ans[2][x]==1)
{
printf("Case %d: the next triple peak occurs in %d days.\n",++n,x-d);
break;
}
}
return 0;
}
第二种做法: 考虑中国剩余定理(孙子定理) 可以百度看看这个定理的解释
已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i
使33×28×a被23除余1,用33×28×8=5544;
使23×33×b被28除余1,用23×33×19=14421;
使23×28×c被33除余1,用23×28×2=1288。
因此有(5544×p+14421×e+1288×i)% lcm(23,28,33) =n+d
又23、28、33互质,即lcm(23,28,33)= 21252;
所以有n=(5544×p+14421×e+1288×i-d)%21252
本题所求的是最小整数解,避免n为负,因此最后结果为n= [n+21252]% 21252
那么最终求解n的表达式就是:
n=(5544*p+14421*e+1288*i-d+21252)%21252;
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<set>
const int INF =0x3f3f3f3f;
const int maxn=10010;
using namespace std;
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
int p,e,i,d,cnt=1;
while(scanf("%d%d%d%d",&p,&e,&i,&d))
{
if(p==-1&&e==-1&&i==-1&&d==-1) break;
int ans=(5544*p+14421*e+1288*i-d+21252)%21252;
if(ans==0) printf("Case %d: the next triple peak occurs in 21252 days.\n",cnt++);
else printf("Case %d: the next triple peak occurs in %d days.\n",cnt++,ans);
}
return 0;
}
T题:
这道题可以采取递归的思想,分为两种状况,n为苹果数,m为盘子数
第一:当n<m时,那么就是将n个苹果分到n个盘的方法
第二:n>=m时,那么1.将至少其中一个盘不放,那么就是n个苹果放到m-1个盘的方法
2.每个盘放一个,然后就是n-m个放在m个盘的放法
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<set>
const int INF =0x3f3f3f3f;
const int maxn=40010;
using namespace std;
int sol(int n,int m)
{
if(n==1||m==1||n==0) return 1;
if(n<m) return sol(n,n);
else return sol(n,m-1)+sol(n-m,m);
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
printf("%d\n",sol(n,m));
}
return 0;
}