csp202006-3 做题记录(getchar()造成的错误问题)

目录

参考了CCF CSP202006-3 Markdown渲染器的代码,将其中一些判断改为了find。

题目

202006-3

c++代码如下,在devc++上运行通过,也可以可以在vs上运行。

#include<iostream>
#include<stdio.h>
#include<string>
#include<vector> 
using namespace std;

//规定 1是项目,2是子项目,3是段落
vector<pair<string, int> > vec;
int w;

void trim(string& str) {
	str.erase(0, str.find_first_not_of(" "));
	str.erase(str.find_last_not_of(" ") + 1);
}

int main() {
	// 用于加速,不知道是不是这个导致getchar有问题?
	ios::sync_with_stdio(false);

	cin >> w;
	string temp;
	bool flag = true;
	
	getchar();//问题出在这里
	
	while (getline(cin, temp)) {
		/*cout << "输入的temp是" << temp<<endl;*/
		if (temp.find_first_not_of(" ") == -1) {
			//上一行是空行
			flag = true;
			continue;
		}
		if (flag) {
			if (temp.find("* ") == 0) {
				string mid = temp.substr(2);
				trim(mid);
				vec.push_back(pair<string, int>(mid, 1));
			}
			else {
				trim(temp);
				vec.push_back(pair<string, int>(temp, 3));
			}
		}
		else {
			pair<string, int>& last = vec.back();
			//cout <<"debug:"<< last.first << endl;
			if (last.second <= 2) {
				if (temp.find("* ") == 0) {
					string mid = temp.substr(2);
					trim(mid);
					vec.push_back(pair<string, int>(mid, 2));
				}
				else if (temp.find("  ") == 0) {
					string mid = temp.substr(2);
					trim(mid);
					if (last.first.length() != 0)
						last.first += " ";
					last.first += mid;
				}
				else {
					trim(temp);
					vec.push_back(pair<string, int>(temp, 3));
				}
			}
			else {
				if (temp.find("* ") == 0) {
					string mid = temp.substr(2);
					trim(mid);
					vec.push_back(pair<string, int>(mid, 1));
				}
				else {
					trim(temp);
					last.first += " ";
					last.first += temp;
				}
			}
		}
		flag = false;
	}
	flag = true;
	int lastnum = vec.begin()->second;
	int res = 0;

	for (vector<pair<string, int> >::iterator it = vec.begin(); it != vec.end(); it++) {
		if (!flag) {
			if (it->second != 2) {
				res++;
				cout << endl;
			}
		}
		flag = false;
		lastnum = it->second;
		int temp = 0;
		if (it->second <= 2) {
			temp = 3;
		}
		int len = it->first.length();
		if (len==0) {
			res++;
		}
		for (int i = 0; i < len;) {
			if (len - i >= w - temp) {
				cout << it->first.substr(i, w-temp) << endl;
				i += w - temp;
				res++;
				while (i<it->first.length()&&it->first[i] == ' ') {
					i++;
				}
			}
			else {
				cout << it->first.substr(i) << endl ;
				res++;
				break;
			}
		}
	}
	cout << res;
	return 0;
}
// 额外的一个测试用例 结果17
/*
10
asdfasdfsdfasfasfasf s           sdf sfasda          
                 asdf
                      
134

***************sadf
*** 
* sadfas
agfs
* 
*/

在本地运行我能想到的测试用例都没有问题,可是一提交就发现只有20分。
经过了一个下午的逐句替换为上述博客代码,发现把getchar()去掉就是100 分了。此getchar()的目的是去掉cin读取剩下的一个回车,但是此题其实不去掉也可以;至于为什么会只有20分,目前我不是很懂,希望有大佬可以帮我解除困惑……

最后吐槽一下垃圾ccf,连哪里错了都不讲,害我浪费了好长时间。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值