Example
input
616
output
99
题目分析
题目给出一个字符串,该字符串全部由数字组成,你需要输出经过排序后的最大的字符串
具体实现
我们知道,个位数字最大的是9,所以我们需要输出尽可能多的9,首先遍历一下字符串,判断除最后一位以外,是否全部由9组成,如果是,则输出该字符串,反之,则输出比该字符串长度小1位的9
AC代码及注释
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string str;
cin >> str;
int flag = 0;
int len = str.size(); //记录字符串长度
if(len == 1)
{
cout << str[0] << endl;
}
if(len != 1)
{
for(int i = 0 ; i < len - 1 ; i ++)
{
if(str[i] != '9') flag = 1;
//判断除了最后一位之外,是否全部由9组成
}
if(flag == 0) cout << str << endl;
// 如果是,输出该字符串
else
{// 如若不是,则输出 len - 1 位数字 9
for(int i = 0 ; i < len - 1 ; i ++)
{
cout << '9';
}
cout << endl;
}
return 0;
}
}
Example
input #1
5
0 1
0 3
5 1
6 1
9 2
output #1
1
input #2
2
-1000000000 1000000000
1000000000 1000000000
output #2
0
题目分析
该题目需要计算建筑覆盖不到的距离,用电线来连接,计算需要使用的电线的总和
具体实现
该题目需要使用到区间合并的算法,将每一个输入的小区间进行合并,之后遍历每一个大区间,计算大区间之间的距离总和
AC代码及注释
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL , LL> PII;
const int N = 2e5 + 100;
vector<PII> ve;
vector<PII> vec;
int main()
{
LL n;
cin >> n;
for(int i = 0 ; i < n ; i ++)
{ // 输入建筑坐标以及其半径
LL a , b;
cin >> a >> b;
ve.push_back({a - b , a + b});
// 将输入的数据存入名为 ve 的 vector 之中
}
sort(ve.begin() , ve.end());
//将数据进行升序排序
LL x = -4e9 , y = -4e9;
LL sum = 0;
LL l = 2e9 , r = 0;
for(auto i = ve.begin() ; i != ve.end() ; i ++)
{
// 进行区间合并
if(y < (*i).first)
{
if(x != -4e9) vec.push_back({x , y});
// 将数据存入到名为 vec 的 vector 之中
x = (*i).first;
y = (*i).second;
}
else
{
y = max(y , (*i).second);
}
}
if(x != -4e9) vec.push_back({x , y});
for(auto i = vec.begin() ; i != vec.end() ; i ++)
{
//计算每一个合并之后的区间之间的距离之和
l = min(l , (*i).first);
r = max(r , (*i).second);
sum += (*i).second;
sum -= (*i).first;
}
cout << r - l - sum << endl;
return 0;
}
Example
input
1
2
0 0 1
output
2.094395102393
题目分析
该题目实际上为一道数学几何题目,根据数学关系计算即可,注意线段要过圆心
AC代码及注释
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t; //输入 t 组数据
double r , x , y , d;
double sum = 0;
while(t--)
{
cin >> r >> x >> y >> d;//输入半径、坐标和半个线段长度
sum = 0;
double distance = sqrt(x * x + y * y);
sum = acos((distance - d) / r);
sum -= acos((distance + d) / r);
//通过数学关系式计算
printf("%.12lf\n", sum * r);
//保留 12 位小数输出
}
return 0;
}