For interview training, calculate the number 1 in the number.

Source: Internet
Author: User

My Idea 1 is to use space for time. Enter N and assign n a number of digits to assign a n * number of digits to the string, then sprintf (STR, "% d", num) to the string input, and finally

The number of 1 can be obtained from the beginning to the traversal string. The Code is as follows:

#include "stdio.h"#include "stdlib.h"int main(){int n;int num;int digit=0,index=0;char *str;int total;scanf("%d",&n);num=n;while(n){n/=10;++digit;}        str = calloc(digit*num+1,1);memset(str,0,digit*num);str[0]='\0';for(index=1;index<=num;index++){sprintf(str+strlen(str),"%d",index);}index=0;total=0;while(str[index]!='\0'){if(str[index]=='1')total++;index++;}printf("%d\n",total);free(str);return 0;}

Solution to this question

1. The interview book is too complex and has no time for analysis, and the variable name is rather difficult to understand.

2 Haitao's book also has related solutions. It's complicated to use recursion or the like.

I was relieved when I met a blog post from a netizen.

What is the idea.

Assume that the input number is 21356.

How to calculate it? Of course, starting from the low position

If the low position is 6:> 1, then the number of 1 is greater than its number plus 1, and then multiplied by the weighting of this bit.

(2135 + 1) * 1

10: 5: (213 + 1) * 10

Hundreds of digits: 3 (21 + 1) * 100

1 For a thousand bits: When the value is 1, you need to consider the high and low bits. Obviously, if the value is not 0, x1999 --- x1000, at the same time, we also need to consider the low level as the number above 1000-1xxx.

It is XXX + 1.

10000 bits: (0 + 1) *

There is another case that does not explain how to deal with this bit when it is 0.

At this point, you only need to process the high level. For example, 10xx is obviously (1 + 0) * 100. Why not consider the low level? Because if the high level is removed, the hundred bits will be 0, and you do not need to consider the latter.

Let's get the code.

#include "stdio.h"#include "stdlib.h"int main(){  int n;  int factor=1;  int lower=0;  int current=0;  int higher=0;  int sum =0;  scanf("%d",&n);  while(n/factor!=0)  {lower=n-(n/factor)*factor;current = (n/factor)%10;higher =(n/factor)/10;switch(current){case 0: sum+=higher*factor;break;case 1: sum+=(higher*factor+lower+1);break;default:sum+=((higher+1)*factor);break;}factor*=10;  }  printf("%d",sum); return 0;}

This method is very cool, with wood, with wood ~

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.