Branch structure---reverse output of numbers

Branch structure—reverse output of numbers

Description of the problem:
Please enter a positive integer less than 100000, please output the number in its various positions backwards, and find the sum of each digit?
For example: input 12345, the output is: 5 4 3 2 1 15
Another example: input 123 then output: 3 2 1 6
Input:
input a positive integer n less than 100000

Output
several numbers, separated by spaces between numbers, and the last number is the sum of each number

enter result
12345 5 4 3 2 1 15
123 3 2 1 6

Thought process The
first time I saw this question, I thought of the input and output of the array. The difficulty of this question is very simple. After all, it is not too easy to be done by me at once.
The ideas for this question are summarized as follows:

  1. Define an array
  2. Assign a value to each subscript in a loop, and calculate the value of sum;
  3. After the loop is over, an array space is given to store the value of sum;
  4. Loop to output all element values ​​of array a; the
    previous code
#include<stdio.h>
int main(){
    
    
	int a[100001],n,t=0,i,sum=0;
	scanf("%d",&n);
	for(i=0;n!=0;i++){
    
    
		a[i]=n%10;//循环赋值
		n=n/10;//每赋值一次就除以10,减少一位
		sum+=a[i];//计算每一位的数之和
		t++;//看这是多少位的数字;
	}
	a[t]=sum;//先开辟一个空间把sum值存进去
	for(int r=0;r<=t;r++){
    
    
		printf("%d ",a[r]);//直接循环输出
	}
	
}

Summary
This question mainly masters the basic loop structure, and it is easy to solve it by mastering the application of arrays.

Guess you like

Origin blog.csdn.net/qq_52044923/article/details/109957832