首页 > 试题广场 >

数学实验

[编程题]数学实验
  • 热度指数:1522 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 128M,其他语言256M
  • 算法知识视频讲解
给出一个数字n,需要不断地将所有数位上的值做乘法运算,直至最后数字不发生变化为止。
问最后生成的数字为多少?
示例1

输入

10

输出

0
示例2

输入

55

输出

0

说明

55 -> 5 * 5 = 25 -> 2 * 5 = 10 -> 1 * 0 = 0  

备注:
var res = 1,a,b;
function mathexp( n ) {
    // write code here
    if(n < 10){
        return n;
    }
    res = 1;
    while(n >= 1){
        a = n % 10;
        n = Math.floor(n / 10);
        res = a * res;
    }
    return mathexp(res);
}

发表于 2021-05-17 11:26:01 回复(0)
递归做法

int mathexp(long long n ) {
     
    int m = 1;
    if( n < 10)
        return n;
        while(n > 0)
        {
            m *= n % 10;
            n = n/10;
        }
    
    return mathexp(m);
    // write code here
}
发表于 2021-08-07 15:37:10 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
# 
# @param n long长整型 老师给牛牛的数字
# @return int整型
#
def calcu(x):
    n_fix = []
    if x < 10:
        return x
    else:
        while x > 0:
            n_fix.append(x%10)
            x = math.floor(x/10)
        out = 1
        for items in n_fix:
            out *= items
        return out


import math
class Solution:
    def mathexp(self , n ):
        # write code here
        result = calcu(n)
        while result >= 10:
            result = calcu(result)
        return result

发表于 2021-07-17 16:14:10 回复(0)
class Solution:
    def mathexp(self , n ):
        # write code here
        num = []
        multi = n
        if n<10:
            return n
        while multi>=10:
            n = multi
            multi = 1
            num = []
            if n<10:
                return n
            while n:
                num.append(n%10)
                n = int(n/10)
            if 0 in num:
                return 0
            else:
                for i in range(0,len(num)):
                    multi *= num[i]
        return multi

发表于 2021-07-12 11:25:34 回复(0)
func mathexp(n int64)int {
    var  single int64 =0
    var res int64=1
    if n<10{
        return int(n)
    }
    for n>=1{
        single=n%10
        n=n/10
        res = single*res
    }
    return mathexp(res)
}
发表于 2021-06-03 18:46:07 回复(0)
  num = str(n)
        tmp = int(n)
        while True:
            res=1
            for c in num:
                res*=int(c)
            if  res == tmp:
                break
            else:
                tmp = res
            num = str(res)
        return tmp
编辑于 2021-03-30 14:16:34 回复(0)
    public int mathexp (long n) {
        if(n>=0 && n<10)
            return (int)n;
        char[] ans=String.valueOf(n).toCharArray();
        int result=1;
        for(char a:ans){
            result=result*(a-'0');
        }
        return mathexp(result);
    }
发表于 2021-03-04 18:03:31 回复(0)
第一次击败百分之百,坚持坚持
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
# 
# @param n long长整型 老师给牛牛的数字
# @return int整型
#
class Solution:
    def mathexp(self , n ):
        # write code here
        sums = n
        while sums % 10 != sums:
            n = sums
            sums = 1
            while n != 0:
                sums *= n % 10
                n //= 10
        return sums

发表于 2021-02-25 13:33:28 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 
     * @param n long长整型 老师给牛牛的数字
     * @return int整型
     */
    public int mathexp (long n) {
        // write code here
        long t = 1;
        long m = n;
        while(m>=10){
            char[] c = String.valueOf(m).toCharArray();
            for(int i=0; i<c.length; i++){
                t*= Integer.parseInt(String.valueOf(c[i]));
            }
            m = t;
            t = 1;
        }
        return (int)m;
    }
}

发表于 2021-02-03 11:02:44 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# @param n long长整型 老师给牛牛的数字
# @return int整型
#
class Solution:
    def mathexp(self , n ):
        t = 1
        while n >= 10:
            a = list(str(n))
            for i in range(len(str(n))):
                t = t * int(a[i])
            n = t
            t = 1
        return n

发表于 2021-01-27 20:26:30 回复(0)

问题信息

难度:
10条回答 1647浏览

热门推荐

通过挑战的用户

查看代码