键盘输入一个正整数N,去掉其中任意S个数字后剩下的数字按原左右次序将组成一个新的正整数。编程对给定的N和S,寻找一种方案使得剩下的数字组成的新数最小。(N不超过240位,N > S)
输入描述
两行,第一行:正整数n,第二行:正整数S。
输出描述
n去掉的s个数字后组成的新的正整数m。
样例输入
123006 2样例输出
1006来源
2016年中北大学新生赛
提示
补充样例
输入
123006 3
输出
6
#include <cstdlib>
#include <cstdio>
#include<iostream>
#include <string>
#include<cstring>
using namespace std;
int main()
{
string a;
long s,t;
cin>>a>>s;
t=a.length();
if(s>=a.length())
{
cout<<"0"<<endl;
return 0;
}
//printf("%d***********%d\n",t,a.length());
for(int i=1; i<=s; i++,t--)
{
//printf("***********\n");
for (int j=0; j<t; j++)
{
// printf("*******************\n");
if(a[j]>a[j+1])
{
a=a.erase(j,1);
break;
}
}
}
cout<<stoi(a.c_str())<<endl;//stoi函数默认数据范围int型
return 0;
}
这个代码总是ac不了,emmm,原来stoi函数默认输出int类型,否则会溢出[-2147483648, 2147483647]
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
string a;
int s,t;
cin>>a>>s;
t=a.length();
if(s>=a.length())
{
cout<<"0"<<endl;
return 0;
}
//printf("%d***********%d\n",t,a.length());
for(int i=1; i<=s; i++,t--)
{
//printf("***********\n");
for (int j=0; j<t; j++)
{
// printf("*******************\n");
if(a[j]>a[j+1])
{
a=a.erase(j,1);
break;
}
}
}
int i=0;
for(i=0;i<a.length();) {
if(a[i]=='0') i++;
else break;
}
cout<<a.substr(i)<<endl;
return 0;
}
稍做处理,不过oj上的用例过了,如果是100100这种数据就不行