Missing number

一、题目

Given a positive integer n(n≤40), pick n-1 numbers randomly from 1 to n and concatenate them in random order as a string s, which means there is a missing number between 1 and n. Can you find the missing number?(Notice that in some cases the answer will not be unique, and in these cases you only need to find one valid answer.)

Examples:
 

  Input: 20
         81971112205101569183132414117
  Output: 16

二、题意

给定正整数n(n≤40),从1到n中随机选择n-1个数,并将它们以随机顺序连接为字符串s,这意味着在1和n之间有一个缺失的数字。你能找到那个缺失的数字吗?(请注意在某些情况下答案不唯一,此时你只需要找到一个有效的答案。)

三、解题思路

分支限界+回溯

归根结底本题时要找到字符串s的合理划分方法,因此可以用回溯法求解。假设字符串s长度为n,则当第i(i

因此划分和回溯就出来了:

1、当前字符下标等于n时,划分结束,判断当前是否找到了n-1个并输出

2、当前字符下标等于n-1时,只有单独划分的情况,递归调用(下标+1)

3、当前字符下标小于n-1时,有两种划分情况:

     1) 单独划分,递归调用(下标+1)

     2) 与后一个一起相组划分,递归调用(下标+2)

#include 
using namespace std;
int a[41] = {0};   // 记录这些数字有没有被找到 
bool flag = 0;     // 标记找到没,只找一个数 
void find(int findnum, string s, int index, int n){
	int length = s.length();
	if(flag == 1) return;
	if(index == length){
		// 判断有无符合要求的返回
		if(findnum == n-1){
			for(int i=1; i<=n; i++)
				if(a[i] == 0)
					cout<>n;
	string s;
	cin>>s;
	find(0, s, 0, n);
	
	return 0;
} 

参考博文:https://blog.csdn.net/climber16/article/details/81604342

你可能感兴趣的:(考研/保研机试题)