C Primer Plus 第6章 C控制语句:循环 编程练习

来源:互联网 发布:上海知楚 旻泉 编辑:程序博客网 时间:2024/06/05 03:12

记录下写的最后几题。

14.#include <stdio.h>int main() {    double value[8];    double value2[8];    int index;    for (index = 0; index < 8; ++index) { //输入值。        printf("value #%d:", index + 1);        scanf("%lf", &value[index]);    }    for (index = 0; index < 8; index++) {        printf("%10.2lf", value[index]);    }    printf("\n");    value2[0] = value[0];    for (index = 1; index < 8; ++index) {        value2[index] = value2[index - 1] + value[index];    }    for (index = 0; index < 8; index++) {        printf("%10.2lf", value2[index]);    }    return 0;}
15.#include <stdio.h>int main() {    char symbol[255], ch;    int i = 0;    do {        scanf("%c", &ch);        symbol[i++] = ch;    } while (ch != '\n');    while (i >= 0) {        printf("%c", symbol[i - 2]);        // -2是因为减去\n和最后多出来的一个没用的i,大概就是这个意思吧。        i--;    }    return 0;}
16.#include <stdio.h>#define PRINCIPAL 100.0#define RATE_DAPHNE 0.1#define RATE_DEIRDRE 0.05// 给出本金,利率。int main() {    double Daphne = PRINCIPAL;    double Deirdre = PRINCIPAL;    int years = 0;    while (Daphne >= Deirdre) {        Daphne = Daphne + RATE_DAPHNE * PRINCIPAL;        Deirdre = Deirdre + Deirdre * RATE_DEIRDRE;        years++;    }    printf("Daphone:$%.2lf \nDeirdre:$%.2lf \n", Daphne, Deirdre);    printf("Investment values after %d years.", years);    return 0;}
17.#include <stdio.h>#define RATE 0.08// 给出利率。int main() {    double principal = 100.0;    int years = 0;    while (principal > 0) {        principal = principal + principal * RATE - 10;        years++;    }    printf("It takes %d years to complete.", years);    return 0;}
18.#include <stdio.h>#define DUNBARS_NUMBER 150int main() {    int friend = 5;    int value = 1;    int week = 1;    while (DUNBARS_NUMBER > friend) {        friend = (friend - value) * 2;        value++;        printf("Rabnud have %3d friends on the %2d of the week. \n",               friend, week);        week++;    }    return 0;}
0 0