PTA 求解保留数最大问题

输入一个高精度的正整数n,去掉其中任意s个数字后剩下的数字按原左右次序组成一个新的正整数。编程对给定的n和s,寻找一种方案使得剩下的数字组成的新数最大。

输入格式:

输入两个数n(存储为字符串)和s。

输出格式:

输出最大数

输入样例:

175438 4

输出样例:

78

思路:

  1. 本题的数字需要用字符类型表示

  1. 每次删去升序数列的第一个可保证所得数最大(反之可求最小数)

代码:

#include<stdio.h>
int main()
{
    char a[1000000];
    scanf("%s",a);
    int s;
    scanf("%d",&s);
    int c=0;
    while(a[c]!='\0')
    {
        c++;
    }
    int i,j,k;
    for(i=0; i<s; i++)
    {
        for(j=0; j<c-1; j++)
        {
            if(a[j]<a[j+1])
            {
                for(k=j; k<c-1; k++)
                {
                    a[k]=a[k+1];
                }
                break;
            }
        }
        c--;
    }
    for(i=0; i<c; i++)
    {
        printf("%c",a[i]);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PTA(Programming Test and Assessment)平台上有很多简单的四则运算表达式题目,可以练习基本的算术运算和表达式求值。下面是一个例子: 题目描述: 给定一个只包含加、减、乘、除四种基本运算的表达式,求表达式的值。 输入格式: 第一行包含一个整数 T,表示共有 T 组测试数据。 每组数据占一行,包含一个长度不超过 50 的只包含数字与加、减、乘、除四种运算符的表达式。 输出格式: 对于每组数据,输出表达式的值,保留两位小数。 样例输入: 2 1+2*3-4/5 3+5*8-6/2 样例输出: 6.20 43.00 代码示例(C++): ```cpp #include <iostream> #include <stack> #include <string> #include <iomanip> using namespace std; int main() { int T; cin >> T; while (T--) { string s; cin >> s; stack<double> nums; stack<char> ops; for (int i = 0; i < s.size(); i++) { if (isdigit(s[i])) { double num = s[i] - '0'; while (i + 1 < s.size() && isdigit(s[i + 1])) { num = num * 10 + s[i + 1] - '0'; i++; } nums.push(num); } else if (s[i] == '(') { ops.push('('); } else if (s[i] == ')') { while (ops.top() != '(') { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.pop(); } else if (s[i] == '+' || s[i] == '-') { while (!ops.empty() && (ops.top() == '+' || ops.top() == '-' || ops.top() == '*' || ops.top() == '/')) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.push(s[i]); } else if (s[i] == '*' || s[i] == '/') { while (!ops.empty() && (ops.top() == '*' || ops.top() == '/')) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.push(s[i]); } } while (!ops.empty()) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } cout << fixed << setprecision(2) << nums.top() << endl; } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值