天天看點

【iHooya】2023年2月1日作業解析

【iHooya】2023年2月1日作業解析
【iHooya】2023年2月1日作業解析
【iHooya】2023年2月1日作業解析
#include <bits/stdc++.h>
using namespace std;
//a存放皇後位置,b标記皇後是否被占用,c,d表示對角線
int a[9], b[9], c[17], d[17];
int sum = 0;

void output()
{
	int i;
	sum++;
	cout << "sum=" << sum << endl;
	for (int i = 1; i <= 8; i++)
		cout <<<< " " << a[i];
	cout << endl;
}

void search(int i)//第i行開始
{
	int j;
	for (j = 1; j <= 8; j++) //每行的列上有8個皇後可以選擇
	{
		if ((b[j] == 0) && c[i + j] == 0 && (d[i - j + 7]) == 0) //行,列,對角線沒有别的皇後
		{
			a[i] = j; //第i行第j列儲存了皇後
			b[j] = 1; //第j列被占領了
			c[i + j] = 1; //占領對角線
			d[i - j + 7] = 1; //占領對角線
			if (i == 8)
				output();
			else
				search(i + 1);
			b[j] = 0;
			c[i + j] = 0;
			d[i - j + 7] = 0;
		}
	}
}

int main()
{
	search(1);

	return 0;
}
           

單詞排序

輸入一行單詞序列,相鄰單詞之間由1個或多個空格間隔,請按照字典序輸出這些單詞,要求重複的單詞隻輸出一次。(區分大小寫)

輸入:一行單詞序列,最少1個單詞,最多100個單詞,每個單詞長度不超過50,單詞之間用至少1個空格間隔。資料不含除字母、空格外的其他字元。

輸出:按字典序輸出這些單詞,重複的單詞隻輸出一次。

樣例輸入She wants to go to Peking University to study Chinese

樣例輸出

Chinese

Peking

She

University

go

study

to

wants

分割字元模版不變,對使用sort對動态數組進行排序。

#include <bits/stdc++.h>
using namespace std;

int main()
{
	string s;
	getline(cin, s);
	vector<string> word;//建立string類型動态數組用來存單詞
	int x = 0;
	s = s + ' '; //字元串後加一個空格用做最後一個單詞的判斷
	for (int a = 0; a < s.length(); a++)
	{
		if (s[a] == ' ') //當遇到空格就說周遊完一個單詞了
		{
			word.push_back(s.substr(x, a - x)); //提取單詞放進動态數組中
			x = a + 1;
		}
	}
	sort(word.begin(), word.end());
	for (int a = 0; a < word.size(); a++)
		if ((word[a] != word[a - 1]) && (word[a].empty() != true))
			cout << word[a] << endl;

	return 0;
}