给定任意一个数字 m,然后给出数字 n,则需在 m 中去掉 n 位数,保持各位顺序不变的情况下,得到最大数

//给定任意一个数字 m,然后给出数字 n,则需在 m 中去掉 n 位数,保持各位顺序不变的情况下,得到最大数;

输入 1234 2

输出 34

因为定义的int类型的范围限制,m最多不超过10位数;

题目:

#include <stdio.h>
#include <Windows.h>
#include <vector>
//#include <iostream>
#include <string>
#include <algorithm>
#include <stdlib.h>


int solution(int m, int n){
	int result=0;
        //......输入程序
	return result;
}

int main()
{
	int m;
	int n;
	scanf("%d", &m);
	scanf("%d", &n);
	int result = solution(m, n);
	printf("%d\n", result);
	
	system("pause");
	return EXIT_SUCCESS;

}

因为题目给的是int类型的m,所以我只能写到10位数,如果超出则会出现错误结果,int 类型的范围是-2的31次方到2的31次方-1; 

解答方案: 

#include <stdio.h>
#include <Windows.h>
#include <vector>
//#include <iostream>
#include <string>
#include <algorithm>
#include <stdlib.h>

int fun(int n)
{
	if(n==0)
		return 1;
	if(n<0)
		return 0;
	return 10*fun(n-1);
}
int solution(int m, int n){
	int result=0;
	int i,len=0;
	char a[1024]={0};
	char b[1024]={0};
	char *p=b;
	for (i=0;m>0;i++)
	{
		a[i]=m%10;
		m=m/10;
	}
	if (i<n)
	{
		return 0;
	}
	len=i;
	for (i=0;i<len;i++)
	{
		b[i]=a[len-i-1];//b[0]=a[3] a[1]=a[2] a[2]=a[1] a[3]=a[0]
	}
	int x=n;
	while (n--)
	{
		int min=0;
		for (i=0;i<len;i++)
		{
			if(b[min]>b[i])
				min=i;
		}   
		for (i=min;i<len-1;i++)
		{
			b[i]=b[i+1];
		}
	}
	for (i=0;i<len-x;i++)
	{
		result+=b[i]*fun(len-x-1-i);
	}
	return result;
}

int main()
{
	int m;
	int n;
	while(1)
	{
	scanf("%d", &m);
	scanf("%d", &n);
	int result = solution(m, n);
	//printf("%d ",fun(2));
	printf("%d\n", result);
	}
	system("pause");
	return EXIT_SUCCESS;
	
}

通过while循环观察运行结果:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

干饭人小龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值