有一颗树,一年两个生长周期,春天它长高一倍,夏天长高1m,问N个周期后树有多高?假设从春天开始树高为1m,第0个周期树高为1m。 要求:1、可以同时输入多个生长周期 如:表示下面有几个生长周期 0 1 4 输出结果为: 1 2 7 图例 单个需求: #include <stdio.h> int main() { int n; int high = 0; int i; printf("请输入一个周期值: \n"); scanf("%d", &n); for (i = 0; i <= n; i ++) { if (i%2 == 0) { high = high + 1; } else if(i%2 == 1) { high = high * 2; } } printf("第%d周期目前的高度为:%d米\n", n, high); return 0; } |
多个需求 #include <stdio.h>
int tree_high(int n) { int high = 0; int i; for (i = 0; i <= n; i ++) { if (i%2 == 0) { high = high + 1; } else if(i%2 == 1) { high = high * 2; } }
return high; }
int main() { printf("请输入多个周期: \n"); int a; scanf("%d", &a); int p[10]; for (int i = 0; i < a; i++) { scanf("%d", &p); } for (int i = 0; i < a; i++) { int high = tree_high(p); printf("%d\n", high); } } 求10! #include <stdio.h> int main() { int n = 10; int i = 2; float fac = 1; if(n == 0 || n == 1) { printf("factorial is 1\n"); return 0; } while(i <= n) { fac = fac * i; i++; } printf("%.2f\n", fac); }
|