• 题目描述
  • As Dr. Orooji is getting older, he is becoming more attached to older items and has difficulty letting go of them (he claims they have historical value). For example, he still has the first table he got for the programming team! The situation is the same at home, e.g., there is a broken TV remote control but Dr. O still uses it, because he considers it an old item with historical value!
    The old remote control has 12 buttons: digits 0-9, channel down, and channel up. There are no other buttons on the remote control. Some digits on the remote don’t work but channel up/down always works. So, to get to a particular channel, Dr. O sometimes has to use the channel up/down.

  • For example, let’s assume digits 0 and 5 on the remote don’t work:
    If Dr. O wants to watch channel 102, he would select 99 and then “channel up” 3 times.
    If he wants to watch channel 597, he would select 611 and then “channel down” 14 times.

  • Given the digits that do not work and a target channel, determine how many times Dr. O needs to hit channel up or down. Dr. O, of course, wants to exert the least energy, hence he wants to hit the channel up/down the minimum number of times. Assume that Dr. O will enter a channel between 0 and 999 (inclusive) to start and that channel down has no effect at 0 and channel up has no effect at 999.

  • 输入

  • The first input line contains an integer, n (1 ≤ n ≤ 9), indicating how many digits on the remote do not work. These broken digits are listed (in increasing order) on the same input line. The second input line provides the target channel (an integer between 1 and 999, inclusive).

  • 输出

  • The output consists of a single integer, indicating how many times Dr. O needs to hit channel up/down. Note that, since one or more digits work, it is always possible to reach the target channel.

  • 样例输入 Copy

  • 4 1 2 5 9
    250

  • 样例输出 Copy
    50

  • 题意:给出一个电视台0-999 ;还有一个遥控器 有0-9 和+1,-1 这12个按钮。让后有n个数字按钮是坏的,所以只能通过其他数字+1或者-1来达到目的。问最小按几下?
  • 思路:因为是0-999 所以直接枚举,两种情况:1,大于等于0,小于等于m。2,大于等于m,小于等于999;这样从m往外扩就可以找到两边最小步数了。之后就是判断函数了 就是判断开始数字每一位是不是毁掉的那几个数字;没有就可以,有就不可以。
  • 之所写这个题是因为这个题坑太多了,这个题到时没啥难度。一不小心就掉坑里了(具体现在代码中吧)
  • 坑一,初始化 没有 res=maxn,ans=-maxn; 这个应该是过了70多;
  • 坑二,考虑m为999的情况
  • 坑三 ,考虑m为0的情况 这个应该是 过了97
    代码:
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include <algorithm>
#include <bits/stdc++.h>
#define ll long long int
#define sc(a) scanf("%lld",&a)
const int mod=998244353;
using namespace std;
const int inf=1e9+7;
const int maxn=2e5+10;

ll n,m, sum,t,p,res;
ll a[maxn],b[maxn],c[maxn];
ll x,y,ans;
ll dp[110][110];
ll maxx=-1,num=-1;
char str[maxn],s[maxn],ss[maxn];

int f(int x){
	if(x==0){//坑三 为零时直接判断,负责直接不进行判数了
	 for(int i=1;i<=n;i++)
		if(x==a[i])
		{
			return 0;
		}
	}
	while(x){
		int y=x%10,temp=1;
		for(int i=1;i<=n;i++)
			if(y==a[i]){
				temp=0;
				break;
			}
		if(temp==0) return 0;
		x=x/10;
	}
	return 1;
}

int main(){
	scanf("%lld",&n);
	res=maxn,ans=-maxn;//初始化避免出现上限不可以,或者下限不可以实现
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
	}
	scanf("%lld",&m);
	for(int i=m;i>=0;i--)//下限
	{
		if(f(i)){//得到一个可行的就直接退出,因为后面的肯定比他步数多
		ans=i;
		break;
		}
	}
	for(int i=m;i<=999;i++)//上限
	{
		if(f(i)){//得到一个可行的就直接退出,因为后面的肯定比他步数多
			res=i;
			break;
		}
	}
	cout<<min((res-m),(m-ans));//比较哪个最优
	return 0;
}

ps :考虑问题,要全面,细心吧!加油!!!