10.14,坐标:电子科技大学
24h,不间断的编程,感觉还是很爽的。
排名一般,但是这是开始,未来还很远。
题目举例1:
广袤的非洲大草原上,狮子居住在一个个的网格里,他们的势力范围会以曼哈顿路程的的方式给出,求出哪个狮子重叠在其他狮子领地里的次数最少。
代码如下:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int n, m, k, ri, ci, di;
int sign[1000][1000];
void draw() {
int i, j;
for (i = ri - di + 1; i < ri+di; i++) {
for (j = ci - di + 1; j < ci+di; j++) {
if (i < 0 || j < 0 || i >= n || j >= m)
continue;
if (i == ri&&j == ci) {
sign[i][j]--;
}
sign[i][j]++;
}
}
int row1, row2, col1, col2;
if ((row1 = ri - di) >= 0) sign[row1][ci]++;
if ((row2 = ri + di) < n) sign[row2][ci]++;
if ((col1 = ci - di) >= 0) sign[ri][col1]++;
if ((col2 = ci + di) < m) sign[ri][col2]++;
}
int main() {
vector<int>row, col;
map<int, vector<int>>s;
cin >> n >> m >> k;
int i = 0;
while (i++ < k) {
cin >> ri >> ci >> di;
ri--; ci--;
row.push_back(ri);
col.push_back(ci);
draw();
}
i = 0;
vector<int>::iterator it = row.begin(), ittt = col.begin();
set<int>ss;
while (i++ < k) {
s.insert(make_pair(sign[*it][*ittt], i));
it++;
ittt++;
}
map<int, vector<int>>::iterator tar = s.end();
advance(tar, -1);
sort(tar->second.begin(), tar->second.end());
cout << *(tar->second).begin() << " " << tar->first;
return 0;
}
题目举例2:
有一堆人,每个人都有身高,将人按身高排序,同样身高的放到一行,同时按名字排序,每一行的最后输出这一行人的始编号和末编号,
代码如下:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
multimap<int, string>num;
int n;
int main() {
cin >> n;
int t = 0, height;
string s;
while (t < n) {
cin >> s >> height;
num.insert(make_pair(height, s));
t++;
}
vector<string>store[1000];
multimap<int, string>::iterator it = num.begin(), itt = num.begin();
int k = 0;
for (; it != num.end(); it++) {
if (it->first == itt->first) {
store[k].push_back(it->second);
}
else {
k++;
itt = it;
store[k].push_back(it->second);
}
}
vector<string>::iterator iet;
int time = 0, init = 1;
for (int j = 0; j <= k; j++) {
sort(store[j].begin(), store[j].end());
for (iet = store[j].begin(); iet != store[j].end(); iet++) {
cout << *iet << " ";
time++;
}
printf("%d %d\n", init, time);
init = time+1;
}
return 0;
}
题目举例3:
有一个n*n的地图,你从(0,0)出发,目的地是(n,n), 一路上只能像下走或者向右走,每个格子都有血药或者毒药,走上去会按药的数量扣血或加血,计算开始时的应该有的最低血量。
代码如下:
#include
#include
#include
using namespace std;
int r, c, health;
int rev_div[2][2] = { {-1,0},{0,-1} };
//vector>step;
struct {
int potion;
bool state = false;
}unit[1000][1000];
void init() {
int i = 0, j = 0;
while (i < r) {
unit[i][0].potion += unit[i + rev_div[0][0]][0].potion;
i++;
}
while (j < c) {
unit[0][j].potion += unit[0][j + rev_div[1][1]].potion;
j++;
}
}
int main() {
cin >> r >> c;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> unit[i][j].potion;
}
}
// struct type position;
init();
int mid = 0;
while (1) {
int i = 1, j = 1;
while (i < r) {
while (j < c) {
mid = unit[i + rev_div[0][0]][j + rev_div[0][1]].potion > unit[i + rev_div[1][0]][j + rev_div[1][1]].potion ? unit[i + rev_div[0][0]][j + rev_div[0][1]].potion : unit[i + rev_div[1][0]][j + rev_div[1][1]].potion;
unit[i][j].potion += mid;
j++;
}
j = 1;
i++;
}
health = (unit[r-1][c-1].potion > 1 ? 1 : 1 - unit[r-1][c-1].potion);
break;
}
cout << health;
return 0;
}
题目举例4:
这道题就是生命游戏的实现,要求左边界与右边界相接,上边界与下边界相接,输出迭代n次后的结果。
代码如下:
1: 啰里啰唆版:
#include
#include
using namespace std;
char cell[26][26], new_cell[26][26];
int n, m, c;
int get_neigh(int row,int col) {
int count=0, i, j;
//左上
if (row == 0 && col == 0) {
if (cell[0][1] == '*') count++;
if (cell[1][1] == '*') count++;
if (cell[1][0] == '*') count++;
if (cell[n-1][0] == '*') count++;
if (cell[n-1][1] == '*') count++;
if (cell[n-1][m-1] == '*') count++;
if (cell[0][m-1] == '*') count++;
if (cell[1][m-1] == '*') count++;
return count;
}
//左下
else if (row == n-1 && col == 0) {
if (cell[n-2][0] == '*') count++;
if (cell[n-1][1] == '*') count++;
if (cell[n-2][1] == '*') count++;
if (cell[n - 1][m-1] == '*') count++;
if (cell[n - 2][m-1] == '*') count++;
if (cell[0][0] == '*') count++;
if (cell[0][1] == '*') count++;
if (cell[0][m - 1] == '*') count++;
return count;
}
//右上
else if (row == 0 && col == m - 1) {
if (cell[0][0] == '*') count++;
if (cell[1][0] == '*') count++;
if (cell[0][m - 2] == '*') count++;
if (cell[1][m - 2] == '*') count++;
if (cell[1][m - 1] == '*') count++;
if (cell[n - 1][0] == '*') count++;
if (cell[n - 1][m - 2] == '*') count++;
if (cell[n - 1][m - 1] == '*') count++;
return count;
}
//右下
else if (row == n-1 && col == m-1) {
if (cell[0][0] == '*') count++;
if (cell[n-1][m-2] == '*') count++;
if (cell[n-2][m - 2] == '*') count++;
if (cell[n-2][m - 1] == '*') count++;
if (cell[n-1][0] == '*') count++;
if (cell[n - 2][0] == '*') count++;
if (cell[0][m - 2] == '*') count++;
if (cell[0][m - 1] == '*') count++;
return count;
}
//首行
else if (row == 0) {
for (i = row; i <= row + 1; i++) {
for (j = col - 1; j <= col + 1; j++) {
if (cell[i][j] == '*')
count++;
}
}
for (j = col - 1; j <= col + 1; j++) {
if (cell[n-1][j] == '*')
count++;
}
if (cell[row][col] == '*')
count--;
return count;
}
//末行
else if (row == n - 1) {
for (i = row-1; i <= row; i++) {
for (j = col - 1; j <= col + 1; j++) {
if (cell[i][j] == '*')
count++;
}
}
for (j = col - 1; j <= col + 1; j++) {
if (cell[0][j] == '*')
count++;
}
if (cell[row][col] == '*')
count--;
return count;
}
//首列
else if (col == 0) {
for (i = row-1; i <= row + 1; i++) {
for (j = col ; j <= col + 1; j++) {
if (cell[i][j] == '*')
count++;
}
}
for (i = row - 1; i <= row + 1; i++) {
if (cell[i][m-1] == '*')
count++;
}
if (cell[row][col] == '*')
count--;
return count;
}
//末列
else if (col == m) {
for (i = row - 1; i <= row + 1; i++) {
for (j = col-1; j <= col ; j++) {
if (cell[i][j] == '*')
count++;
}
}
for (i = row - 1; i <= row + 1; i++) {
if (cell[i][0] == '*')
count++;
}
if (cell[row][col] == '*')
count--;
return count;
}
//一般
else {
for (i = row - 1; i <= row + 1; i++) {
for (j = col - 1; j <= col + 1; j++) {
if (cell[i][j] == '*')
count++;
}
}
if (cell[row][col] == '*')
count--;
return count;
}
}
int main() {
cin >> n >> m >> c;
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cin >> cell[i][j];
}
}
int kase = 0;
while (kase < c) {
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
switch (get_neigh(i, j))
{
case 0:
case 1:
case 4:
case 5:
case 6:
case 7:
case 8:
new_cell[i][j] = '-';
break;
case 2:
new_cell[i][j] = cell[i][j];
break;
case 3:
new_cell[i][j] = '*';
break;
}
}
}
kase++;
}
cout << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << cell[i][j];
}
cout << endl;
}
return 0;
}
代码举例2:
这个最后改的挺多的,其实按照第一个的大思路然后把拿这个的邻居计算思路就行。
#include
#include
#include
using namespace std;
int n, m, c;
//获取邻居数;
int get_neigh(int row, int col, char (*cell)[26]) {
int count = 0, i, j;
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
if (cell[(row + i + n) % n][(col + j + m) % m] == '*')
count++;
}
}
if (cell[row][col] == '*')
count--;
return count;
}
void change(char (*mat1)[26], char (*mat2)[26]) {
int i, j, num;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
num = get_neigh(i, j, mat1);
if (num == 2) mat2[i][j] = mat1[i][j];
else if (num == 3) {
mat2[i][j] = '*';
}
else {
mat1[i][j] = '-';
}
}
}
}
int main() {
char cell[26][26], new_cell[26][26] = {};
cin >> n >> m >> c;
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cin >> cell[i][j];
}
}
int kase = 1;
while (kase <= c) {
if (kase % 2 != 0)
change(cell, new_cell);
else
change(new_cell, cell);
kase++;
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << new_cell[i][j];
}
cout << endl;
}
return 0;
}
题目举例5:有好多个长方形,每个长方形中间都会包一个小的长方形,大长方形已经内部已经一个一个小格的依次编号,题目会给出你小长方形的两个对角线顶点,计算每个长方形在小长方形之外的所有数字的异或和
代码如下:
#include
#include
#include
#include
#include
using namespace std;
long long xor1;
int t;
set<int>s;
struct {
int l, h, d1, d2;
long long val;
int length, width, position;
}rec[100];
int main() {
cin >> t;
int i = 0, j = 0;
while (i < t) {
cin >> rec[i].l >> rec[i].h >> rec[i].val >> rec[i].d1 >> rec[i].d2;
int a1, a2, b1, b2, min1, min2;
a1 = (rec[i].d1 - rec[i].val + 1) % rec[i].l;
a2 = (rec[i].d2 - rec[i].val + 1) % rec[i].l;
b1 = (rec[i].d1 - rec[i].val + 1) / rec[i].l;
b2 = (rec[i].d2 - rec[i].val + 1) / rec[i].l;
min1 = a1 > a2 ? a2 : a1;
min2 = b1 > b2 ? b2 : b1;
rec[i].position = rec[i].l*min2 + min1;
rec[i].length = abs(a1 - a2) + 1;
rec[i].width = abs(b1 - b2) + 1;
i++;
}
set<int>::iterator it ,itt;
for (i = 0; i < t; i++) {
j = 0;
while (j < rec[i].l*rec[i].h) {
s.insert(rec[i].val++);
j++;
}
it = s.begin();
advance(it, rec[i].position-1);
itt = it;
advance(it, rec[i].length);
for (j = 0; j < rec[i].width; j++) {
s.erase(itt, it);
itt = it;
advance(it, rec[i].l);
advance(itt, rec[i].l - rec[i].length);
}
itt = it = s.begin();
for (; it != s.end(); it++) {
xor1 ^= *it;
}
cout << xor1 << endl;
xor1 = 0;
s.clear();
}
return 0;
}
题目举例6:求斐波那契数列的前n项值(这个n上界是22400)
代码举例:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Bigintegar {
static const int BASE = 100000000;
static const int WIDTH = 8;
vector<int>s;
Bigintegar(long long num = 0) { *this = num; }
Bigintegar operator = (long long num) {
s.clear();
do {
s.push_back(num%BASE);
num /= BASE;
} while (num > 0);
return *this;
}
Bigintegar operator=(const string& str) {
s.clear();
int x, len = (str.length() - 1) / WIDTH + 1;
for (int i = 0; i < len; i++) {
int end = str.length() - i*WIDTH;
int start = max(0, end - WIDTH);
sscanf(str.substr(start, end - start).c_str(), "%d", &x);
s.push_back(x);
}
return *this;
}
Bigintegar operator + (const Bigintegar& b)const {
Bigintegar c;
c.s.clear();
for (int i = 0, g = 0;; i++) {
if (g == 0 && i >= s.size() && i >= b.s.size()) break;
int x = g;
if (i < s.size())x += s[i];
if (i < b.s.size())x += b.s[i];
c.s.push_back(x%BASE);
g = x / BASE;
}
return c;
}
};
ostream& operator<<(ostream &out, const Bigintegar& x) {
out << x.s.back();
for (int i = x.s.size() - 2; i >= 0; i--) {
char buf[20];
sprintf(buf, "%08d", x.s[i]);
for (int j = 0; j < strlen(buf); j++) out << buf[j];
}
return out;
}
Bigintegar fibMinus1 = 1;
Bigintegar fibMinus2 = 2;
Bigintegar fibN;
Bigintegar step(int n)
{
for (int i = 2; i < n; i++)
{
fibN = fibMinus1 + fibMinus2;
fibMinus1 = fibMinus2;
fibMinus2 = fibN;
}
return fibN;
}
int main()
{
int s, kase;
int t, i = 0, k;
cin >> t;
while (icin >> k;
i++;
}
i = 0;
}