算法笔记学习(2)---入门模拟

一、简单模拟

1.问题A:剩下的树

题目链接:http://codeup.hustoj.com/problem.php?cid=100000575&pid=0

题目描述:

有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,…,L共L+1个位置上有L+1棵树。
现在要移走一些树,移走的树的区间用一对数字表示,如 100 200表示移走从100到200之间(包括端点)所有的树。
可能有M(1<=M<=100)个区间,区间之间可能有重叠。现在要求移走所有区间的树之后剩下的树的个数。

输入:

两个整数L(1<=L<=10000)和M(1<=M<=100)。
接下来有M组整数,每组有一对数字。

输出:

可能有多组输入数据,对于每组输入数据,输出一个数,表示移走所有区间的树之后剩下的树的个数。

样例输入:

4 2
1 2
0 2
11 2
1 5
4 7
0 0

样例输出:

2
5

代码如下:

#include<iostream>
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int l,m;
    while(scanf("%d %d",&l,&m)!=EOF)
    {
        if(l>=1 && l<=10000 && m>=1 && m<=100)
        {
            int tree[l+1];
            int l_sum = l+1;
            for(int i=0;i<=l;i++)
            {
                tree[i] = 1;
            }
            int num_l,num_r;
            for(int i=0;i<m;i++)
            {
                cin>>num_l>>num_r;
                for(int j=num_l;j<=num_r;j++)
                {
                    if(tree[j]==1)
                    {
                        tree[j] = 0;
                        l_sum--;
                    }
                    else continue;
                }
            }
            cout<<l_sum<<endl;
        }
    }
    return 0;
}

注意:题目要求的l和m是大于1的,但是测试样例中的l和m可能会等于0


2.问题B:A+B

题目链接:http://codeup.hustoj.com/problem.php?cid=100000575&pid=1

题目描述:

给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。现在请计算A+B的结果,并以正常形式输出。

输入:

输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。

输出:

请计算A+B的结果,并以正常形式输出,每组数据占一行。

样例输入:

-234,567,890 123,456,789
1,234 2,345,678

样例输出:

-111111101
2346912

代码如下:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    char a[15],b[15];
    char A[10],B[10];
    int m,n;
    while(scanf("%s %s",&a,&b)!=EOF)
    {
        int x = strlen(a);
        int j=0;
        for(int i=0;i<x;i++)
        {
            if(a[i]>='0' && a[i]<='9')
            {
                A[j++] = a[i];
            }
        }
        A[j] = '\0';
        sscanf(A,"%d",&m);
        if(a[0]=='-') m = -m;

        int y = strlen(b);
        j = 0;
        for(int i=0;i<y;i++)
        {
            if(b[i]>='0' && b[i]<='9')
            {
                B[j++] = b[i];
            }
        }
        B[j] = '\0';
        sscanf(B,"%d",&n);
        if(b[0]=='-') n = -n;

        cout<<m+n<<endl;
    }
    return 0;
}

注意:这里我们是把字符串转换成了整型,用到了sscanf()这个函数


3.问题 E: Shortest Distance

题目链接:http://codeup.hustoj.com/problem.php?cid=100000575&pid=4

题目描述:

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

输入:

Each input file contains one test case. For each case, the first line contains an integer N (in [3, 10^5]), followed by N integer distances D1 D2 … DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=10^4), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 10^7.

输出:

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

样例输入:

5 1 2 4 14 9
3
1 3
2 5
4 1

样例输出:

3
10
7

代码如下:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,x,m;
    cin>>n;
    int sum = 0;
    int dis[100001];
    for(int i=0;i<n;i++)
    {
        cin>>x;
        sum += x;
        dis[i+1] = sum;
    }
    int sum_2 = sum / 2;
    cin>>m;
    while(m--)
    {
        int l,r;
        cin>>l>>r;
        if(l>r) swap(l,r);
        int sum_lr = dis[r-1] - dis[l-1];
        cout<<min(sum_lr,sum-sum_lr)<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小天才才

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值