原文

翻譯

 

C++

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int i,degree;

void cal(string str)//計算數字是否為9的倍數用 
{
	int sum=0;
	string cs1="";
	stringstream ss;
	
	for(i= 0;i<str.length();i++)//全部的位數相加
	{
		sum+=str[i]-'0';
	}
	
	if(sum%9==0)//判斷是否是9的倍數 
	{
		ss.clear();//將數字轉成字串 
		ss<<sum;
		ss>>cs1;
		
		degree+=1;//階層+1 
		
		if(sum!=9)
		{
			cal(cs1);
		}
	}
}
int main()
{
    string is1;//數字很大用字串 
    while(cin>>is1&&is1!="0")//輸入數字 
    {
    	degree=0;
    	
    	cal(is1);
    	
    	if(degree!=0)//輸出 
    	{
    		cout<<is1<<" is a multiple of 9 and has 9-degree "<<degree<<"."<<endl;
		}
		else
		{
			cout<<is1<<" is not a multiple of 9."<<endl;
		}
		
	}
    
    return 0;
}
 
創作者介紹
創作者 程式道路,必為豐富 的頭像
豪CO

程式道路,必為豐富

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