数字瘦身,最后输出一位数,例如:75 7+5=12 1+2=3 最终答案 3
using System;
public class Solution
{
private int renum;
public int diet(int in_num)
{
renum = 0;
while (in_num / 10 > 0)
{
renum = renum + in_num % 10;
in_num = in_num / 10;
}
renum = renum + in_num;
if (renum > 10)
{
diet(renum);
}
return renum;
}
}
public class Program
{
public static void Main(string[] args)
{
string s = Console.ReadLine();
int in_num = int.Parse(s);
Solution solution = new Solution();
int out_num = solution.diet(in_num);
Console.WriteLine(out_num);
Console.ReadKey();
}
}
注:主要使用递归方法diet实现基本功能……
原文地址:https://www.cnblogs.com/XiaoLang0/p/9846548.html
时间: 2024-10-05 04:33:39