目录
1.Recover the Smallest Number
这题关键就是那个cmp函数不好想,想出来了还是比较容易做的。
另外一个要注意的就是去除前导零的那个while循环可以琢么一下
最后,傻逼的我居然用scanf对string类型进行输入,还一直找不到bug原因。。。。。。
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=10010;
string martix[maxn];
bool cmp(string a,string b){
return a+b<b+a;
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
cin>>martix[i];
}
sort(martix,martix+n,cmp);
string ans;
for(int i=0;i<n;i++){
ans+=martix[i];
}
while(ans.size()!=0&&ans[0]=='0'){
ans.erase(ans.begin());
}
if(ans.size()==0) printf("0");
else printf("%s",ans.c_str());
return 0;
}
2.二分查找
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
int binarysearch(int martix[],int left,int right,int value){
int mid;
while(left<=right){
mid=(right+left)/2;
if(martix[mid]>value){
right=mid-1;
}else if(martix[mid]<value){
left=mid+1;
}else return mid;
}
return -1;
}
int main(){
int A[10]={1,3,4,6,7,8,10,11,12,15};
printf("%d %d",binarysearch(A,0,9,6),binarysearch(A,0,9,9));
return 0;
}
3.第一个大于等于
注意,初试输入的right为数组长度+1
int lower_bound(int martix[],int left,int right,int value){
int mid;
while(left<right){
mid = left+right;
if(martix[mid]>=value){
right=mid
}else{
left=mid+1;
}
}
}
4.第一个大于
int lower_bound(int martix[],int left,int right,int value){
int mid;
while(left<right){
mid = left+right;
if(martix[mid]>value){
right=mid
}else{
left=mid+1;
}
}
}
5.木棒分割
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
int martix[3]={10,24,15};
int counts=7;
int gettimes(int length){
int times=0;
for(int i=0;i<3;i++){
times+=martix[i]/length;
}
return times;
}
int lefts=0,rights=martix[2];
int binartsearch(int left,int right){
int mid;
while(left+1<right){
mid=(left+right)/2;
if(gettimes(mid)>=counts){
left=mid;
}else{
right=mid;
}
}
return right;
}
int main(){
sort(martix,martix+3);
printf("%d",binartsearch(lefts,rights));
return 0;
}
6.快速幂的递归写法
typedef long long ll;
ll binarypow(ll a,ll b,ll m){
if(b==0) return 1;
if(b%2==1) return a*binarypow(a,b-1,m)%m;
else{
ll mul=binarypow(a,b/2,m);
return mul*mul%m;
}
}
7.快速幂的迭代写法
typedef long long ll;
ll binarypow(ll a,ll b,ll m){
ll ans=1;
while(b>0){
if(b&1){
ans = ans*a%m;
}
a=a*a%m;
b>>1
}
}