有N个数,组成的字符串,如012345,求出字串和取MOD3==0的子串,如012 12 123 45。

#include <iostream>
using namespace std;

int getSum(string str, int begin, int len) {
	int sum = 0;
	while (begin < len) {
		sum += str.at(begin++) - '0';
	}
	return sum;
}

void findSubStr(string str) {
	int strlen = str.length();

	int sublen = strlen;

	while (sublen > 1) {	//长度控制
		int front = 0;
		int sublenTmp = sublen;
		int sum = getSum(str, front, sublen);

		while (sublenTmp <= strlen) {	//范围控制
			if (sum % 3 == 0) {
//				cout << sum << ' ';
				//args2是长度
				cout << str.substr(front, sublenTmp - front) << endl;
			}

			if (sublenTmp + 1 <= strlen) {
				sum -= str.at(front++) - '0';
				sum += str.at(sublenTmp++) - '0';
			} else {
				break;
			}
		}
		sublen--;
	}
}
上一篇:C#面对对象之封装、继承、多态的简单理解


下一篇:C#——面对对象之封装、继承、多态的简单理解