C Exercise Example 83
100 cases of classic C language
Title: Find the number that can be composed of an odd number of 0-7.
Program analysis: use 1,3,5,7 for bits, 0 can not be the highest level.Single digits, double digits. . . 7 digits.
Source Code:
// Created by www.w3big.com on 15/11/9. // Copyright © 2015年 本教程. All rights reserved. // #include<stdio.h> #include<stdlib.h> int factorial(int a,int b);/*求阶乘*/ int main() { int sum=0,i; sum+=4; /*一位数不能按下面处理,一位数时的奇数有4个*/ for(i=2;i<8;i++) sum+=4*(factorial(7,i-1)-factorial(6,i-2)); printf("可以组成%d个奇数\n",sum); return 0; } int factorial(int a,int b) { int i,sum=1; if(b<=0)return 1; for(i=0;i<b;i++) sum*=(a-i); return sum; }
Run the above example output is:
可以组成29692个奇数