【C++】1877 - Digital Relay

question

The cows are playing a numbers game, and they start with a whole number, such as: 6593. Take out all the digits in this integer and multiply them to get a new integer.
The above example is 6×5×9×3=810, and then continue to do 8×1×0=0 to get a single digit 0.
Help the cow complete this game by reading in a number and calculating the game to get a single digit.

Insert image description here

1. Analyze the problem

  1. Known: an integer
  2. Unknown: The process of getting a single digit
  3. Relationship: split solution

2. Define variables

	//二、数据定义
	int n,temp;

3. Enter data

//三、数据输入 
	cin>>n;

4.Data calculation

This question requires splitting the digits to get the number on each digit and then multiplying to get a new number, so the first thing to think about is short division.
temp: actually the value of n, used for the iteration of short division.
result: the new number obtained after splitting operation.

temp=n;
int result=1;
while(temp>0){
    
    
			result*=temp%10;
			temp/=10;
		}

The entire splitting process continues until the input integer n becomes a single digit.

while(n>=10){
    
    
		//五、输出结果
		cout<<result<<" ";
		n=result;//条件迭代
	}

5. Output results

#include<iostream>
using namespace std;
int main(){
    
    
	//一、分析问题
	//已知:一个整数
	//未知:得到一个个位数的过程
	//关系:拆位求解 

	//三、数据输入 
	cin>>n;
	//四、数据计算
	
	cout<<n<<" ";
	
	while(n>=10){
    
    
		temp=n;
		int result=1;
		while(temp>0){
    
    
			result*=temp%10;
			temp/=10;
		}
		//五、输出结果
		cout<<result<<" ";
		n=result;
	}
	

	 
	return 0;	
}

Guess you like

Origin blog.csdn.net/qq_39180358/article/details/135213561