NC207641. LargerNumber
描述
输入描述
A single line contains two integers n,k (1<=n<=101000, 0<=k<=1000).
输出描述
Print one integer in a single line, represents the largest number Binbin can get after at most k operations.
示例1
输入:
1205 2
输出:
2150
C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 616K, 提交时间: 2020-06-22 12:31:31
#include<iostream> #include<cstdio> #include<string> #include<algorithm> using namespace std; int main() { string s; int k; cin>>s>>k; int len=s.size(); int pre_k=k,pre_pos=0; char num; int id; while(pre_k&&pre_pos<len) { num=s[pre_pos]; id=pre_pos; for(int i=pre_pos+1;i<=pre_pos+pre_k&&i<len;i++) { if(s[i]>num) { num=s[i]; id=i; } } for(int i=id;i>pre_pos;i--) { char t; t=s[i]; s[i]=s[i-1]; s[i-1]=t; } pre_k-=(id-pre_pos); pre_pos++; } cout<<s<<endl; return 0; }
C(clang 3.9) 解法, 执行用时: 4ms, 内存消耗: 376K, 提交时间: 2020-06-21 16:51:07
#include<stdio.h> #include<string.h> int main(){ char a[2000]; int k; while(~scanf("%s%d",a,&k)){ int i,j,t,len; char temp; len=strlen(a); for(i=0;i<len&&k!=0;i++){ t=i; for(j=i+1;j<=i+k&&j<len;j++) if(a[t]<a[j]) t=j; for(j=t;j>i;j--){ temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } k-=t-i; } printf("%s\n",a); } return 0; }