Skip to content

Instantly share code, notes, and snippets.

@edward-hsu-1994
Created May 15, 2015 15:41
Show Gist options
  • Save edward-hsu-1994/1802ed4f667e09246e81 to your computer and use it in GitHub Desktop.
Save edward-hsu-1994/1802ed4f667e09246e81 to your computer and use it in GitHub Desktop.
Problem C - 2 the 9s
using System;
namespace CPE
{
class Program{
public static void Main(string[] args){
decimal value = decimal.Parse("999999999999999999999");
Console.Write(Degree(value));
Console.ReadKey(true);
}
static decimal Degree(decimal Value){
decimal sum = Sum(Value);//位數值總和
if(sum % 9 != 0)return 0;//如果不可以被整除則回傳0
if(sum == 9)return 1;//如果剛好等於自己則可以執行一次
return Degree(sum) + 1;//沒有除完則交給下一層計算
}
static decimal Sum(decimal Value){
decimal sum = 0;
for(; Value > 0;){
sum += Value % 10;
Value -= Value %10;
Value /= 10;
}
return sum;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment