C 练习实例83

C 语言教程 · 2019-02-22 14:42:14

题目:求0—7所能组成的奇数个数。

程序分析:

这个问题其实是一个排列组合的问题,设这个数为 sun=a1a2a3a4a5a6a7a8,a1-a8 表示这个数的某位的数值,当一个数的最后一位为奇数时,那么这个数一定为奇数,不管前面几位是什么数字。如果最后一位数为偶数,则这个数一定为偶数。

a1-a8可以取 0-7 这个八个数字,首位数字不为 0。

从该数为一位数到该数为8位数开始统计奇数的个数:

  • 1.当只有一位数时也就是该数的最后一位,奇数个数为4
  • 2.当该数为两位数时,奇数个数为4*7=28
  • 3.当该数为三位数时,奇数个数为:4*8*7=224
  • ...
  • 8.当该数为八位数时,奇数个数为:4*8*8*8*8*8*8*7(依次为最后一位到第一位)

实例

// Created by www.codercto.com on 15/11/9. // Copyright © 2015年 码农教程. All rights reserved. // #include<stdio.h> int main(int agrc, char*agrv[]) { long sum = 4, s = 4;//sum的初始值为4表示,只有一位数字组成的奇数个数为4个 int j; for (j = 2; j <= 8; j++) { printf("%d位数为奇数的个数%ld\n", j-1, s); if (j <= 2) s *= 7; else s *= 8; sum += s; } printf("%d位数为奇数的个数%ld\n", j-1, s); printf("奇数的总个数为:%ld\n", sum); // system("pause"); return 0; }

以上实例运行输出结果为:

1位数为奇数的个数4
2位数为奇数的个数28
3位数为奇数的个数224
4位数为奇数的个数1792
5位数为奇数的个数14336
6位数为奇数的个数114688
7位数为奇数的个数917504
8位数为奇数的个数7340032
奇数的总个数为:8388608

点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html

查看所有标签

Learn Python 3 the Hard Way

Learn Python 3 the Hard Way

Zed A. Shaw / Addison / 2017-7-7 / USD 30.74

You Will Learn Python 3! Zed Shaw has perfected the world’s best system for learning Python 3. Follow it and you will succeed—just like the millions of beginners Zed has taught to date! You bring t......一起来看看 《Learn Python 3 the Hard Way》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码