【2019暑期】【PAT乙级】1078 字符串压缩与解压 (20 分)

48 篇文章 0 订阅

复习

字符判断

inlcude <cctype>

isupper(c) 是否为大写
islower(c) 是否为小写
isdigit(c) 是否为数字
isalpha(c) 是否为字母
isalnum(c) 是否为字母或数字
tolower() 大写字母转换为小写字母
toupper() 小写字母转换为大写字母

类似于字符串中整理数字的办法(ssre56sdf8)这种,找出里面的数字

int count = 0;
while(){
	if(isdigit(string[i]){
		count = count * 10 + string[i] - '0';
	}
}

问题:
to_string 用不了,头文件也加了,不知道为什么
19分代码,第三点不知道哪里错了,也找不到哪里错了,,,

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <cmath>

using namespace std;

char key;
int l;
string in,out;
string a; 
 
void yasuo(){
	getchar();
	getline(cin,in);
	l = in.length();
	char com = ' ';
	int num = 1;
	for(int i=0; i<l+1; i++){
		if(in[i] == com){
			num = num + 1;
		}
		else {
			if(i != 0){
				if(num != 1){
					cout << num << com;				
				}
				else{
					cout << com;
				}				
			}			
			com = in[i];
			num = 1;
			
		}
	}
	cout << out << endl;
}

void jieya(){
	getchar();
	getline(cin,in);
	l = in.length();
	int wei = -1;
	int flag = 0;
	int num = 0;
	vector<int> cun(0);
	for(int i=0; i<l+1; i++){
		if(isdigit(in[i])){
			int c = in[i] - '0';
			cun.push_back(c);
			wei = wei + 1;
			flag = 1;
		}
		else{
			if(flag){
				int ll = cun.size();
				for(int k=0; k<ll; k++){
					num += cun[k] * pow(10,wei);
					wei = wei-1;
				}
				for(int j=0; j<num; j++)
					cout << in[i];
				flag = 0;
				num = 0;
				wei = -1;
				cun.clear();
			}
			else cout << in[i];
		}
	}
}
 
int main(){
	
	cin >> key;
	if(key == 'C') yasuo();
	else jieya();


	return 0;
} 

迷茫,,重写了解压部分还是不对,,难道是错在了压缩部分?
不深究了,,可能一些小点没办法,,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值