第5章(第四版)C语言程序设计练习

一、例题

1. 1+2+3+···+100,即Σ100 n=1  n

#include<stdio.h>
int main()
{
    int i = 1;
    int sum = 0;
    while (i<=100)
    {
        sum = sum + i;
        i++;
    }
    printf("%d", sum);
    return 0;
}

2.do...while

#include<stdio.h>
int main()
{
    int i = 1;
    int sum = 0;
    do
    {
        sum = sum + i;
        i++;
    } while (i <= 100);
    printf("%d", sum);
    return 0;
}

3.

4.1000名学生,募捐达到10万元结束,统计捐款人数和数目

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define SUM 100000
int main()
{
    float amount,aver,sum;
    int i;
    for (i = 1,sum=0; i <= 1000; i++)
    {
        printf("请输入");
        scanf("%f", &amount);
        sum = sum + amount;
        if (sum >= SUM)
            break;
    }
    aver = sum / i;
    printf("aver=%f\nnum=%d", aver, i);
    return 0;
}

5.100~200之间,不能被3整除的数

#include<stdio.h>
int main()
{
    int i = 0;
    for (i = 100; i <= 200; i++)
    {
        if (0 == i % 3)
            continue;
        printf("%d ", i);
    }
    return 0;
}

6.输出4*5矩阵

1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20

#include<stdio.h>
int main()
{
    int i, j, n = 0;
    for (i = 1; i <= 4; i++)
        for (j = 1; j <= 5; j++,n++)
        {
            if (n % 5 == 0)
                printf("\n");
            printf("%d\t", i * j);
        }
    return 0;
}

7.∏/4 = 1- 1⁄3 +1⁄5 - 1⁄7 + ···

Π  ≈  22⁄7

Π ⁄6  ≈  1⁄1 + 1⁄4 + 1⁄9 ···+ 1⁄n*n

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main()
{
    int sign = 1, count = 0;
    double pi = 0;
    double n = 1;
    double term = 1;
    while (fabs(sign/n)>=1e-6)//1乘以10的负6次方
    {
        pi = pi + sign / n;
        n = n + 2;
        sign = -sign;
        count++;
    }
    pi = pi * 4;
    printf("pi=%lf\n", pi);
    printf("count=%d\n", count);
    return 0;
}

8.Fibonacci

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//通过公式计算,效率低
//造成大量重复计算
int count=0;
int Fib(int n)
{
    if (n == 3)
    {
        count++;
    }
    if (n <= 2)
        return 1;
    else
        return Fib(n - 1) + Fib(n - 2);
}
int main()
{
    int n = 0;
    int ret = 0;
    scanf("%d", &n);
    ret = Fib(n);
    printf("ret=%d\n", ret);
    printf("count=%d\n",count);
    return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//循环或迭代的方式
//倒着进行计算
int Fib(int n)
{
    int a = 1;
    int b = 1;
    int c = 1;
    while (n > 2)
    {
        c = a + b;
        //产生新的a、b,进行赋值
        a = b;
        b = c;
        n--;
    }
    return c;//n为1或2,这里执行,返回c=1
}
int main()
{
    int n = 0;
    int ret = 0;
    scanf("%d", &n);
    ret = Fib(n);
    printf("ret=%d\n", ret);
    //printf("count=%d\n",count);
    return 0;
}

9.判断素数

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    int n;
    int i;
    printf("请输入大于3的整数:");
    scanf("%d", &n);
    for (i = 2; i <= n-1; i++)
        if (n % i == 0)
            break;
        if (i < n)
            printf("是素数");
        else
            printf("不是素数");
    return 0;
}

10.100~200之间全部素数

#include<stdio.h>
#include<math.h>
int prime(int n)
{
    //2~n-1 之间的数字
    int j = 0;
    for (j = 2; j <=sqrt(n); j++)
    {
        if (n % j == 0)//即n被整除,不是素数
            return 0;//返回0
    }
    return 1;//返回整数
}
int main()
{
  //100-200
    int i = 0;
    int count=0;
    for (i = 100; i <= 200; i++)
    {
      //判断i是否为素数
        if (prime(i) == 1)
        {
            count++ ;
            printf(" %d",  i );
          }
    }
    printf(" \ncount = %d\n",count);
    return 0;
}

