Classroom Practice _ Find out the number of 1

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.