- 论坛徽章:
- 0
|
- /*
- ============================================================================
- Name : hare.c
- Author : Miao
- Version :
- Copyright : Your copyright notice
- Description : Hello World in C, Ansi-style
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- //输入月数
- int input_month();
- //兔子总数
- int allHare(int new_hare, int young_hare, int hare);
- //计算一个月的兔子变化
- void computerHare(int *new_hare, int *young_hare, int *hare);
- //main函数
- int main(void) {
- //设定初始成年兔子、年轻兔子、新生兔子
- int hare = 0, young_hare = 0, new_hare = 0;
- //月数
- int month = input_month();
- for (int i = 1;i<=month;i++) {
- computerHare(&new_hare, &young_hare, &hare);
- printf("第%d个月,目前共有兔子%d对\n", i, allHare(new_hare, young_hare, hare));
- }
- return EXIT_SUCCESS;
- }
- int input_month() {
- int month = 0;
- for(int result = 0; result == 0;) {
- fflush(stdin);
- printf("%s", "请问要统计多少个月后兔子的数量?");
- result = scanf("%d", &month);
- }
- return month;
- }
- int allHare(int new_hare, int young_hare, int hare) {
- return new_hare + young_hare + hare;
- }
- void computerHare(int *new_hare, int *young_hare, int *hare) {
- *hare += *young_hare;
- *young_hare = *new_hare;
- //如果目前没有兔子,则第一次购进1对兔子,反之则开始繁殖
- *new_hare = allHare(*hare, *young_hare, *new_hare) > 0 ? *hare : 1;
- }
复制代码 写完了。不过C语言也就是入门等级,可能写得不好。 |
|