数组循环输入输出
#include <stdio.h>
int main(int argc, char *argv[]) {
int length;
printf("创建数组长度:");
scanf("%d", &length);
int arr[length];
for (int *i = arr; i <= arr + length - 1; ++i) {
printf("输入第%ld个:", i - arr + 1);
scanf("%d", i);
}
printf("开始输出");
for (int *i = arr; i <= arr + length - 1; ++i) {
printf("输出第%ld个:%d\n", i - arr + 1, *i);
}
return 0;
}
斐波那契数列数组版
#include <stdio.h>
int main(int argc, char *argv[]) {
int length;
printf("斐波那契数列长度:");
scanf("%d", &length);
int arr[length];
for (int i = 0; i < length; ++i) {
if (i == 0 || i == 1) {
arr[i] = 1;
} else {
arr[i] = arr[i - 2] + arr[i - 1];
}
}
for (int *i = arr; i <= arr + length - 1; ++i) {
printf("%5d", *i);
}
return 0;
}
n个西瓜,第一天卖一半多两个,以后每天卖剩下的一半多两个,问几天以后能卖完?
当西瓜个数为奇数时,卖一半为一半的整数,如当西瓜个数为5时,卖一半为卖2个。
#include <stdio.h>
int days = 1;
int sell_melons(int amount) {
if (amount > 5) {
days++;
return sell_melons(amount / 2 - 2);
} else if (amount == 5) {
return days + 1;
} else {
return days;
}
}
int main(int argc, char *argv[]) {
int amount;
scanf("%d", &amount);
printf("%d", sell_melons(amount));\
return 0;
}