5.2 细菌繁殖



代码如下:

/* * *Declaration:The author of <<Accelerated C++>> has wrote in the end of that book: As you look for reading materimal, keep in mind that books on the shelf do not make you a better programmer. Ultimately, the only way to improve your programming is to write programs. >这些程序来自一些ACM书籍,作者只为提高编程能力,实现书中例题或练习。如有侵权,请联系作者,作者将立即删除。 * *联系邮箱:mingxinglai#gmail.com * */ #include <stdio.h> int main(int argc, char* argv[]) { int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int n; scanf("%d", &n); for( int i = 0; i < n; i++) { int month_1, day_1, month_2, day_2, num; scanf("%d%d%d%d%d", &month_1, &day_1, &num, &month_2, &day_2); int sum = 0; for( int k = month_1; k < month_2; k++) sum += days[ k - 1 ]; sum -= day_1; sum += day_2; int nNum = num; for(int k = 0; k < sum; k++) nNum *= 2; printf("%d\n", nNum); } return 0; }


示例2:(较上面的代码更为简洁明了)

#include <stdio.h> int month[]= {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main(int argc, char* argv[]) { int times; scanf("%d", ×); int mon1, date1, mon2, date2, num; while( times-- ) { scanf("%d%d%d%d%d", &mon1, &date1, &num, &mon2, &date2); int days = date2 - date1; for( int i = mon1; i < mon2; i++) days += month[i]; long nNum = num; for( int j = 0; j < days; j++) num *= 2; printf("%d\n", num); } return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值