close

原文

https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2307

感謝Ruby兔大大的翻譯

https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2307

 

C++

#include <iostream>
using namespace std;

int cal (long long x)//計算的函式 
{
	int sum=0;
	while(x)//每個位數加總 
	{
		sum=sum+x%10;//sum與x最左邊的數相加 
		x=x/10;
	}
	
	if(sum>=10)//如果還是大於10要再加總一次 
	{
		sum=cal(sum);
	}
	
	return sum; 
}

int main()
{
	long long in1;
	
	while(cin>>in1)
	{
		if(in1==0)//輸入0結束 
		{
			break;
		}
		
		cout<<cal(in1)<<endl;//輸出結果 
	}
	
	return 0;
}
 
arrow
arrow
    文章標籤
    UVa一星題
    全站熱搜
    創作者介紹
    創作者 豪CO 的頭像
    豪CO

    程式道路,必為豐富

    豪CO 發表在 痞客邦 留言(0) 人氣()