Biorhythms(中国剩余定理)
http://shuxueshi.jie.blog.163.com/blog/static/13611628820104179856631/
这篇博客写的很棒!
#include<stdio.h>
#include<string.h>
#include<stdlib.h> int main()
{
int a,b,c,t;
int i,j;
int k = ;
while(scanf("%d%d%d%d",&a,&b,&c,&t)!=EOF)
{
k++;
if(a==- && b==- && c==- && t==-)
{
break;
}
int a1 = a%;//只有每次对23取余等于a1时才能达到高峰。
int b1 = b%;
int c1 = c%;
int a2 = ,b2 = ,c2 = ;
for(i=;i<=;i++)
{
if(((i*(*))% == ) && a2 == )
{
a2 = i*(*);
}
if(((i*(*))% == ) && b2 == )
{
b2 = i*(*);
}
if(((i*(*))% == ) && c2 == )
{
c2 = i**;
}
if(a2!= && b2!= && c2!=)
{
break;
}
}
int a3 = a2 * a1;
int b3 = b2 * b1;
int c3 = c2 * c1;
int aa = a3 + b3 + c3;
int bb = **;
int cc = aa%bb;
if(cc-t<=)//防止出现0 0 0 X的情况
{
printf("Case %d: the next triple peak occurs in %d days.\n",k,cc-t+);
}
else
{
printf("Case %d: the next triple peak occurs in %d days.\n",k,cc-t);
}
}
return ;
}
POJ1006——Biorhythms(中国剩余定理)
Biorhythms
Description
人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一
天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的
高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一
次高峰出现的时间)。你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天的时间(距给定时间的
天数)。例如:给定时间为10,下次出现三个高峰同天的时间是12,则输出2(注意这里不是3)。
Input
输入四个整数:p, e, i和d。 p, e, i分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d 是给定的时间,可能小于p, e, 或 i。 所有给定时间是非负的并且小于365, 所求的时间小于21252。
当p = e = i = d = -1时,输入数据结束。
Output
从给定时间起,下一次三个高峰同天的时间(距离给定时间的天数)。
采用以下格式:
Case 1: the next triple peak occurs in 1234 days.
注意:即使结果是1天,也使用复数形式“days”。
Sample Input
0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1
Sample Output
Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.
题目大意:
中文。(POJ右上角Language可选)
解题思路:
有题可知:设天数为T,则T%23==p;T%28==e;T%33==i 输出答案为T-d(为其中的最小正整数解)
典型的中国剩余定理,根据(T%23==p;T%28==e;T%33==i )可以求出一个T-d和周期lcm(23,28,33)
最小正整数解为(T-d)%lcm(23,28,33);
PS:注意当T-d<=0时,(T-d)%lcm(23,28,33)<=0,故答案是(T-d)%lcm(23,28,33)+lcm(23,28,33)
Code1:

1 #include<stdio.h>
2 #include<iostream>
3 using namespace std;
4 int gcd(int a,int b)
5 {
6 return b==0?a:gcd(b,a%b);
7 }
8 int lcm(int a,int b)
9 {
10 return a/gcd(a,b)*b;
11 }
12 int main()
13 {
14 int p,e,i,d;
15 int times=0;
16 while (cin>>p>>e>>i>>d)
17 {
18 times++;
19 if (p==-1) break;
20 int x1=lcm(23,28);
21 int x2=lcm(23,33);
22 int x3=lcm(28,33);
23 int k1=0,k2=0,k3=0;
24 while ((k1*x1)%33!=1)
25 k1++;
26 while ((k2*x2)%28!=1)
27 k2++;
28 while ((k3*x3)%23!=1)
29 k3++;
30 int sum=k1*x1*i+k2*x2*e+k3*x3*p;
31 sum=sum%(lcm(lcm(28,23),33));
32 sum-=d;
33 if (sum<=0) sum+=lcm(lcm(28,23),33);
34 printf("Case %d: the next triple peak occurs in %d days.\n",times,sum);
35 }
36 return 0;
37 }

