1.最高分是多少
max_element函数
[first,last)
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<assert.h>
#include<math.h>
using namespace std;
int main(){
int m, n;
while (cin >> m >> n)
{
vector<int>Grade(m);
for (int i = 0; i < m; i++)
cin >> Grade[i];
char c; int a, b;
for (int j = 0; j < n; j++)
{
cin >> c >> a >> b;
if (c == 'Q')
{
if (a>b)swap(a, b);
cout << *max_element(Grade.begin() + a, Grade.begin() + b);
}
if (c == 'U')
Grade[a-1] = b;
}
}
return 0;
}
2.简单错误记录
查阅c++正则表达式
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<sstream>
#include<assert.h>
#include<math.h>
using namespace std;
bool compare(pair<string, int>a, pair<string, int>b){
return a.second > b.second;
}
int main(){
string input, file;
vector<pair<string, int>>errors;
while (getline(cin,input))
{
if (input.size() == 0)
break;
int f = input.rfind('\\');//找到最后一个
file = input.substr(f + 1);
errors.push_back(make_pair(file, 1));
for (int i = 0; i < (errors.size() - 1);i++)//再次合并,除了最后的自己
if (errors[i].first == file){
errors[i].second++;
errors.pop_back(); break;
}
}
stable_sort(errors.begin(), errors.end(), compare);
int idx = 0;
while (idx < 8 && idx < errors.size()){
string check = errors[idx].first;
int t = check.find(' ');
if (t>16)
errors[idx].first.erase(0, t - 16);
cout << errors[idx].first << ' ' << errors[idx].second << endl;
idx++;
}
return 0;
}
3.解数独 递归表示
#include <iostream>
using namespace std;
/* 构造完成标志 */
bool sign = false;
/* 创建数独矩阵 */
int num[9][9];
/* 函数声明 */
void Input();
void Output();
bool Check(int n, int key);
int DFS(int n);
/* 主函数 */
int main()
{
// cout << "请输入一个9*9的数独矩阵,空位以0表示:" << endl;
Input();
DFS(0);
Output();
system("pause");
}
/* 读入数独矩阵 */
void Input()
{
char temp[9][9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cin >> temp[i][j];
num[i][j] = temp[i][j] - '0';
}
}
}
/* 输出数独矩阵 */
void Output()
{
// cout << endl;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 8; j++)
{
cout << num[i][j] <<" ";
// if (j % 3 == 2)
//{
// cout << " ";
//}
}
cout<<num[i][8];
cout << endl;
//if (i % 3 == 2)
// {
// cout << endl;
// }
}
}
/* 判断key填入n时是否满足条件 */
bool Check(int n, int key)
{
/* 判断n所在横是否合法*/
for (int i = 0; i < 9; i++)
{
/* j为n竖坐标 */
int j = n / 9;
if (num[j][i] == key) return false;
}
/* 判断n所在竖是否合法, */
for (int i = 0; i < 9; i++)
{
/* j为n横坐标 */
int j = n % 9;
if (num[i][j] == key) return false;
}
/* 特殊的性质:x为n所在的 小九宫格 左顶点竖坐标 */
int x = n / 9 / 3 * 3;
/* y为n所在的小九宫格左顶点横坐标 */
int y = n % 9 / 3 * 3;
/* 判断n所在的小九宫格是否合法 */
for (int i = x; i < x + 3; i++)
{
for (int j = y; j < y + 3; j++)
{
if (num[i][j] == key) return false;
}
}
/* 全部合法,返回正确 */
return true;
}
/* 深搜构造数独 */
int DFS(int n)
{
/* 所有的都符合,退出递归 */
if (n > 80)
{
sign = true;
return 0;
}
/* 当前位不为空时跳过 */
if (num[n/9][n%9] != 0)
{
DFS(n+1);
}
else
{
/* 否则对当前位进行枚举测试 */
for (int i = 1; i <= 9; i++)
{
/* 满足条件时填入数字 */
if (Check(n, i) == true)
{
num[n/9][n%9] = i;
/* 继续搜索 */
DFS(n+1);
/* 返回时如果构造成功,则直接退出 */
if (sign == true)
return 0;
/* 如果构造不成功,还原当前位 */
num[n/9][n%9] = 0;
}
}
}
}
4.只用0交换排序
/**
* 交换数组里n和0的位置
* array: 存储[0-n)的数组
* len: 数组长度
* n: 数组里要和0交换的数
*/
extern void swap_with_zero(int* array, int len, int n);
class Solution {
public:
/**
* 调用方法swap_with_zero来对array进行排序
*/
int findI(int *array,int len,int ele)
{
for(int i=0;i<len;i++)
if(ele==array[i])return i;
return 0;
}
void sort(int* array, int len) {
for(int i=0;i<len;i++)
{
if(array[i]!=i)
{
int j=findI(array,len,array[array[i]]);
swap_with_zero(array,len,j);
swap_with_zero(array,len,i);
}
}
}
};
更多华为题目来刷,我正在 https://www.nowcoder.com/ta/huawei?page=0