要在给定日期之后啊!!!
不能是负的啊!!!
直接限制范围来枚举即可:
int main()
{
int t,y;
scanf(“%d”,&t);
for(y=0;y<t;y++){
int p,e,i,d,x,j,k;
scanf(“%d%d%d%d”,&p,&e,&i,&d);
for(j=d+1;j<=21252;j++){
if((j-p)%23==0) break;
}
for(k=j;k<=212522;k+=23){
if((k-e)%28==0) break;
}
for(x=k;x<=21252;x+=23*28){
if((x-i)%33==0) break;
}
printf(“case #%d:\n”,y);
printf(“the next triple peak occurs in %d days.\n”,x-d);
}
return 0;
}
为什么总是编译错误呢,明明答案没问题
int main()
{
int cases;
scanf(“%d”,&cases);
int c;
for(c=0;c<cases;c++)
{
int p,e,i,d;
int num;
scanf(“%d %d %d %d”,&p,&e,&i,&d);
printf(“case #%d:\n”,c);
for(num=1;num<=21252;num++)
if((num%23)==p%23)
if((num%28)==e%28)
if((num%33)==i%33)
{
printf(“the next triple peak occurs in %d days.\n”,num-d);
break;
}
}
return 0;
}
总是说没有定义c,还说type 前少了;
有没有解决办法啊
说好的不超过365呢……买数据才发现最后一个测试数据有400……
附上不假思索写的C代码(没想到取余,就用了个最笨的)
#include <stdio.h>
int main()
{
int T, I;
scanf("%d",&T);
for(I=0; I<T; I++)
{
int p, e, i, d, n, x[22000]={0};
scanf("%d %d %d %d",&p,&e,&i,&d);
for(n=-40; p+23*n<22000; n++) if(p+23*n>0) x[p+23*n]++;
for(n=-40; e+28*n<22000; n++) if(e+28*n>0) x[e+28*n]++;
for(n=-40; i+33*n<22000; n++) if(i+33*n>0) x[i+33*n]++;
for(n=0; n<22000; n++) if(x[n]==3 && n>d) {printf("case #%d:\nthe next triple peak occurs in %d days.\n",I,n-d); break;}
}
return 0;
}
原来是中国剩余定理…
虽然这种数据暴力躺着随便过。
#include <bits/stdc++.h>
using namespace std;
int arr[3];
int mm[3];
int c_time = 0;
int extend_gcd(int a, int b, int &x, int &y)
{
if(b == 0){
x = 1;
y = 0;
return a;
} else {
int r = extend_gcd(b,a%b,y,x);
y -= x*(a/b);
return r;
}
}
int CRT(int a[], int m[], int n)
{
int x = 0,y = 0;
int M = 1;
for (int i = 0 ; i < n ; ++i) M *= m[i];
int ret = 0;
for (int i = 0 ; i < n ; ++i){
int tm = M / m[i];
extend_gcd(tm,m[i],x,y);
ret = (ret + tm*x*a[i])%M;
}
ret = ret%M;
while(ret <= c_time)
ret += M;
return ret;
}
int main()
{
mm[0] = 23; mm[1] = 28 ; mm[2] = 33;
int T = 0;
int cc = 0;
int getTime = 0;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d",&arr[0],&arr[1],&arr[2],&c_time);
getTime = CRT(arr,mm,3);
printf("case #%d:\nthe next triple peak occurs in %d days.\n",cc++,getTime-c_time);
}
return 0;
}
不仅不能是负的,连zero也是不可以的......是吧,电碳。(=·ω·=)