- 排序的奶牛题
关于字典序:按照字典中出现的顺序排列,两个字符从第一个字符开始比较,若第一个字符不行等,则第一个字典序小;若相等,比较下一个字符,若某一字符串没有下一个字符了,则该字符串字典序较小。
举个例子:a < ab < abc < b < ba < bc;
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
char l[2010];
int n;
int main()
{
cin >> n;
int num = 0;
for(int i = 0; i < n; i++) cin >> l[i];
for(int i = 0, j = n - 1; i <= j; ){
int le = i, ri = j;//le表示左,ri表示右;先记录比到了哪个位置;
while(l[le] == l[ri]){//处理两边相等的情况, 继续比较下一位;
le ++;
ri --;
}
if(l[le] < l[ri]) cout << l[i ++], le ++; //找到较小的打印出来;
else cout << l[j --], ri--;
num ++;
if(num > 0 && num % 80 == 0) cout << endl;//这是个坑;
}
return 0;
}
//开始不清楚为什么答案不是ABBCCD,纠结了好久(还以为是字典序的问题),后来发现题没有读明白,血的教训,以后一定要认真读题;
- 棋盘挑战(8皇后的变形)
采用暴搜方法,用数组存储状态
#include <iostream>
#include <algorithm>
using namespace std;
int n;
bool a[15], b[30], c[30];
int path[15], ans;
void dfs(int x)
{
if(x > n){//都可以填完,意味着存在的情况;
ans ++;//记录方案数量;
if(ans <= 3){
for(int i = 1; i <= n; i++){
cout << path[i] << ' ';
}
cout << endl;
}
return;
}
for(int y = 1; y <= n; y ++){
if(!a[y] && !b[x + y] && !c[x - y + n]){
path[x] = y;
a[y] = b[x + y] = c[x - y + n] = true;//标记横纵及对角线(用截距来表示)
dfs(x + 1);//填下一个
a[y] = b[x + y] = c[x - y + n] = false;//更新一下
path[x] = 0;
}
}
}
int main()
{
cin >> n;
dfs(1);
cout << ans << endl;
return 0;
}
- 复习高精度,通过一步一步模拟来完成,贴一下模板吧
高精度 + 高精度
vector<int> add(vector<int> A, vector<int> B)
{
if(A.size() < B.size()) return add(B, A);
vector<int> C;
int t = 0;
for(int i = 0; i < A.size(); i++)
{
t += A[i];
if(i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if(t) C.push_back(t);
return C;
}
高精度-高精度
vector<int> sub(vector<int> A, vector<int> B)
{
vector<int> C;
int t = 0;
for(int i = 0; i < A.size(); i++)
{
t += A[i];
if(i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if(t < 0) t = -1;
else t = 0;
}
while(C.size() > 1 && C.back() == 0) C.pop_back();//避免出现0001这种情况,但是保证如果结果为0时,
return C;
}
高精度 * 低精度
vector<int> mul(vector<int> A, int b)
{
vector<int> C;
int t = 0;
for(int i = 0; i < A.size() || t; i++)
{
if(i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
高精度 / 低精度
vector<int> div(vector<int> A, int b, int &r)
{
vector<int> C;
r = 0;
for(int i = A.size() - 1; i >= 0; i--)
{
r = r * 10 + A[i];
C.push_back(r / b);
r = r % b;
}
reverse(C.begin(), C.end());
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}