swustojDelete Numbers(0700)

    xiaoxiao2021-04-19  212

    给定n 位正整数a,去掉其中任意k≤n 个数字后,剩下的数字按原次序排列组成一个新的正整数。对于给定的n位正整数a(n<100) 和正整数k,设计一个算法找出剩下数字组成的新数最小的删数方案。 对于给定的正整数a,编程计算删去k个数字后得到的最小数。

    Description

    第1 行是1 个正整数a。第2 行是正整数k。

    Input

    计算出的最小数(输出无前导0)

    Output 1 2 178543 4 Sample Input 1 13 //如果数字式升序排列,就删除最后一位数,否则删除最先突变的那一位数的前一位数 //即最先没按照升序排列的数字的前一位。ex:12312 在第二个1处突变,故应该删除3. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include<stack> #include<iostream> #include<string.h> using namespace std; int main() { int kk; char str[10000]; while (cin >> str) { cin >> kk; int len = strlen(str); int head = 0; for (int k = 0; k < kk; k++) { int i = head + 1; while (str[i] >= str[i - 1] && i < len)i++; i--; for (int j = i; j >= head; j--) { str[j] = str[j - 1]; } head++; } while (str[head] == '0'&&head != len - 1)head++; if (head < len) { for (int i = head; i < len; i++) { cout << str[i]; } cout << endl; } } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-676269.html

    最新回复(0)