In-the-box assignment-there are several "1" (C + +)

Source: Internet
Author: User

First, design ideas

When discussing in class, the teacher's idea was to use the previous results to calculate the "1" of the larger number. But I do not think so, I am the input of the positive integer number of each bit on the decomposition of the calculation. such as ABC, first count C, plus B, and finally add a.

Second, the source code

1 //One.cpp:Defines the entry point for the console application.2 //3 4#include"stdafx.h"5#include"iostream.h"6 7 intFindones (intnum)8 {9     intones,mul,num1,num2,num3;Tenones=0; OneMul=1;//Divisor, 10 of the N-th square Anum1=num2=num3=0; -      while(Num/mul)//when the rounding is not zero, the loop is calculated -     { thenum1=num-(Num/mul) *mul;//the number on the latter one -num2= (Num/mul)%Ten;//number on the current number of digits -num3=num/(mul*Ten);//the number on the previous one -         Switch(num2)//If the positive integer is abc,b as the current number of digits +         { -              Case 0://The remainder is 0 o'clock, containing A*mul 1; +ones+=num3*Mul; A                  Break; at              Case 1://The remainder is 1 o'clock and contains a*mul+c+1; -ones+=num3*mul+num1+1; -                  Break; -             default://the rest of the >1 are (a+1) *mul -ones+= (num3+1)*Mul; -                  Break; in         } -mul*=Ten;//change the divisor and continue to calculate higher to     } +     returnones; - } the intMainintargcChar*argv[]) * { $     intNumber,ones;//number, number of 1Panax Notoginsengcout<<"Please enter a positive integer:"; -Cin>>Number ; theones=findones (number); +cout<<number<<"The number of "1" in"<<ones<<Endl; A     return 0; the}

Iii. Results

Iv. Experience

When I hit the code, I thought it would be very good to write, want to use the idea of recursion to achieve, but did not be able to summarize the situation on each person. So draw on the idea of the Internet, there is a program is a one-time look at three, the current bit, high one and low one. The code is simple and perfect.

I find that every time I write this algorithm-oriented program, I am stretched out, or have to practice more, improve their ability to optimize the code.

In-the-box assignment-there are several "1" (C + +)

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.