ALGO-104算法训练 阿尔法乘积(c++)

版权声明:Nicolas https://blog.csdn.net/qq_42835910/article/details/85243029

算法训练 阿尔法乘积  

时间限制:1.0s   内存限制:512.0MB

    

问题描述

  计算一个整数的阿尔法乘积。对于一个整数x来说,它的阿尔法乘积是这样来计算的:如果x是一个个位数,那么它的阿尔法乘积就是它本身;否则的话,x的阿尔法乘积就等于它的各位非0的数字相乘所得到的那个整数的阿尔法乘积。例如:4018224312的阿尔法乘积等于8,它是按照以下的步骤来计算的:
  4018224312 → 4*1*8*2*2*4*3*1*2 → 3072 → 3*7*2 → 42 → 4*2 → 8
  编写一个程序,输入一个正整数(该整数不会超过6,000,000),输出它的阿尔法乘积。
  输入格式:输入只有一行,即一个正整数。
  输出格式:输出相应的阿尔法乘积。
  输入输出样例

样例输入

4018224312

样例输出

8

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char** argv) {
	string s;
	cin>>s;
	int m=1;
	for(int i=s.length()-1;i>=0;i--)
		if((s[i]-'0')%10)
			m*=(s[i]-'0');
	int n=m;
	while(n/10){
		m=n;
		n=1;
		while(m){
			if(m%10)
				n*=m%10;
			m/=10;
		}
	}
	cout<<n<<endl;
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42835910/article/details/85243029
今日推荐