蓝桥杯 密码发生器

时间:2022-09-10 10:23:21

蓝桥杯——密码发生器

第一步:将字符串转换为一行只有6个字母的二维数组

第二步:将每一列单独加起来

第三部:递归处理数据直至只剩个位数

代码:

#include<iostream>
#include<string>
using namespace std;

int toOnenum(int n) {
	int sum = 0;
	if (n / 10 == 0) {
		return n;
	}
	else {
		while (n) {
			sum += n % 10;
			n /= 10;
		}
		return toOnenum(sum);
	}
}

void change(int num[6]) {
	for (int i = 0; i < 6; i++) {
		cout << toOnenum(num[i]) << ' ';
	}
	cout << endl;
}

int main() {
	int n;
	cin >> n;
	string *arr;
	arr = new string[n];
	cin.get();
	for (int i = 0; i < n; i++) {
		getline(cin, arr[i]);
	}
	for (int i = 0; i < n; i++) {
		string buf = arr[i];
		int x = buf.length() / 6 + 1;
		string *str = new string[x];
		int z = 0;
		for (int j = 0; j < x; j++) {
			for (int k = 0; k < 6 && buf[z]; z++, k++) {
				str[j] += buf[z];
			}
		}
		for (int c = 0; c < x; c++, cout << endl) {
			cout << str[c];
		}
		int num[6] = { 0, 0, 0, 0, 0, 0 };
		for (int m = 0; m < x; m++) {
			for (int n = 0; n < str[m].length(); n++) {
				num[n] += str[m][n];
			}
		}
		change(num);
	}
	return 0;
}