Try   HackMD

You can say 11

題目連結 UVa 10929

中文簡述

輸入一個字串,書判斷是否為11的倍數

[think]

11倍數判別法
奇數位相加-偶數位相加 如果是11的倍數,就是11的倍數

這題直接用字串去解決 方便又快速

solution:

#include<iostream>
using namespace std;
int main()
{
	string str;
	int odd,even,i;
	while(cin>>str and str!="0")
	{
		odd=(even=0);
		for(i=0;i<str.length();i++)
		{
			if(i%2==0)
				odd+=(int)str[i]-'0';
			else
				even+=(int)str[i]-'0';
		}
		if(abs(even-odd)%11==0)
			cout<<str<<" is a multiple of 11."<<endl;
		else
			cout<<str<<" is not a multiple of 11."<<endl;
	}
}

tags: UVA

回目錄 學習筆記