華爲2021屆實習機試題

  • 筆試正確率:20% + 20% + 100%
  • 筆試分數:0.2 * 100 + 0.2 * 200 + 1* 300= 360分,過100分數線
  • 4.8號機試,目前很多題目細節不太記得了,是4月下旬面試前一晚翻之前的草稿紙才回憶起一些題目。再加上牛客網已經有大佬分享了部分題目,總結一下機試題目。

牛客網可以參考以下鏈接:
https://www.nowcoder.com/discuss/403185?type=post&order=time&pos=&page=1&channel=
https://www.nowcoder.com/discuss/403134?type=all&order=time&pos=&page=1&channel=

1. 快速冪

  • 描述某種情景(不泄露具體細節,其實也是忘了),題目進行總結髮現實質就是排列組合之後,求等比數列之和。
  • 但是數太大,需要對1e9+7取餘(又或者是1e8+7)。
  • 實際考察點:快速冪、大數冪。
  • 雖然劍指offer刷斐波那契那題有這種思想,但是考場緊張完全沒想到這一點。
  • 怕後面來不及,直接從數學角度分析快速的方法了,只AC20%。
int power(int a, int n, int mod)
{
	long long ans = 1;
	while (n > 0)
	{
		if (n & 1 == 1)
			ans = a * ans % mod;
		a = a * a % mod;
		n = n >> 1;
	}
	return ans;
}

2. 應該是一道錯題

題目說一個二進制串,可以進行一些字符的替換。
00 ⇒ 10
10 ⇒ 01
可以無限操作,求二進制串可以達到的最大值。

  • 這題搞了20多分鐘,一直卡在20%。
  • 下面有牛客網別人曬的思路正確代碼。

如果是00,直接替換爲10;
如果是11,不做修改;
如果是10,當前一個數也爲0,即010的情況,可以替換爲101,否則不做修改;
如果是01,同第3條。

作者:沉迷單車
鏈接:https://www.nowcoder.com/discuss/403185?type=post&order=time&pos=&page=1&channel=
來源:牛客網

typedef long long int ll;
 
int main(){
    int T;
    ll n;
    string s;
    cin>>T;
    for(int i=0;i<T;i++){
        cin>>n>>s;
        for(int j=0;j<n-2;j++){
            if(s[j]=='0'&&s[j+1]=='0'){
                s[j] = '1';
            }else if(s[j]=='0'&&s[j+1]=='1'&&s[j+2]=='0'){
                s[j]='1';
                s[j+1]='0';
                s[j+2]='1';
            }
        }
        if(s[n-2]=='0'&&s[n-1]=='0'){
            s[n-2]='1';
        }
        cout<<s<<endl;
    }
    return 0;
}

但是,這裏不是完全正確的。
比如,對於01110,這個結果還是01110。
但是,01110 => 01101 => 01011 => 00111 => 10111,顯然大於10111。

這裏貼一下我修改的代碼(也不一定對,輸入輸出格式忘記了,其他有錯誤可以指出)

int main()
{
	int n;
	string s;
	cin >> n >> s;
	int pLeft = 0;
	while (pLeft < n)
	{
		if (s[pLeft] == '1')
			pLeft++;
		else
		{
			int pRight = pLeft+1;
			while (pRight < n)
			{
				if (s[pRight] == '1')
					pRight++;
				else
				{
					s[pLeft] = '1';
					s[pRight] = '1';
					s[pLeft + 1] = '0';
					break;
				}
			}
			pLeft++;
		}
	}
	cout << s << endl;
	return 0;
}

3. 數獨
經典題。
我直接用暴力方法強解的,AC100%。

#define SIZE 9
bool processCoreForZeros(int nums[SIZE][SIZE], int begin, int& i, int &j)
{
	for (int ii = begin; ii < SIZE; ii++)
		for (int jj = 0; jj < SIZE; jj++)
		{
			if (0 == nums[ii][jj])
			{
				i = ii;
				j = jj;
				return true;
			}
		}
	return false;
}
bool processCoreForChange(int nums[SIZE][SIZE], int i, int j)
{
	int n = 0;
	while (n <= 8)
	{
		n++;
		bool find = false;
		for (int ii = 0; ii < 9; ii++)
		{
			if (nums[ii][j] == n)
			{
				find = true;
				break;
			}
		}
		if (find)
			continue;

		find = false;
		for (int jj = 0; jj < 9; jj++)
		{
			if (nums[i][jj] == n)
			{
				find = true;
				break;
			}
		}
		if (find)
			continue;

		find = false;
		for (int ii = (i / 3) * 3; ii < (i / 3) * 3 + 3; ii++)
		{
			if (find)
				break;
			for (int jj = (j / 3) * 3; jj < (j / 3) * 3 + 3; jj++)
			{
				if (nums[ii][jj] == n)
				{
					find = true;
					break;
				}
			}
		}

		if (find)
			continue;

		nums[i][j] = n;
		int new_i = 0, new_j = 0;
		if (processCoreForZeros(nums, i, new_i, new_j) == false)
			return true;
		if (processCoreForChange(nums, new_i, new_j) == false)
		{
			nums[i][j] = 0;
			continue;
		}
		else
			return true;

	}
	return false;
}
void processCore(int nums[SIZE][SIZE])
{
	int i = 0;
	int j = 0;
	int begin = 0;
	processCoreForZeros(nums, begin, i, j);
	processCoreForChange(nums, i, j);
}

int main()
{
	int nums[SIZE][SIZE];
	for (int i = 0; i < SIZE; i++)
	{
		getchar();
		for (int j = 0; j < SIZE; j++)
		{
			cin >> nums[i][j];
			getchar();
		}
		getchar();
	}
	processCore(nums);
	for (int i = 0; i < SIZE - 1; i++)
	{
		cout << "{";
		for (int j = 0; j < SIZE - 1; j++)
			cout << nums[i][j] << ',';
		cout << nums[i][SIZE - 1] << '}'<<endl;
	}
	cout << "{";
	for (int j = 0; j < SIZE - 1; j++)
		cout << nums[SIZE - 1][j] << ',';
	cout << nums[SIZE - 1][SIZE - 1] << '}';
	system("pause");
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章