11.电文变成密码: 将字母A变为E、将字母a变为e,即变成其后的第四个字母,W将变成A。字母按上述规律转换,非字母字符不变。输入一行字符,输出相应密码

#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//void main()
//{
//    char c;
//    while ((c = getchar()) != '\n')
//    {
//        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
//        {
//            c = c + 4;
//            if (c > 'Z' && c <= 'Z' + 4 || c > 'z')
//                c = c - 26;
//        }
//        printf("%c", c);
//    }
//    printf("\n");
//}
#include<stdio.h>
int main()
{
    char ch;
    while ((ch = getchar()) != '\n')
    {
        if ((ch >= 'A' && ch <= 'V') || (ch >= 'a' && ch <= 'v'))
        {
            ch = ch + 4;
            
        }
        if (ch >= 'W' && ch <= 'Z')
        {
            ch = ch - 26;
        }
                
        printf("%c",ch);
    }
    printf("\n");
    return 0;
}

二、习题

3.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
    int  p, r, n, m, tmp;
    printf("请输入两个正整数n,m:");
    scanf("%d,%d,", &n, &m);//5,3
    if (n < m)
    {
        tmp = n;
        n = m;
        m = tmp;
    }
    p = n * m;//15
    while (m != 0)
    {
        r = n % m;//5%3=2;3%2=1;2%1=0
        n = m;//3;2;1
        m = r;//2;1;0
    }
    printf("它们的最大公约数为:%d\n", n);//1
    printf("它们的最小公倍数为:%d\n", p / n);//15/1
    return 0;
}

4.

#include <stdio.h>
int main()
{
    char c;
    int letters = 0, space = 0, digit = 0, other = 0;
    printf("请输入一行字符:\n");
    while ((c = getchar()) != '\n')
    {
        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
            letters++;
        else if (c == ' ')
            space++;
        else if (c >= '0' && c <= '9')
            digit++;
        else
            other++;
    }
    printf("字母数:%d\n空格数:%d\n数字数:%d\n其它字符数:%d\n", letters, space, digit, other);
    return 0;
}

5.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
    int  a, n, i = 1, sn = 0, tn = 0;
    printf("a,n=:");
    scanf("%d,%d", &a, &n);//1,2
    while (i <= n)//1<2;2=2
    {
        tn = tn + a; //0+1;1+10=11
        sn = sn + tn; //0+1;11+1=12
        a = a * 10;//10;100
        ++i;//2;3
    }
    printf("a+aa+aaa+...=%d\n", sn);
    return 0;
}

 6.

#include <stdio.h>
int main()
{
    long long n = 1, sum = 0;
    int i = 1;
    for (i = 1; i <= 20; i++)
    {
        n *= i;
        sum += n;
    }
    printf("%I64d", sum);//2561327494111820313
    //printf("%lld", sum);
}

7.

#include <stdio.h>
int main()
{
    int n1 = 100, n2 = 50, n3 = 10;
    double k, s1 = 0, s2 = 0, s3 = 0;
    for (k = 1; k <= n1; k++) 
    {
        s1 = s1 + k;
    }
    for (k = 1; k <= n2; k++) 
    {
        s2 = s2 + k * k;
    }
    for (k = 1; k <= n3; k++) 
    {
        s3 = s3 + 1 / k;
    }
    printf("%f\n", s1);
    printf("%f\n", s2);
    printf("%f\n", s3);
    printf("sum=%15.6f\n", s1 + s2 + s3);
    return 0;
}

8.

#include <stdio.h>
int main()
{
    int i, j, k, n;
    for (n = 100; n < 1000; n++)
    {
        //153
        i = n / 100;//1
        j = n / 10 - i * 10;//15-10=5
        k = n % 10;//153%10=3
        if (n == i * i * i + j * j * j + k * k * k)
            printf("%d ", n);
    }
    printf("\n");
    return 0;
}

9.

#include <stdio.h>
int main()
{
    int m, s, i;
    for (m = 2; m < 1000; m++)
    {
        //m=6
        s = 0;
        for (i = 1; i < m; i++)//1;2;3
            if ((m % i) == 0) //0;0;0
                s = s + i;//1;3;6
        if (s == m)
        {
            printf("%d,its factors are ", m);
            for (i = 1; i < m; i++)//1;2;3
                if (m % i == 0)  printf("%d ", i);
            printf("\n");
        }
    }
    return 0;
}

