细菌繁殖——C++实现

问题描述:
一种细菌的繁殖速度是每天成倍增长。例如:第一天有10个,第二天就变成20个,第三天变成40个,第四天变成80个,……。现在给出第一天的日期和细菌数目,要你写程序求出到某一天的细菌数目。

输入数据:
第一行有一个整数n,表示测试数据的数目。其后n行每行有5个整数,整数之间用一个空格隔开。第一个数表示第一天的月份,第二个数表示第一天的日期,第三个数表示第一天细菌的数目,第四个数表示要求的那一天的月份,第五个数表示要求的那一天的日期。已知第一天和要求的一天在同一年并且该年不是闰年,要求的一天一定在第一天之后。数据保证要求的一天的细菌数目在长整数(long)范围内。

输出要求:
对于每一组测试数据,输出一行,该行包含一个整数,为要求的一天的细菌数。

输入样例:
2
1 1 1 1 2
2 28 10 3 2
输出样例:
2
40
#include<iostream>
using namespace std;
int days(int m){
	int N;
	if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12)
		return N=31;
	else if(m==4 || m==6 || m==9 || m==11)
		return N=30;
	else
		return N=28; 
}
int main()
{
	int n;
	cin>>n;
	while(n--){
		long x;
		int m1,d1,m2,d2,sum=0;
		cin>>m1>>d1>>x>>m2>>d2;
		if(m1 == m2){
			sum=d2-d1;
			for(int i=0;i<sum;i++)
				x=x*2;
		}
		else{
			for(int i=m1; i<m2; i++)
				sum+=days(i); //相隔的月数的天数相加
			sum+=d2-d1;       //减去开始的天数
			for(int j=0;j<sum;j++)
				x=x*2;
		}
		cout<<x<<endl;
	}
	return 0;
}
发布了5 篇原创文章 · 获赞 0 · 访问量 84
展开阅读全文

细菌繁殖的一个推算的问题,怎么使用C语言的计算的方式怎么利用程序的代码编写来实现

06-20

Problem Description In the game "Gem Craft", we create gems in the item bar. There are N grids in the item bar, each grid can only accomodate one gem. Each gem has its level. When the level of a gem upgrade by 1, the power of the gem will be doubled. The key to win the game is to get a high level gem. Gems with level 0 are available directly. To get gems with higher level, the only way is to combine low level gems. Suppose we have 2*N gems with level K now, they can be turned into N gems with level (K+1) afert a combining operation. Your task is to get at least one gem with level M in minimum steps of operation. For one step of operation, you can do one of the following two things: (1) Get gems with level 0 directly, filling up the empty grids. (2) Combine even number of gems with same level. Input The input contain some test cases. Each test case contains two integers N and M (1<=N<=36, 0<=M<=20). The input ends when N=M=0, this test case doesn't need processing. Output For each test case, output one line. The line contains only one integer, the minimum steps of operation. If there is no solution to get a gem with level M, output -1 instead. Sample Input 4 3 8 3 0 0 Sample Output 9 4 Hint We use dots to present empty grid, use number to present the level of a gem. The first sample can be solved like this: **** Initial 0000 Get level 0 gems **11 Combine level 0 gems ***2 Combine level 1 gems 0002 Get level 0 gems *012 Combine level 0 gems 0012 Get level 0 gems *112 Combine level 0 gems **22 Combine level 1 gems ***3 Combine level 2 gems In total of 9 steps of operation. The second sample can be solved like this: ******** Initial 00000000 Get level 0 gems ****1111 Combine level 0 gems ******22 Combine level 1 gems *******3 Combine level 2 gems It only takes 4 steps of operation. 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览