1.女生数量SQL (ac)
select subi.name subject_name, count(*) girl_count from Subject_Register sr join Student_Info si on sr.student_id = si.id join Subject_Info subi on sr.subject_id = subi.id where si.gender = 2 group by subi.name order by girl_count desc limit 0,3;
2. 波峰波谷 AC
#include <bits/stdc++.h>
using namespace std;
void solve(vector<int>& nums)
{
int len = nums.size();
if(len < 2)
{
cout << 0 << endl;
return;
}
bool up = nums[1] > nums[0];
int minnum = nums[0], maxnum = nums[0];
int res = 0;
for(int i = 1; i < len; i++)
{
if(nums[i] < nums[i-1])
{
if(up == false)
{
minnum = min(minnum, nums[i]);
maxnum = max(maxnum, nums[i]);
res = max(res, abs(maxnum-minnum));
}else{
up = false;
maxnum = nums[i-1];
minnum = nums[i];
res = max(res, abs(maxnum-minnum));
}
}else{
if(up == true)
{
minnum = min(minnum, nums[i]);
maxnum = max(maxnum, nums[i]);
res = max(res, abs(maxnum-minnum));
}else{
up = true;
maxnum = nums[i];
minnum = nums[i-1];
res = max(res, abs(maxnum-minnum));
}
}
}
cout << res << endl;
}
int main()
{
string str;
while(cin >> str)
{
vector<int> nums;
int cur = 0;
int num = 0;
while(cur < str.size())
{
if(str[cur] == ',')
{
nums.push_back(num);
num = 0;
}else{
num = num*10+str[cur]-'0';
}
cur++;
}
if(num!=0)
nums.push_back(num);
solve(nums);
nums.clear();
}
return 0;
} 3. N皇后(做了多次,直接背过了,就是输入输出处理了好久,AC)
#include <bits/stdc++.h>
using namespace std;
vector<string> createBoard(vector<int>& queens, int n)
{
vector<string> board;
for(int i = 0; i < n; i++)
{
string row(n, '.');
row[queens[i]] = 'Q';
board.emplace_back(row);
}
return board;
}
void dfs(vector<vector<string>>& res, vector<int>& queens, int n, int row, unordered_set<int>& cols,
unordered_set<int> diags1,unordered_set<int> diags2)
{
// 递归终止
if(row == n)
{
auto board = createBoard(queens, n);
res.push_back(board);
}else{
for(int i = 0; i < n; i++)
{
if(cols.count(i))
continue;
int diag1 = row - i;
if(diags1.count(diag1))
continue;
int diag2 = row + i;
if(diags2.count(diag2))
continue;
queens[row] = i;
cols.insert(i);
diags1.insert(diag1);
diags2.insert(diag2);
dfs(res,queens,n,row+1,cols,diags1,diags2);
queens[row] = -1;
cols.erase(i);
diags1.erase(diag1);
diags2.erase(diag2);
}
}
}
vector<vector<string>> solve(int n)
{
vector<vector<string>> res;
vector<int> queens(n,-1);
unordered_set<int> cols;
unordered_set<int> diag1;
unordered_set<int> diag2;
dfs(res, queens, n, 0, cols, diag1, diag2);
return res;
}
int main()
{
string str;
cin >> str;
int len = str.size() - 2;
str = str.substr(2,len);
int N = stoi(str);
vector<vector<string>> res = solve(N);
cout << "[";
for(int k = 0; k < res.size(); k++)
{
auto& vec = res[k];
cout << "[";
for(int i = 0; i < vec.size()-1; i++)
cout << vec[i] << ", ";
cout << vec[vec.size()-1];
cout << "]";
if(k != res.size()-1)
cout << ", ";
}
cout << "]";
return 0;
} 4. 多线程输出。 #include <condition_variable>
#include <functional>
#include <iostream>
#include <memory>
#include <mutex>
#include <thread>
using namespace std;
class ZeroAndEvenOdd {
private:
bool ze; // 输出zero
bool other; // 输出其他的
int n; // 总共要输出的次数
int count; // 输出总量
int othercount; // 输出非0的总量
std::condition_variable cv;
std::mutex mtx;
public:
ZeroAndEvenOdd(int m) : ze(true), other(false), n(m), count(0), othercount(0){
}
// printNumber(x) outputs "x", where x is an integer.
void printZero(std::function<void(int)> printNumber) {
while(true)
{
unique_lock<mutex> lock(mtx);
if(count >= 2*n)
return;
while(!ze)
cv.wait(lock);
printNumber(0);
count++;
ze = false;
other = true;
cv.notify_all();
lock.unlock();
}
}
void printEven(std::function<void(int)> printNumber) {
while(true)
{
unique_lock<mutex> lock(mtx);
if(count >= 2*n)
return;
while(!other)
cv.wait(lock);
printNumber(++othercount);
count++;
ze = true;
other = false;
cv.notify_all();
lock.unlock();
}
}
void printOdd(std::function<void(int)> printNumber) {
while(true)
{
unique_lock<mutex> lock(mtx);
if(count >= 2*n)
return;
while(!other)
cv.wait(lock);
printNumber(++othercount);
count++;
ze = true;
other = false;
cv.notify_all();
lock.unlock();
}
}
};
void printNumber(int i) {
std::cout << i;
}
int main(int argc, char** argv) {
int n = 0;
std::cin >> n;
ZeroAndEvenOdd zeo(n);
std::thread th1 = std::thread(std::bind(&ZeroAndEvenOdd::printZero, &zeo, std::placeholders::_1), printNumber);
std::thread th2 = std::thread(std::bind(&ZeroAndEvenOdd::printEven, &zeo, std::placeholders::_1), printNumber);
std::thread th3 = std::thread(std::bind(&ZeroAndEvenOdd::printOdd, &zeo, std::placeholders::_1), printNumber);
th1.join();
th2.join();
th3.join();
std::cout << std::endl;
return 0;
}

全部评论
(3) 回帖