zoukankan      html  css  js  c++  java
  • C Primer Plus 第6章 C控制语句:循环 编程练习

    记录下写的最后几题。

    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("
    ");
    
        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 != '
    ');
    
        while (i >= 0) {
            printf("%c", symbol[i - 2]);
            // -2是因为减去
    和最后多出来的一个没用的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 
    Deirdre:$%.2lf 
    ", 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 150
    
    int 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. 
    ",
                   friend, week);
            week++;
        }
        return 0;
    }
  • 相关阅读:
    Hibernate官方文档翻译-(第二章,入门)
    关于我的Blog
    退役记
    考场上要写的emacs
    关于在linux下将caps lock键改成ctrl的方法
    SXOI2020退役记
    min_25筛学习笔记
    带花树学习笔记
    [CF438E] 小朋友和二叉树
    多项式中步
  • 原文地址:https://www.cnblogs.com/cccj/p/7667962.html
Copyright © 2011-2022 走看看