一、简单模拟
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;
}