P1098 字符串的展开【洛谷】

题目链接:https://www.luogu.org/problemnew/show/P1098
这道题目我认为只是步骤较为繁琐,没有什么难度。自己写的时候因为好长时间没有写了。居然把一个判定条件写错了!=_=浪费了好长时间。

#include <stdio.h>
#include <iostream>

using namespace std;

const int MAX_N = 150;

int p1, p2, p3;
char str[MAX_N];

int judge (char str1, char str2) {
//	if ((str1 >= 0 && str1 <= 9) && (str2 >= 'a' && str2 <= 'z')) return 1;
//	else if ((str2 >= 0 && str2 <= 9) && (str1 >= 'a' && str1 <= 'z')) return 1;
//	else return 0;
	if ((str1 >= '0' && str2 <= '9' && str2 >= '0' && str2 <= '9') || (str1 >= 'a' && str1 <= 'z' && str2 >= 'a' && str2 <= 'z')) return 0;
	else return 1;
}

void solve () {
	if (p1 == 1) {
		int i = 0;
		while (str[i] != '\0') {
			if (str[i] == '-') {
				
				if (p3 == 1) {
					if (judge (str[i - 1], str[i + 1])) {
						cout << "-" << str[i + 1]; i++; //cout << "1" << endl;
					}
					else if (str[i - 1] >= str[i + 1]) {
						cout << "-" << str[i + 1]; i++;
					}
					else if (str[i - 1] + 1 == str[i + 1]) {
						cout << str[i + 1]; i++;
					}
					else {
						int n = str[i - 1] + 1;
						int m = p2;
						while (n < str[i + 1]) {
							for (int j = 1; j <= m; j++) printf ("%c", n);
							n++;
						}
					}
				}
				else if (p3 == 2) {
					if (judge (str[i - 1], str[i + 1])) {
						cout << "-" << str[i + 1]; i++;
					}
					else if (str[i - 1] >= str[i + 1]) {
						cout << "-" << str[i + 1]; i++;
					}
					else if (str[i - 1] + 1 == str[i + 1]) {
						cout << str[i + 1];
						i ++;
					}
					else {
						int n = str[i + 1] - 1;
						int m = p2;
						while (n > str[i - 1]) {
							for (int j = 1; j <= m; j++) printf ("%c", n);
							n--;
						}
					}
				}
				i ++;
			}
			else {
				cout << str[i];
				i ++;
			}
		}
	}
	if (p1 == 2) {
		int i = 0;
		while (str[i] != '\0') {
			if (str[i] == '-') {
				if (p3 == 1) {
					if (judge (str[i - 1], str[i + 1])) {
						cout << "-" << str[i + 1]; i++;
					}
					else if (str[i - 1] >= str[i + 1]) {
						printf ("-%c", str[i + 1]); i++;
					}
					else if (str[i - 1] + 1 == str[i + 1]) {
						printf ("%c", str[i + 1]); i++;
					}
					else {
						int n = str[i - 1] + 1;
						int m = p2;
						while (n < str[i + 1]) {
							for (int j = 1; j <= m; j++) printf ("%c", n - 32);
							n++;
						}
					}
				}
				else if (p3 == 2) {
					if (judge (str[i - 1], str[i + 1])) {
						cout << "-" << str[i + 1]; i++; //cout << "1" << endl; //没有执行???? 
					}
					else if (str[i - 1] >= str[i + 1]) {
						printf ("-%c", str[i + 1]); i++;
					}
					else if (str[i - 1] + 1 == str[i + 1]) {
						printf ("%c", str[i + 1]); i++;
					}
					else {
						int n = str[i + 1] - 1;
						int m = p2;
						while (n > str[i - 1]) {
							for (int j = 1; j <= m; j++) {
								if (n >= '0' && n <= '9') printf ("%c", n);
								else printf ("%c", n - 32);
							}
							//cout << "1" << endl; 
							n--;
						}
					}
				}
				i ++;
			}
			else {
				cout << str[i];
				i ++;
			}
		}
	}
	if (p1 == 3) {
		int i = 0;
		while (str[i] != '\0') {
			if (str[i] == '-') {
				if (p3 == 1) {
					if (judge (str[i - 1], str[i + 1])) {
						cout << "-" << str[i + 1]; i++;
					}
					else if (str[i - 1] >= str[i + 1]) {
						printf ("-%c", str[i + 1]); i++;
					}
					else if (str[i - 1] + 1 == str[i + 1]) {
						printf ("%c", str[i + 1]); i++;
					}
					else {
						int n = str[i - 1] + 1;
						int m = p2;
						while (n < str[i + 1]) {
							for (int j = 1; j <= m; j++) printf ("*");
							n++;
						}
					}
				}
				else if (p3 == 2) {
					if (judge (str[i - 1], str[i + 1])) {
						cout << "-" << str[i + 1]; i++;
					}
					if (str[i - 1] >= str[i + 1]) {
						printf ("-%c", str[i + 1]); i ++;
					}
					else if (str[i - 1] + 1 == str[i + 1]) {
						printf ("%c", str[i + 1]); i ++;
					}
					else {
						int n = str[i + 1] - 1;
						int m = p2;
						while (n > str[i - 1]) {
							for (int j = 1; j <= m; j++) printf ("*");
							n--;
						}
					}
				}
				i ++;	
			}
			else {
				cout << str[i];
				i ++;
			}
		}
	}
	cout << endl;
}

int main() {
	cin >> p1 >> p2 >> p3;
	cin >> str;
	solve();
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值