XidianOJ 1123 K=1 Problem of Orz Pandas

题目描述

One panda named orz is playing a interesting game, he gets a big integer Num and an integer K. In this game, he can exchange two single numbers in Num. For example, he can get 1243 from 3241 by exchange 1 and 3.But orz can exchange at most K times. So what’s the biggest number after at most K times operations?

However, a VIP (Very Important Panda) of ACM/OPPC (Orz Panda Programming Contest) comittee thought this problem was too hard for Orz Pandas. So he simplified this problem with constraint K=1. Your task is to solve the simplified problem.

 

输入

Multiple cases. For each case:
The first line is an integer Num. 0 ≤ Num ≤ 10
1000, and it is guaranteed that Num is a legal integer and doesn’t contain any leading zero

 

输出

For each test case, print the biggest number after at most 1 times operation in a line.

--正文
字符串读取数,贪心找可能的数进行交换
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
char num[1001];

int findMax(char* num){
    int len = strlen(num);
    int max = -1,maxi;
    int i;
    for (i=0;i<len;i++){
        if (num[i] - '0' >= max) {
            max = num[i] - '0';
            maxi = i;
        }
    }
    return maxi;
}

int main(){
    while (scanf("%s",num) != EOF){
        char* begin = num;
        int len = strlen(num);
        int total = 0;
        bool over = false;
        while (!over){
            int maxi = findMax(&num[total]) + total;
            int now = total;
            while (num[now] == num[maxi] && now < maxi) now++;
            if (now == maxi) {
                total = maxi+1;
                if (num[total] == '\0') break;
                continue;
            }
            else {
                char temp = num[now];
                num[now] = num[maxi];
                num[maxi] = temp;
                break;
            }
        }
        printf("%s\n",begin);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/ToTOrz/p/6135347.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值