当前位置: 首页 > 工具软件 > Bronze > 使用案例 >

Fence Painting【USACO 2015 December Contest, Bronze】

邵骏喆
2023-12-01

        今天继续来分享我做过usaco的比较简单,适合初学者的题。

        刷了那么多题目,可能有的程序猿会问:“小编,你是不是要预备考试呀?”我想回答大家的是:“不是,我学习c++只不过是拓展了兴趣爱好,并不是想进军专业(可能能力不够,【捂脸】)”其实也是想给你们分享一些思维类的好题,以便有der程序猿做不来,可以查找文章。在此也非常感谢那些默默在支持我的人们,虽然大家没有点赞,但是可能处于对我的信任,阅读量在蹭蹭往上涨。

        好啦,切入正题,讲题目!

题目大意:

Several seasons of hot summers and cold winters have taken their toll on Farmer John's fence, and he decides it is time to repaint it, along with the help of his favorite cow, Bessie. Unfortunately, while Bessie is actually remarkably proficient at painting, she is not as good at understanding Farmer John's instructions.

If we regard the fence as a one-dimensional number line, Farmer John paints the interval between x=ax=a and x=bx=b. For example, if a=3a=3 and b=5b=5, then Farmer John paints an interval of length 2. Bessie, misunderstanding Farmer John's instructions, paints the interval from x=cx=c to x=dx=d, which may possibly overlap with part or all of Farmer John's interval. Please determine the total length of fence that is now covered with paint.

几个炎热的夏天和寒冷的冬天已经对农场主John的栅栏造成了伤害,他决定是时候在他最喜欢的奶牛贝西的帮助下重新粉刷栅栏了。不幸的是,尽管贝西实际上非常擅长粉刷,但她并不擅长理解农夫约翰的指示。

如果我们把栅栏看成一维数轴,John粉刷了x=a和x=b之间的区间。例如,如果a=3, b=5,那么John画了一个长度为2的区间。贝西误解了John的指令,粉刷了从x=c到x=d的区间,这段区间可能与John的部分或全部区间重合。请确定现在油漆覆盖的栅栏的总长度。

样例输入:

The first line of the input contains the integers a and b, separated by a space (a<b).
The second line contains integers c and d, separated by a space (c<d).

The values of a, b, c, and d all lie in the range 0…100, inclusive.

输入的第一行包含整数a和b,由空格分隔(a<b)

第二行包含整数c和d,由空格分隔(c<d)

a、b、c和d的值都在0…100范围内,包括100。

7 10

4  8

样例输出:

Please output a single line containing the total length of the fence covered with paint.
请输出一行,表示油漆覆盖的栅栏的总长度。

6

样例解释:

Here, 6 total units of fence are covered with paint, from x=4 all the way through x=10.
6个栅栏单元被油漆覆盖,从x=4一直到x=10。

作者的代码思路:

这道题我用了最简单的笨方法,一个for循环从1-100,判断这个数是否存在于油漆粉刷的区间里,设置一个变量,每找到一个 变量+1,最后输出变量就好了。

建议大家如果写不来,先看我上面的代码思路,尝试着自己写,尽量不要直接复制哦!

#include<bits/stdc++.h>
using namespace std;
int a, b, c, d, p;
int main()     
{    
	cin >> a >> b >> c >> d;
	for(int i = 0; i < 100; i++){
		if(i >= a && i + 1 <= b) p++;
		else if(i >= c && i + 1 <= d) p++;
	}
	cout << p;
    return 0;    
}

是不是真的很简单呀!

最后的最后,不喜勿喷,蟹蟹        

 类似资料:

相关阅读

相关文章

相关问答