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:

 #include<stdio.h>
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
int lcm(int a,int b)
{
return a/gcd(a,b)*b;
}
int main()
{
int p,e,i,d;
int times=;
while (cin>>p>>e>>i>>d)
{
times++;
if (p==-) break;
int x1=lcm(,);
int x2=lcm(,);
int x3=lcm(,);
int k1=,k2=,k3=;
while ((k1*x1)%!=)
k1++;
while ((k2*x2)%!=)
k2++;
while ((k3*x3)%!=)
k3++;
int sum=k1*x1*i+k2*x2*e+k3*x3*p;
sum=sum%(lcm(lcm(,),));
sum-=d;
if (sum<=) sum+=lcm(lcm(,),);
printf("Case %d: the next triple peak occurs in %d days.\n",times,sum);
}
return ;
}

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

Code2:

 #include<stdio.h>
int main()
{
int p,e,i,d,times=;
while (scanf("%d %d %d %d",&p,&e,&i,&d))
{
times++;
if (p==-) break;
int sum=(*i+*e+*p)%-d;
if (sum<=) sum+=;
printf("Case %d: the next triple peak occurs in %d days.\n",times,sum);
}
return ;
}

POJ1006——Biorhythms(中国剩余定理)的更多相关文章

  1. Biorhythms(中国剩余定理)

    http://shuxueshi.jie.blog.163.com/blog/static/13611628820104179856631/ 这篇博客写的很棒! #include<stdio.h ...

  2. POJ 1006 - Biorhythms (中国剩余定理)

    B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  3. POJ 1006 Biorhythms --中国剩余定理(互质的)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103539   Accepted: 32012 Des ...

  4. POJ 1006 Biorhythms(中国剩余定理)

    题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...

  5. PKU POJ 1006 Biorhythms (中国剩余定理)

    中国剩余定理 x = ai (mod mi)  ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk     其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st   M ...

  6. poj1006 / hdu1370 Biorhythms (中国剩余定理)

    Biorhythms 题意:读入p,e,i,d 4个整数,已知(n+d)%23=p;   (n+d)%28=e;   (n+d)%33=i ,求n .        (题在文末) 知识点:中国剩余定理 ...

  7. poj1006 中国剩余定理&&中国剩余定理解析

    poj 1006 题的思路不是很难的,可以转化数学式: 现设 num 是下一个相同日子距离开始的天数 p,e,i,d 如题中所设! 那么就可以得到三个式子:( num + d ) % 23 == p: ...

  8. 【中国剩余定理】 poj 1006

    生理周期  简单模拟 对于超出23 * 28 * 33(21252)时进行求余运算即可. #include<stdio.h> int main() { //freopen("in ...

  9. Biorhythms(poj1006+中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 117973   Accepted: 37026 Des ...

随机推荐

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFacto

    控制台: 2016-4-1 16:32:06 org.hibernate.annotations.common.Version <clinit> 信息: Hibernate Commons ...

  2. 如何让input number类型的标签不产生上下加减的按钮

    之前用 input type="number" 来放数字框,发现有个上下加减的东西,感觉不太好 这个容易出现0 然后减为负数 这种情况下怎么去掉右边的那个上下加减的小按钮呢?前端同 ...

  3. Python 数据类型:字符串

    一.字符串介绍 字符串是由单引号/双引号/三引号引起来的,由字母 .数字或符号等构成的一串字符 In [1]: name = "Tom" # 定义字符串 In [2]: type( ...

  4. Swift/Objective-C-Swift与Objective-C混用教程

    简介:我想很多iOS开发者在知道Swift后,心中最大的问题就是如何将Swift应用到原有项目之中.下面我将简要介绍这2种语言的混用方法,内容参考自官方文档 Using Swift with Coco ...

  5. ASP.NET 4.0尚未在Web服务器注册 解决

    http://www.cnblogs.com/lvxiouzi/p/3511446.html 安装asp.net 4.0.30319.0版本 命令: %windir%\Microsoft.NET\Fr ...

  6. Java虚拟机原理图解

    具体详情参考: http://blog.csdn.net/luanlouis/article/details/41046443 http://blog.csdn.net/luanlouis/artic ...

  7. Android N 7 【 classes.dex】反编译失败:com.googlecode.d2j.DexException: not support version.

    Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. D:\Android反编译工具[全]\2016\d ...

  8. C语言学习链接

    C语言博客链接: http://www.cnblogs.com/ningvsban/category/585944.html

  9. weblogic新漏洞学习cve-2017-10271

    一.原理: 很明显啦,readobject又出来背锅了,一个XML的反序列化漏洞导致的命令执行. 具体原理我看不懂java代码的我也只能学习别人的分析.给出一篇参考文章,写的非常详细: 漏洞原理 二. ...

  10. NodeJS路由(server.js + router.js)

    手动路由... server.js 创建http服务器,解析pathname,传递给router处理. var http = require("http"); var url = ...