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;
    }
  • 相关阅读:
    cd 好吃的 收藏
    2011 无代码无意义…test 指针 v1
    转 云中漫步的 电子书 from simon
    2011无代码无意义 test_gets_scanf连用 等
    svn—relocate 的原因
    转 CString,string,char*的综合比较
    2011 无代码无意义 test_内存之 变量的边界 (图)
    转 解决"应用程序配置不正确,程序无法启动"
    转 删除已存在的SVN账户信息
    C#中IO类FileInfo和Directory操作实例
  • 原文地址:https://www.cnblogs.com/cccj/p/7667962.html
Copyright © 2011-2022 走看看