原题链接:CCF-CSP 202006-3 Markdown渲染器
写的下面这个代码就是奔着40分去的,结果呢,细节又没把握好,一直只有20分,找错的时候发现自己犯的两个错误:
1.如果一开头就有空格行,需要忽略,无需计数
2.如果一个段落中,前一行没有满w(假设w为10,前一行刚好9个字符),那么需要换到下一个行加字符,这时候就刚好满足了w,需要先存入,再讨论该行的情况
#includeusing namespace std; const int N=510; bool check(string s) { for(int i=0;i if(s[i]!=' ') return false; } return true; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int w; cin>>w; int res=0; cin.ignore(); string s,t; vector v; int flag=0,len=0; while(!cin.eof()) { getline(cin,s); if(check(s)) { flag=1; if(len>0) v.push_back(t); len=0; t=""; continue; } if(flag && v.size()>0) v.push_back(" ");//错误点一 flag=0; int i=0,j=s.size()-1; while(i<=j && s[i]==' ') i++; while(j>=0 && s[j]==' ') j--; s=s.substr(i,j-i+1); i=0; if(len>0) { len++; t+=" "; if(len==w)//错误点二 { len=0; res++; v.push_back(t); t=""; } } while( len for(;i len++; t+=s[i]; } if(len==w) { len=0; res++; v.push_back(t); t=""; while(i 60分代码:后面的实在是写不出来了,罢了罢了
#includeusing namespace std; const int N=510; vector v; int w; int res=0,len=0,f=-1,pre=-1; bool check(string s) { for(int i=0;i if(s[i]!=' ') return false; } return true; } string drop(string s) { int i=0,j=s.size()-1; while(i<=j && s[i]==' ') i++; while(j>=0 && s[j]==' ') j--; s=s.substr(i,j-i+1); return s; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>w; cin.ignore(); string s,t; while(!cin.eof()) { getline(cin,s); if(check(s)) { if(!check(t)) v.push_back(t); t="";len=0; f=pre=0; } else if((s.size()>=2 && s[0]=='*' && s[1]==' ') || (pre==2 && s.size()>=2 && s[0]==' ' && s[1]==' ') ) { f=2; if(pre==0 && v.size()>0) v.push_back(" "); if(pre==1) { if(!check(t)) v.push_back(t); v.push_back(" "); t="";len=0; } if(s.size()>=2 && s[0]=='*' && s[1]==' ') { if(pre==2 && !check(t)) v.push_back(t); t=" . "; len=3; s=s.substr(2); s=drop(s); int i=0; while(len for(;i t+=s[i]; len++; } if(len==w) { if(!check(t)) v.push_back(t); len=3; t=" "; while(i =2 && s[0]==' ' && s[1]==' ') { if(len>0) { if(t!=" . ") { t+=" "; len++; if(len==w) { if(!check(t)) v.push_back(t); t=" ";len=3; } } } s=s.substr(2); s=drop(s); int i=0; while(len for(;i t+=s[i]; len++; } if(len==w) { if(!check(t)) v.push_back(t); len=3; t=" "; while(i f=1; if(pre==0 && v.size()>0) { v.push_back(" "); t=""; len=0; } if(pre==2) { if(!check(t)) v.push_back(t); v.push_back(" "); t=""; len=0; } if(len>0) { t+=" ";len++; if(len==w) { if(!check(t)) v.push_back(t); t=""; len=0; } } s=drop(s); int i=0; while(len for(;i t+=s[i]; len++; } if(len==w) { if(!check(t)) v.push_back(t); len=0; t=""; while(i



