栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

CCF-CSP 202006-3 Markdown渲染器 60

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CCF-CSP 202006-3 Markdown渲染器 60

原题链接:CCF-CSP 202006-3 Markdown渲染器

写的下面这个代码就是奔着40分去的,结果呢,细节又没把握好,一直只有20分,找错的时候发现自己犯的两个错误:
1.如果一开头就有空格行,需要忽略,无需计数
2.如果一个段落中,前一行没有满w(假设w为10,前一行刚好9个字符),那么需要换到下一个行加字符,这时候就刚好满足了w,需要先存入,再讨论该行的情况

#include 
using 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分代码:后面的实在是写不出来了,罢了罢了

#include 
using 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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948548.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号