10.

#include <stdio.h> 
int main()
{
    int i, n = 20;
    double a = 2, b = 1, s = 0, t;
    for (i = 1; i <= n; i++)
    {
        s = s + a / b;//2/1;2/1+3/2
        t = a;//2;3;
        a = a + b;//3;5
        b = t;//2;3
    }
    printf("sum=%16.10f\n", s);
    return 0;
}

11

#include <stdio.h> 
int main()
{
    double sn = 100, hn = sn / 2;
    int n;
    for (n = 2; n <= 10; n++)
    {
        sn = sn + 2 * hn;   
        hn = hn / 2;     
    }
    printf("第10次落地时共经过%f米\n", sn);
    printf("第10次反弹%f米\n", hn);
    return 0;
}

12

#include <stdio.h> 
int main()
{
    int day, x1, x2;//x1为第一天桃子个数
    day = 9;
    x2 = 1;
    while (day > 0)
    {
        x1 = (x2 + 1) * 2;    
        x2 = x1;
        day--;
    }
    printf("total=%d\n", x1);//1534
    return 0;
}

13

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
int main()
{
    float a, x0, x1;
    printf("enter a positive number:");
    scanf("%f", &a);
    x0 = a / 2;
    x1 = (x0 + a / x0) / 2;
    do
    {
        x0 = x1;
        x1 = (x0 + a / x0) / 2;
    } while (fabs(x0 - x1) >= 1e-5);
    printf("The square root of %5.2f  is %8.5f\n", a, x1);
    return 0;
}

14

#include <stdio.h>
#include <math.h>
int  main()
{
    double x1, x0, f, f1;
    x1 = 1.5;
    do
    {
        x0 = x1;
        f = ((2 * x0 - 4) * x0 + 3) * x0 - 6;
        f1 = (6 * x0 - 8) * x0 + 3;
        x1 = x0 - f / f1;
    } while (fabs(x1 - x0) >= 1e-5);
    printf("The root of equation is %5.2f\n", x1);
    return 0;
}

15

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
int main()
{
    float x0, x1, x2, fx0, fx1, fx2;
    do
    {
        printf("enter x1 & x2:");
        scanf("%f,%f", &x1, &x2);
        fx1 = x1 * ((2 * x1 - 4) * x1 + 3) - 6;
        fx2 = x2 * ((2 * x2 - 4) * x2 + 3) - 6;
    } while (fx1 * fx2 > 0);
    do
    {
        x0 = (x1 + x2) / 2;
        fx0 = x0 * ((2 * x0 - 4) * x0 + 3) - 6;
        if ((fx0 * fx1) < 0)
        {
            x2 = x0;
            fx2 = fx0;
        }
        else
        {
            x1 = x0;
            fx1 = fx0;
        }
    } while (fabs(fx0) >= 1e-5);
    printf("x=%6.2f\n", x0);
    return 0;
}

16

#include <stdio.h> 
int main()
{
    int i, j, k;
    for (i = 0; i <= 3; i++)
    {
        for (j = 0; j <= 2 - i; j++)
            printf(" ");
        for (k = 0; k <= 2 * i; k++)
            printf("*");
        printf("\n");
    }
    for (i = 0; i <= 2; i++)
    {
        for (j = 0; j <= i; j++)
            printf(" ");
        for (k = 0; k <= 4 - 2 * i; k++)
            printf("*");
        printf("\n");
    }
    return 0;
}

 

17

#include <stdio.h>  
int main()
{
    char i, j, k;            
    for (i = 'x'; i <= 'z'; i++)
        for (j = 'x'; j <= 'z'; j++)
            if (i != j)
                for (k = 'x'; k <= 'z'; k++)
                    if (i != k && j != k)
                        if (i != 'x' && k != 'x' && k != 'z')
                            printf("A--%c\nB--%c\nC--%c\n", i, j, k);//A--z;B--x;C--y
    return 0;

}

 

posted @ 2021-11-20 19:04  mljrm  阅读(206)  评论(0)    收藏  举报