First, thinking ideas
In finding out 1 of the number, first of all can not use the method of exhaustive. In this program, I borrowed from the classmate's method, will each bit
The number of values on the 1 is added and the final number is finally reached.
Second, the Code
#include"stdafx.h"#include<stdio.h>intCount (intnum) { intcount,mul,num1,num2,num3; Count=0; Mul=1; NUM1=num2=num3=0; while(num/mul) {NUM1=num-(Num/mul) *Mul; Num2= (Num/mul)%Ten; NUM3=num/(mul*Ten); Switch(num2) { Case 0: Count+=num3*Mul; Break; Case 1: Count+=num3*mul+num1+1; Break; default: Count+ = (num3+1)*Mul; Break; } mul*=Ten; } returncount;} intMainintargcChar*argv[]) { intNum,ones; printf ("Please enter a positive integer:"); scanf ("%d",&num); Ones=Count (num); printf ("The number of 1 in this value is:%d\n", ones); return 0; }
Three
Iv. Summary
In this code, I based on the teacher's ideas and did not draw any simple rules, but in listening to the students ' ideas and code before
With a good solution, each one will appear 1 of the number of records, summed up the results.
Classroom Practice _ Find out the number of 1