Thoughts triggered by a question (0): Use a while loop to take out each digit of each number.

@author:Bzdhxs_nt;
@date:2020/11/4;
Cause: An OJ exercise from a freshman in a school. The
simple description is to enter an n (0<=n<=9), and how many n characters are there from 1 to 2020.
Then we have to take out the number of each digit of each number. Before this, I would only use general% and / to deal with, but it is too troublesome to face this question!
Here is how to use the while statement to get a number:

	while(x)     
	{
    
    
		t = x%10;
		x /= 10}

analysis

while(x)     
	{
    
    
		t = x%10;  //用10取余来得到x的个位 ,并赋值给t;
				   //用个printf就取出个位! 
		x /= 10//整除10获得失去个位的x1;
				   //后循环进行,当x只剩一位时, 
	}			   //被取余10得到最高位,被整除10得到x = 0,				
				   //while 循坏结束 从个 十 百 ...的顺序		
				   //取出每位。		

The introduction of features is convenient, but what you get is the reverse order of sorting. Therefore, it can be used to reverse the order of a number, which is very convenient!
Finally, the source code of this question is given:

/* 利用while循环分离数的每位
@author:Bzdhxs_nt;
@date:2020/11/4;    */ 

#include<stdio.h>
int main()
{
    
    
	int n,t;
	scanf("%d",&n);
	int s=0;
	for(int x = 1;x <= 2020;x++)//遍历
	{
    
    	
	int b = x;
	while(b)    //遍历 1 到 2020,即b; 
	{
    
    
	t = b%10;	// 用取余取出b的每位赋值给t 
	if(t == n)	s++;//当t== n时给s加一最后用s来输出要求的N的数量
	b /= 10;
	}
	}
	printf("%d",s);
	return 0;
}

Hope to point out if there is something incorrect!
Thank you for your guidance and let me solve this problem.
Welcome to discuss with me.

Guess you like

Origin blog.csdn.net/qq_51687628/article/details/109501971