由于题目中已知条件有点多。 可以对代码进行优化。
Code2:

1 #include<stdio.h>
2 int main()
3 {
4 int p,e,i,d,times=0;
5 while (scanf("%d %d %d %d",&p,&e,&i,&d))
6 {
7 times++;
8 if (p==-1) break;
9 int sum=(1288*i+14421*e+5544*p)%21252-d;
10 if (sum<=0) sum+=21252;
11 printf("Case %d: the next triple peak occurs in %d days.\n",times,sum);
12 }
13 return 0;
14 }

Biorhythms(中国剩余定理)的更多相关文章
- POJ 1006 - Biorhythms (中国剩余定理)
B - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...
- POJ 1006 Biorhythms --中国剩余定理(互质的)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103539 Accepted: 32012 Des ...
- POJ 1006 Biorhythms(中国剩余定理)
题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...
- PKU POJ 1006 Biorhythms (中国剩余定理)
中国剩余定理 x = ai (mod mi) ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk 其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st M ...
- poj1006 / hdu1370 Biorhythms (中国剩余定理)
Biorhythms 题意:读入p,e,i,d 4个整数,已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i ,求n . (题在文末) 知识点:中国剩余定理 ...
- POJ1006——Biorhythms(中国剩余定理)
Biorhythms Description人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色. ...
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110991 Accepted: 34541 Des ...
- Biorhythms(中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127339 Accepted: 40342 Des ...
- Biorhythms(poj1006+中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 117973 Accepted: 37026 Des ...
随机推荐
- linux下删除大量小文件
当目录下文件太多时,用rm删除文件会报错:-bash: /bin/rm: Argument list too long提示文件数目太多.解决的办法是使用如下命令:ls | xargs -n 10 rm ...
- 一、K3 Cloud 开发插件《K3 Cloud开发手册》
1.K3 Cloud打开 提示错误:未能加载文件或程序集“XXXXXXXXX”或它的某一个依赖项 原因:未正常关机导致的Temporary ASP.NET Files文件夹下的文件损坏 解决:删除C: ...
- laravel 查看sql
方法一: 我们有时候想测试一段代码生产的 SQL 语句,比如: 我们想看 App\User::all(); 产生的 SQL 语句,我们简单在 routes.php 做个实验即可: //app/Http ...
- 使用zsh 替换 bash
摘自:http://macshuo.com/?p=676#wechat_redirect Shell是Linux/Unix的一个外壳,你理解成衣服也行.它负责外界与Linux内核的交互,接收用户或其他 ...
- Docker 容器管理:rancher
Rancher:https://www.cnrancher.com/ 是一个开源的企业级全栈化容器部署及管理平台. 定位上和 K8s 比较接近,都是通过 web 界面赋予完全的 docker 服务编排 ...
- 分享10款效果惊艳的HTML5图片特效【转】
先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...
- numpy中的reshape中参数为-1
上篇文章中的reshape(-1,2),有的时候不明白为什么会有参数-1,可以通过查找文档中的reshape()去理解这个问题 根据Numpy文档(https://docs.scipy.org/doc ...
- 如何通过python代码解压zip包
转载至https://www.cnblogs.com/flyhigh1860/p/3884842.html 很多人在Google上不停的找合适自己的压缩,殊不知Py的压缩很不错.可以试试.当然C#,J ...
- binlog介绍
1.什么是binlog binlog是一个二进制格式的文件,用于记录用户对数据库更新的SQL语句信息,例如更改数据库表和更改内容的SQL语句都会记录到binlog里,但是对库表等内容的查询不会记录. ...
- SqlServer数据库查询表信息/列信息(列ID/列名/数据类型/长度/精度/是否可以为null/默认值/是否自增/是否是主键/列描述)
查询表信息(表名/表描述) Value ) AS value FROM sysobjects a Where a.xtype = 'U' AND a.name <> 'sysdiagram ...