csp201703

分蛋糕

实现

#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[1010];
int main()
{
    
    
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
    
    
        scanf("%d",&a[i]);
    }
    int sum = 0;
    int res = 0;
    for(int i=0;i<n;i++)
    {
    
    
        sum = 0;
        int j=i;
        while(sum<k && j<n)
        {
    
    
            sum+=a[j];
            j++;
        }
        res++;
        i = j-1;
    }
    cout<<res<<endl;
}

学生排队

实现

注意p的正负即可

#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[1010];
int main()
{
    
    
    int p,q;
    cin>>n>>k;
    int t;
    for(int i=1;i<=n;i++) a[i] = i;
    for(int i=0;i<k;i++)
    {
    
    
        scanf("%d %d",&p,&q);
        for(int j=1;j<=n;j++)
        {
    
    
            if(a[j]==p)
            {
    
    
                t = j;
                break;
            }
        }
        int tt = a[t];
        if(q<0)
        {
    
    
            for(int j=t;j>t+q;j--)
            {
    
    
                a[j] = a[j-1];
            }
            a[t+q] = tt;
        }
        else
        {
    
    
            for(int j=t;j<t+q;j++)
            {
    
    
                a[j] = a[j+1];
            }
            a[t+q] = tt;
        }
    }
    for(int i=1;i<=n;i++)
    {
    
    
        printf("%d ",a[i]);
    }
}

markdown

实现

来自acwing-yxc

#include <bits/stdc++.h>
using namespace std;
vector<string> strs;

int work_link(string str, int i)
{
    
    
    string text, link;
    for (i ++; str[i] != ']'; i ++ )
    {
    
    
        char c = str[i];
        if (c == '_')
        {
    
    
            text += "<em>";
            i ++ ;
            while (str[i] != '_') text += str[i ++ ];
            text += "</em>";
        }
        else text += c;
    }
    for (i += 2; str[i] != ')'; i ++ )
        link += str[i];
    printf("<a href=\"%s\">%s</a>", link.c_str(), text.c_str());
    return i;
}

int work_em(string str,int i)
{
    
    
    printf("<em>");
    for(i++;str[i]!='_';i++)
    {
    
    
        char c = str[i];
        if(c=='[') i = work_link(str,i);
        else cout<<c;
    }
    printf("</em>");
    return i;
}
void work_line(string str)
{
    
    
    int k=0;
    while(str[k]==' ') k++;
    str = str.substr(k);
    
    for(int i=0;i<str.size();i++)
    {
    
    
        char c = str[i];
        if(c=='_') i = work_em(str,i);
        else if(c=='[') i = work_link(str,i);
        else cout<<c;
    }
}
void work(int a,int b)
{
    
    
    if(strs[a][0]=='#')
    {
    
    
        int k = 0;
        while(strs[a][k]=='#') k++;
        printf("<h%d>",k);
        work_line(strs[a].substr(k));
        printf("</h%d>\n",k);
    }
    else if(strs[a][0]=='*')
    {
    
    
        printf("<ul>\n");
        for(int i=a;i<=b;i++)
        {
    
    
            printf("<li>");
            work_line(strs[i].substr(1));
            printf("</li>\n");
        }
        printf("</ul>\n");
    }
    else
    {
    
    
        printf("<p>");
        for(int i=a;i<=b;i++)
        {
    
    
            work_line(strs[i]);
            if(i!=b)cout<<endl;
        }
        printf("</p>\n");
    }
}
int main()
{
    
    
    string str;
    while(getline(cin,str)) strs.push_back(str);
    
    for(int i=0;i<strs.size();i++)
    {
    
    
        if(strs[i].empty()) continue;
        int j = i+1;
        while(j<strs.size() && strs[j].size()) j++;
        work(i,j-1);
        i = j-1;
        
    }
}

地铁修建

思路

kruskal选边,直到find(1)==find(n)

猜你喜欢

转载自blog.csdn.net/Tracy_yi/article/details/129170750