解题报告:UVa10929(奇偶差位法)

版权声明:个人笔记,仅供复习 https://blog.csdn.net/weixin_41162823/article/details/82184422

原题链接:UVa10929

解析:本题可以用递推或者奇偶差位法:

  • 从右至左,分别将奇数位的数字和偶数位的数字加起来,再求他们的差。如果这个差是11的备注(包括0),则A一定能被11整除。

代码示例:

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	string str;
	while(cin >> str && str != "0"){
		int len = str.length();
		int sum = 0;
		for(int i = len-1;i >= 0;i--){
			if((i+1)&1)	sum += str[i]-'0';
			else sum -= str[i]-'0';
		}
		if(sum % 11)	cout << str << " is not a multiple of 11." << endl;
		else cout << str << " is a multiple of 11." << endl;
		
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_41162823/article/details/82184422