public class Infection { static int N = 13; static int men = 1; public static void main(String[] args) { infection(N); System.out.println("第" + N + "天共有" + men + "个人被感染!"); System.out.println("************************************");
} public static void infection(int N){ int day =1; if(N <= 5){ return; }else if(N>5&&N<10){ for(day = 6;day <= N;day++){ men = men + 3; } }else{ for(day = 6;day <= 10;day++){ men = men + 3; infection(N-day+1); infection(N-day+1); infection(N-day+1); } men--; } } }
很简单的逻辑 int day = ...; day < 6 0 6<day<11 3的(day-6)次方 day >= 11 3的(day-6)次方 - 3的(day-10)次方
static public int s = 0; static void Main(string[] args) { while (1==1) { int n = Convert.ToInt32(Console.ReadLine()); Console.Write(f(n)); Console.ReadLine(); } } static public int f(int n) { if (n == 1) { s = 1; } else if (n > 1 || n < 5) { s = f(n - 1) * 3 + f(n - 1); } else if (n > 5) { s = f(n - 1) * 3 + f(n - 1) - f(n - 4); } return s; }
public class Infection { static int N = 13; static int men = 1; public static void main(String[] args) { infection(N); System.out.println("第" + N + "天共有" + men + "个人被感染!"); System.out.println("************************************");
} public static void infection(int N){ int day =1; if(N <= 5){ return; }else if(N>5&&N<10){ for(day = 6;day <= N;day++){ men = men + 3; } }else{ for(day = 6;day <= 10;day++){ men = men + 3; infection(N-day+1); infection(N-day+1); infection(N-day+1); } men--; } } }
我想沿着这个思路写下来却总也写不对:谁能给看看?static public int f(int n) { if (n<=0) { s = 0; } else if (n>=1 &&n <6) { s = 1; } else if (n >=6 && n<11) { s = (n - 5) * 3 + f(n - 5); } else if(n==11||n>=15) { s = f(n-1)+ (f(n - 5)-f(n-10)) * 3 - f(n - 10); } else if(n>11&&n<15) { s = f(n - 1) + (f(n - 5) - f(n - 10)) * 3; } return s; }:
//解题的思路是把患者的患病程度分一个周期,从第一天患病到治愈总共10天也就是10个程度,分别为患病1天...患病n天.把每个程度的患者数量存放在一个数组里,然后求和 /*简单点分析,从第一天开始 :患病一天的患者有1人, 患病二天的有0人 ,患病三天的有0人,....第十天患病有0人; * 第二天 患病一天的患者有0人, 患病两天的有1人 患病三天的有0人 .......... * 第三天 患病一天的患者有0人, 患病两天的有0人 患病三天的有1人 ...... * *** 第六天 患病一天的有3人(之前那人开始传染) 患病两天的有0人 .......患病6天的有1人 ........ * 第七天 患病一天的有3人 患病两天的有3人 患病三天的有0人 ....患病6天的有0人 .患病7天的有1人 * 以此类推可以发现规律 m[0](患病一天的人数)= 3 * (m[9] + m[8] + m[7] + m[6] + m[5]); m[n] = m[n-1] ; */ int num = 0;//患病的人数 int day = 10;//天数随意定 也可以写段代码运行时输入 int[] m = new int[10];//此数组为传染病周期的没一天里患病的人数(传染病周期是从得病到治愈总共10天) m[0] = 1;//是第一天得病的人数为1 for (int i = 1; i < 10; i++) { m[i] = 0; } if ( 1 == day) { num = 1; return; } else { for (int i = 1; i < day; i++) { for (int j = 9; j > 0; j--) { m[j] = m[j - 1]; } m[0] = 3 * (m[9] + m[8] + m[7] + m[6] + m[5]); } } for (int i = 0; i < 10; i++) { num += m[i]; }
面向对象实现using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication1 { public static class World { public static int day; public static event EventHandler NewdayEvent; public static void NextDay() { day += 1; if (NewdayEvent != null) NewdayEvent(null,null); } } class Program { static void Main(string[] args) { int n = 20; People peo = new People(); for (int i = 0; i < n-1; i++) { World.NextDay(); } Console.WriteLine(People.Cnt.ToString()); Console.Read(); } } public class People { static int cnt=0; public static int Cnt { get{return cnt;} } int birthday; int num; public People() { birthday = World.day; World.NewdayEvent += Do; cnt += 1; num = cnt; Console.WriteLine("我是第"+num+"个人,我还在潜伏期"); } public void Die() { cnt -= 1; World.NewdayEvent -= Do; Console.WriteLine("我是第"+num+"个人,要死了"); } internal void Do(object sender,EventArgs e) { if (World.day - birthday <= 5) return; if (World.day - birthday == 10) Die(); else { Console.WriteLine("我是第" + num + "人,我要传染人了"); People newpeo1 = new People(); People newpeo2 = new People(); People newpeo3 = new People(); } } } }
class Program { static void Main(string[] args) { string s = ""; while (s != "k") { Console.WriteLine("输入天数:"); int days; while (!int.TryParse(Console.ReadLine(), out days)) ; Console.WriteLine("总共: {0}", Sum(days)); Console.WriteLine("退出请输入'k',继续请按任意键"); s = Console.ReadLine(); } } static private int Sum(int days) { int result = 1; for (int i = 1; i <= days; i++) { if (i < 6 || i >10) continue; else { result += (3 * Sum(days - i + 1)); } } return result; } }
public class Infection { static int N = 13; static int men = 1; public static void main(String[] args) { infection(N); System.out.println("第" + N + "天共有" + men + "个人被感染!"); System.out.println("************************************");
} public static void infection(int N){ int day =1; 12. if(N <= 5){ 13. return; 14. }else if(N>5&&N<10){ 15. for(day = 6;day <= N;day++){ 16. men = men + 3; 17. } 18. }else{ 19. for(day = 6;day <= 10;day++){ 20. men = men + 3; 21. infection(N-day+1); 22. infection(N-day+1); 23. infection(N-day+1); 24. } 25. men--; 26. } 27. } 28. }
public class Infection { static int N = 1; static int men = 1; public static void Main(String[] args) { while (true) { men = 1; string s = Console.ReadLine(); if (s == "q") break; N = int.Parse(s); infection(N); Console.WriteLine("第" + N + "天共有" + men + "个人被感染!"); Console.WriteLine("************************************"); } } //public static void infectionOld(int totalDay) //{ // int day; // if (totalDay <= 5) // { // return; // } // if (totalDay > 5 && totalDay <= 10) // { // for (day = 6; day <= totalDay; day++) // { // men = men + 3; // } // } // else // { // for (day = 6; day <= 10; day++) // { // men = men + 3; // infection(totalDay - day + 1); // infection(totalDay - day + 1); // infection(totalDay - day + 1); // } // men--; // } //} public static void infection(int totalDay) { int day; if (totalDay <= 5) return; for (day = 6; day <= totalDay; day++) { if (day > 10) { men--; break; }
men = men + 3; infection(totalDay - day + 1); infection(totalDay - day + 1); infection(totalDay - day + 1); } } }
public class Infection { static int N = 1; static int men = 1; public static void Main(String[] args) { while (true) { men = 1; string s = Console.ReadLine(); if (s == "q") break; N = int.Parse(s); infection(N); Console.WriteLine("第" + N + "天共有" + men + "个人被感染!"); Console.WriteLine("************************************"); } } public static void infection(int totalDay) { int day; if (totalDay <= 5) return; for (day = 6; day <= totalDay; day++) { if (day > 10) { men--; break; }
men = men + 3; infection(totalDay - day + 1); infection(totalDay - day + 1); infection(totalDay - day + 1); } } }
//将people第几天得病的数保存到list表中 List<int> ls = new List<int>(); ls.Add(1); int n = int.Parse(Console.ReadLine()); for (int i = 2; i <= n; i++) { List<int> lss = new List<int>(); foreach (int l in ls) { lss.Add(l); if (i-l>=5&&i-l<10)//传染给其他人 { lss.Add(i); lss.Add(i); lss.Add(i); } } ls = lss; } int count = 0; foreach (int l in ls)//得到第n天的病人数 { if (n-l<10) { count += 1; } } Console.WriteLine(count);
void main() { int d=Comsole.ReadLine(); Comsole.Witle(pe(d)); Comsole.Read(); }static int pe(int day) { int man=1; for(int today=1;today<day;today++) { if(today>5) { man+=3*man; } if(today>10) { man--; } } returm man; }我的是最短的(必须的)
To a714387602: 你是最短的,你也是最错误的。请注意,不是所有病人都会传染的,只有在发作期的病人每天才传染给3人。 康复的算法也错误,当天数大些的时候,怎么可能每天只康复一个病人呢
凑个热闹using System; using System.Numerics;namespace CSharpTest { class Program { public static void Main() { int n = 100, start = 5, end = 10, infection = 3; BigInteger[] counter = new BigInteger[n]; for (int i = 0; i < n; i++) { if (i < start) counter[i] = 1; else { counter[i] = counter[i - 1] + counter[i - start] * infection; if (i >= end) counter[i] -= counter[i - end]; } } Console.WriteLine(counter[n - 1]); Console.ReadKey(); } } }
private ArrayList FunA(int number, int hidlenght) //number天数 hidlenght潜伏期 { ArrayList al = new ArrayList(); //存储第一天的感染人数 int InfectCount = 1; //感染总数初始值 int InfectBound = 0; //感染初始范围 int r = hidlenght - 1; for (int i = 1; i <= number; i++) { al.Add(InfectCount += InfectBound);
if (i % 5 == 0) //每五天会爆发一次大规模感染,成几何增展 { InfectBound = Int32.Parse(al[r++].ToString()) * 3; } if (i % 10 == 0) //第十天治愈前十天感染的那批人 { InfectCount = Int32.Parse(al[i - 1].ToString()) - Int32.Parse(al[i - 1 - 8].ToString()); } } return al; }
return this_day; }}int main(int narg,char** argement) { int nday; cout<<"intput a day number!"<<endl; while (cin>>nday) { DayInfo day; day = GetCount(nday); cout<<"the people was seek is "<<day.nWeekCount+day.nPainetCount<<endl; cout<<"intput a day number!"<<endl; } return 0; }
第11天结果不是24的都不对。修正一下非递归解法: static void CountInfection(int N) { var span = 5; var list1 = new List<int>(N * N) { span }; //Explicit Infectors var list2 = new List<int>(3 * N * N); //Hidden Infectors for (int i = 0; i < N; i++) //Count from the second day { for (int m = list1.Count - 1; m >= 0; m--) { if (--list1[m] < 1) //ill { list1.RemoveAt(m); list2.Add(span + 1); } } for (int n = list2.Count - 1; n >= 0; n--) { if (--list2[n] < 1) list2.RemoveAt(n); //cured else if (list2[n] <= span) for (int k = 0; k < 3; k++) list1.Add(span); } Console.WriteLine(list1.Count + list2.Count); } } 还有递归解法: static int count, span = 5 , span2 = 10; static void incrusionCountInfection(int N) { if (N <= span) count++; else { int end; if (N > span2) end = span2; else { end = N; count++; } for (int i = span; i < end; i++) { for (int n = 0; n < 3; n++) incrusionCountInfection(N - i); } } }
static int N = 13;
static int men = 1;
public static void main(String[] args) {
infection(N);
System.out.println("第" + N + "天共有" + men + "个人被感染!");
System.out.println("************************************");
}
public static void infection(int N){
int day =1;
if(N <= 5){
return;
}else if(N>5&&N<10){
for(day = 6;day <= N;day++){
men = men + 3;
}
}else{
for(day = 6;day <= 10;day++){
men = men + 3;
infection(N-day+1);
infection(N-day+1);
infection(N-day+1);
}
men--;
}
}
}
引用 3 楼 qgqch2008 的回复:http://blog.csdn.net/n67628814/archive/2011/06/09/6533080.aspx
这题在百一搜就出来了
day < 6 0
6<day<11 3的(day-6)次方
day >= 11 3的(day-6)次方 - 3的(day-10)次方
static public int s = 0;
static void Main(string[] args)
{
while (1==1)
{
int n = Convert.ToInt32(Console.ReadLine());
Console.Write(f(n));
Console.ReadLine();
}
} static public int f(int n)
{
if (n == 1)
{
s = 1;
}
else if (n > 1 || n < 5)
{
s = f(n - 1) * 3 + f(n - 1);
}
else if (n > 5)
{
s = f(n - 1) * 3 + f(n - 1) - f(n - 4);
}
return s;
}
static int N = 13;
static int men = 1;
public static void main(String[] args) {
infection(N);
System.out.println("第" + N + "天共有" + men + "个人被感染!");
System.out.println("************************************");
}
public static void infection(int N){
int day =1;
if(N <= 5){
return;
}else if(N>5&&N<10){
for(day = 6;day <= N;day++){
men = men + 3;
}
}else{
for(day = 6;day <= 10;day++){
men = men + 3;
infection(N-day+1);
infection(N-day+1);
infection(N-day+1);
}
men--;
}
}
}
{
if (n<=0)
{
s = 0;
}
else if (n>=1 &&n <6)
{
s = 1;
}
else if (n >=6 && n<11)
{
s = (n - 5) * 3 + f(n - 5);
}
else if(n==11||n>=15)
{
s = f(n-1)+ (f(n - 5)-f(n-10)) * 3 - f(n - 10);
}
else if(n>11&&n<15)
{
s = f(n - 1) + (f(n - 5) - f(n - 10)) * 3;
}
return s;
}:
/*简单点分析,从第一天开始 :患病一天的患者有1人, 患病二天的有0人 ,患病三天的有0人,....第十天患病有0人;
* 第二天 患病一天的患者有0人, 患病两天的有1人 患病三天的有0人 ..........
* 第三天 患病一天的患者有0人, 患病两天的有0人 患病三天的有1人 ......
* ***
第六天 患病一天的有3人(之前那人开始传染) 患病两天的有0人 .......患病6天的有1人 ........
* 第七天 患病一天的有3人 患病两天的有3人 患病三天的有0人 ....患病6天的有0人 .患病7天的有1人
* 以此类推可以发现规律 m[0](患病一天的人数)= 3 * (m[9] + m[8] + m[7] + m[6] + m[5]); m[n] = m[n-1] ;
*/
int num = 0;//患病的人数
int day = 10;//天数随意定 也可以写段代码运行时输入
int[] m = new int[10];//此数组为传染病周期的没一天里患病的人数(传染病周期是从得病到治愈总共10天)
m[0] = 1;//是第一天得病的人数为1
for (int i = 1; i < 10; i++)
{
m[i] = 0;
}
if ( 1 == day)
{
num = 1;
return;
}
else
{
for (int i = 1; i < day; i++)
{
for (int j = 9; j > 0; j--)
{
m[j] = m[j - 1];
}
m[0] = 3 * (m[9] + m[8] + m[7] + m[6] + m[5]);
} }
for (int i = 0; i < 10; i++)
{
num += m[i];
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
public static class World
{
public static int day;
public static event EventHandler NewdayEvent;
public static void NextDay()
{
day += 1;
if (NewdayEvent != null)
NewdayEvent(null,null);
}
}
class Program
{
static void Main(string[] args)
{
int n = 20;
People peo = new People();
for (int i = 0; i < n-1; i++)
{
World.NextDay();
}
Console.WriteLine(People.Cnt.ToString());
Console.Read();
}
} public class People
{
static int cnt=0;
public static int Cnt
{
get{return cnt;}
}
int birthday;
int num;
public People()
{
birthday = World.day;
World.NewdayEvent += Do;
cnt += 1;
num = cnt;
Console.WriteLine("我是第"+num+"个人,我还在潜伏期");
}
public void Die()
{
cnt -= 1;
World.NewdayEvent -= Do;
Console.WriteLine("我是第"+num+"个人,要死了");
}
internal void Do(object sender,EventArgs e)
{
if (World.day - birthday <= 5)
return;
if (World.day - birthday == 10)
Die();
else
{
Console.WriteLine("我是第" + num + "人,我要传染人了");
People newpeo1 = new People();
People newpeo2 = new People();
People newpeo3 = new People();
}
} }
}
地球负责自转(自增一天),人负责出生,传染,死亡
出生后向自转事件注册动作,地球每天调用该动作
潜伏期内传染不做任何事情,潜伏期外可不断new 3个新的病人,
到10天时,病人死亡,向地球的自增事件取消传染动作,然后结束
该方法不递归,易理解
{
static void Main(string[] args)
{
string s = "";
while (s != "k")
{
Console.WriteLine("输入天数:");
int days;
while (!int.TryParse(Console.ReadLine(), out days)) ;
Console.WriteLine("总共: {0}", Sum(days));
Console.WriteLine("退出请输入'k',继续请按任意键");
s = Console.ReadLine();
}
} static private int Sum(int days)
{
int result = 1;
for (int i = 1; i <= days; i++)
{
if (i < 6 || i >10)
continue;
else
{
result += (3 * Sum(days - i + 1));
}
}
return result;
}
}
static int N = 13;
static int men = 1;
public static void main(String[] args) {
infection(N);
System.out.println("第" + N + "天共有" + men + "个人被感染!");
System.out.println("************************************");
}
public static void infection(int N){
int day =1;
12. if(N <= 5){
13. return;
14. }else if(N>5&&N<10){
15. for(day = 6;day <= N;day++){
16. men = men + 3;
17. }
18. }else{
19. for(day = 6;day <= 10;day++){
20. men = men + 3;
21. infection(N-day+1);
22. infection(N-day+1);
23. infection(N-day+1);
24. }
25. men--;
26. }
27. }
28. }
{
static int N = 1;
static int men = 1;
public static void Main(String[] args)
{
while (true)
{
men = 1;
string s = Console.ReadLine();
if (s == "q")
break; N = int.Parse(s);
infection(N);
Console.WriteLine("第" + N + "天共有" + men + "个人被感染!");
Console.WriteLine("************************************");
}
} //public static void infectionOld(int totalDay)
//{
// int day;
// if (totalDay <= 5)
// {
// return;
// } // if (totalDay > 5 && totalDay <= 10)
// {
// for (day = 6; day <= totalDay; day++)
// {
// men = men + 3;
// }
// }
// else
// {
// for (day = 6; day <= 10; day++)
// {
// men = men + 3;
// infection(totalDay - day + 1);
// infection(totalDay - day + 1);
// infection(totalDay - day + 1);
// } // men--;
// }
//} public static void infection(int totalDay)
{
int day;
if (totalDay <= 5)
return; for (day = 6; day <= totalDay; day++)
{
if (day > 10)
{
men--;
break;
}
men = men + 3;
infection(totalDay - day + 1);
infection(totalDay - day + 1);
infection(totalDay - day + 1);
}
}
}
public class Infection
{
static int N = 1;
static int men = 1;
public static void Main(String[] args)
{
while (true)
{
men = 1;
string s = Console.ReadLine();
if (s == "q")
break; N = int.Parse(s);
infection(N);
Console.WriteLine("第" + N + "天共有" + men + "个人被感染!");
Console.WriteLine("************************************");
}
} public static void infection(int totalDay)
{
int day;
if (totalDay <= 5)
return; for (day = 6; day <= totalDay; day++)
{
if (day > 10)
{
men--;
break;
}
men = men + 3;
infection(totalDay - day + 1);
infection(totalDay - day + 1);
infection(totalDay - day + 1);
}
}
}
你太NB啦C# code 你用java写 。犀利
http://www.cnblogs.com/JordanBlog/archive/2011/06/17/InfectionQuestion.html
List<int> ls = new List<int>();
ls.Add(1);
int n = int.Parse(Console.ReadLine());
for (int i = 2; i <= n; i++)
{
List<int> lss = new List<int>();
foreach (int l in ls)
{
lss.Add(l);
if (i-l>=5&&i-l<10)//传染给其他人
{
lss.Add(i);
lss.Add(i);
lss.Add(i);
}
}
ls = lss;
}
int count = 0;
foreach (int l in ls)//得到第n天的病人数
{
if (n-l<10)
{
count += 1;
}
}
Console.WriteLine(count);
void main()
{
int d=Comsole.ReadLine();
Comsole.Witle(pe(d));
Comsole.Read();
}static int pe(int day)
{
int man=1;
for(int today=1;today<day;today++)
{
if(today>5)
{
man+=3*man;
}
if(today>10)
{
man--;
}
}
returm man;
}我的是最短的(必须的)
你是最短的,你也是最错误的。请注意,不是所有病人都会传染的,只有在发作期的病人每天才传染给3人。
康复的算法也错误,当天数大些的时候,怎么可能每天只康复一个病人呢
using System.Numerics;namespace CSharpTest
{
class Program
{
public static void Main()
{
int n = 100, start = 5, end = 10, infection = 3;
BigInteger[] counter = new BigInteger[n]; for (int i = 0; i < n; i++)
{
if (i < start)
counter[i] = 1;
else
{
counter[i] = counter[i - 1] + counter[i - start] * infection;
if (i >= end)
counter[i] -= counter[i - end];
}
} Console.WriteLine(counter[n - 1]);
Console.ReadKey();
}
}
}
private ArrayList FunA(int number, int hidlenght) //number天数 hidlenght潜伏期
{
ArrayList al = new ArrayList(); //存储第一天的感染人数
int InfectCount = 1; //感染总数初始值
int InfectBound = 0; //感染初始范围
int r = hidlenght - 1;
for (int i = 1; i <= number; i++)
{
al.Add(InfectCount += InfectBound);
if (i % 5 == 0) //每五天会爆发一次大规模感染,成几何增展
{
InfectBound = Int32.Parse(al[r++].ToString()) * 3;
}
} return al;
}
private ArrayList FunA(int number, int hidlenght) //number天数 hidlenght潜伏期
{
ArrayList al = new ArrayList(); //存储第一天的感染人数
int InfectCount = 1; //感染总数初始值
int InfectBound = 0; //感染初始范围
int r = hidlenght - 1;
for (int i = 1; i <= number; i++)
{
al.Add(InfectCount += InfectBound);
if (i % 5 == 0) //每五天会爆发一次大规模感染,成几何增展
{
InfectBound = Int32.Parse(al[r++].ToString()) * 3;
} if (i % 10 == 0) //第十天治愈前十天感染的那批人
{
InfectCount = Int32.Parse(al[i - 1].ToString()) - Int32.Parse(al[i - 1 - 8].ToString());
} } return al;
}
public static int GetCount(int days) { int beforeDays = 5; // 潜伏期 if (days < beforeDays + 1) return 1; int dayMens = 3, // 每天传染人数 fineDays = 5, // 发作到治愈需要天数 startMens = 1, // 发作人数 waittingMens = 0; // 潜伏期人数 List<int> dailyMens = new List<int>(); for (int i = 1; i <= days - beforeDays; i ++) { //第一个患者治愈时间 if (i == fineDays + 1) startMens--; //因为每天传染N人, 所以同一天传染的人同一天治愈 if (i > beforeDays + fineDays) startMens -= dailyMens[i - (beforeDays + fineDays) - 1]; //达到发作时间 if( i > beforeDays) { startMens += dailyMens[i - beforeDays - 1]; //发作人数增加N人 waittingMens -= dailyMens[i - beforeDays - 1]; // 潜伏期人数减少N人 } // 当天感染人数 int currentDayMens = startMens * dayMens; waittingMens += currentDayMens; dailyMens.Add(currentDayMens); Console.WriteLine("第{0}天, 发作人数:{1}, 感染人数{2}", i + beforeDays, startMens, waittingMens); } return startMens + waittingMens; }
struct DayInfo
{
int nWeekCount;
int nPainetCount;
int nPainet[5];
int nWeek[5];
void operator =(DayInfo day1)
{
nWeekCount = day1.nWeekCount;
nPainetCount = day1.nPainetCount;
for (int i=0;i<5;++i)
{
nPainet[i]=day1.nPainet[i];
nWeek[i]=day1.nWeek[i];
} }
};DayInfo GetCount(int day)
{
if(day<6)
{
DayInfo daydata;
for(int i=0;i<5;++i)
{
daydata.nPainet[i] = 0;
daydata.nWeek[i] = 0;
}
daydata.nWeek[day-1]=1;
daydata.nWeekCount=1;
daydata.nPainetCount=0;
return daydata;
}
else
{
DayInfo daydata_lastday = GetCount(day -1);
DayInfo this_day;
for (int i=0;i<5;i++)
{
if(i==0)
{
this_day.nPainetCount=0;
this_day.nPainet[i]=daydata_lastday.nWeek[4];
}
else
{
this_day.nPainet[i]=daydata_lastday.nPainet[i-1];
}
this_day.nPainetCount += this_day.nPainet[i];
}
for (int j =0;j<5;j++)
{
if (j==0)
{
this_day.nWeekCount = 0;
this_day.nWeek[j]=this_day.nPainetCount*3;
}
else
{
this_day.nWeek[j]=daydata_lastday.nWeek[j-1];
}
this_day.nWeekCount+=this_day.nWeek[j];
}
return this_day;
}}int main(int narg,char** argement)
{
int nday;
cout<<"intput a day number!"<<endl;
while (cin>>nday)
{
DayInfo day;
day = GetCount(nday);
cout<<"the people was seek is "<<day.nWeekCount+day.nPainetCount<<endl;
cout<<"intput a day number!"<<endl;
}
return 0;
}
if ($n -le 5) {
return 1;
} elseif ($n -le 10) {
return (1 + 3 * ($n - 5))
} else {
$p1 = getit($n - 1)
$p5 = getit($n - 5)
$p10 = getit($n - 10)
return ($p1 - $p10 + ($p5 - $p10) * 3)
}
}PS C:\Windows\system32> getnum 10
16
PS C:\Windows\system32> getnum 11
24
PS C:\Windows\system32> getnum 12
42
PS C:\Windows\system32> getnum 13
69
PS C:\Windows\system32> getnum 14
105
PS C:\Windows\system32> getnum 15
150
PS C:\Windows\system32> getnum 16
210
if ($n -le 5) {
# Day 1 ~ 5
return 1;
} elseif ($n -le 10) {
# Day 6 ~ 10
return (1 + 3 * ($n - 5))
} else {
# Day n > 10
$p1 = getit($n - 1)
$p5 = getit($n - 5)
$p10 = getit($n - 10)
return ($p1 - $p10 + ($p5 - $p10) * 3)
}
}PS C:\Windows\system32> getit 60
9920363826
{
var span = 5; var list1 = new List<int>(N*N) { span }; var list2 = new List<int>(3*N * N); Action infect = () =>
{
list1.Add(span);
list1.Add(span);
list1.Add(span);
}; for (int i = 0; i < N; i++) //Count from the second day
{
for (int m = list1.Count - 1; m >= 0; m--)
{
if (list1[m] < 1) //Blust
{
list1.RemoveAt(m);
list2.Add(span + 1);
}
else list1[m] = list1[m] - 1;
} for (int n = list2.Count - 1; n >= 0; n--)
{
if (list2[n] < 1) list2.RemoveAt(n);
else
{
infect();
list2[n] = list2[n] - 1;
}
} Console.WriteLine(list1.Count + list2.Count);
}
}
{
if ( n < 6) //输入有效性验证暂不考虑,即天数应为整数且大于0
{
return 1;
}
else if (n >= 6 && n < 11)
{
return f(n - 5) * 3 + f(n -1);
}
else if (n == 11)
{ return ((f(n - 5) - f(n - 10)) * 3 + f(n - 1) - f(n - 10)); }
else
{
return ((f(n - 5) - f(n - 10)) * 3 + f(n - 1));
}
}
20天输出结果:
天数 患者
1 1
2 1
3 1
4 1
5 1
6 4
7 7
8 10
9 13
10 16
11 24
12 42
13 69
14 105
15 150
16 210
17 315
18 492
19 768
20 1170
76楼解法附有输出结果图 http://user.qzone.qq.com/116361/blog/1308389155
#include <iostream>
#include <vector>
using namespace std;class patient
{
private:
enum {LATENT, ATTACK, RECOVERY}; //1 潜伏,2 发作, 3 痊愈。
private:
int state;
int days;
public:
patient() {state = LATENT; days = 0;}
patient(const patient &pt);
void count_days(void);
bool isrecovery(void);
bool isattack(void);
};patient::patient(const patient &pt)
{
state = pt.state;
days = pt.days;
}void patient::count_days(void)
{
days++;
if(days > 5)
{
if(state == LATENT)
state = ATTACK;
else if(state == ATTACK)
state = RECOVERY; days = 0;
}
}bool patient::isrecovery(void)
{
return (state == RECOVERY) ? true : false;
}bool patient::isattack(void)
{
return (state == ATTACK) ? true : false;
}int main()
{
cout << "某种传染病第一天只有一个患者,前五天为潜伏期,不发作也不会传染人,第6天开始发作,从发作到治愈需要5天时间,期间每天传染3个人。求第N天共有多少患者?" << endl; vector<patient> pts;
pts.reserve(10000);
while(1)
{
int n;
cout << "请输入N (0退出):" << endl;
cin >> n;
if(n == 0)
break; pts.push_back(patient()); for(int i = 0; i < n; i++)
{
vector<patient>::iterator it = pts.begin();
for(; it != pts.end(); it++)
{
it->count_days();
if(it->isrecovery())
pts.erase(it);
if(it->isattack())
{
pts.push_back(patient());
pts.push_back(patient());
pts.push_back(patient());
}
}
} vector<patient>::iterator it = pts.begin();
for(n = 0; it != pts.end(); it++)
if(!it->isrecovery())
n++; cout << n << endl;
pts.clear();
} return 0;
}
---------------------
1 1
2 1
3 1
4 1
5 1
6 4
10 16
11 28
12 45
18 566
19 870
20 1299
{
var span = 5;
var list1 = new List<int>(N * N) { span }; //Explicit Infectors
var list2 = new List<int>(3 * N * N); //Hidden Infectors for (int i = 0; i < N; i++) //Count from the second day
{
for (int m = list1.Count - 1; m >= 0; m--)
{
if (--list1[m] < 1) //ill
{
list1.RemoveAt(m);
list2.Add(span + 1);
}
} for (int n = list2.Count - 1; n >= 0; n--)
{
if (--list2[n] < 1) list2.RemoveAt(n); //cured
else if (list2[n] <= span)
for (int k = 0; k < 3; k++) list1.Add(span);
} Console.WriteLine(list1.Count + list2.Count);
}
}
还有递归解法:
static int count, span = 5 , span2 = 10;
static void incrusionCountInfection(int N)
{
if (N <= span) count++;
else
{
int end;
if (N > span2)
end = span2;
else
{
end = N;
count++;
} for (int i = span; i < end; i++)
{
for (int n = 0; n < 3; n++) incrusionCountInfection(N - i);
}
}
}