10.14,坐标:電子科技大學
24h,不間斷的程式設計,感覺還是很爽的。
排名一般,但是這是開始,未來還很遠。
題目舉例1:
廣袤的非洲大草原上,獅子居住在一個個的網格裡,他們的勢力範圍會以曼哈頓路程的的方式給出,求出哪個獅子重疊在其他獅子領地裡的次數最少。
代碼如下:
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<utility>
using namespace std;
int n, m, k, ri, ci, di;
int sign[][];
void draw() {
int i, j;
for (i = ri - di + ; i < ri+di; i++) {
for (j = ci - di + ; j < ci+di; j++) {
if (i < || j < || 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) >= ) sign[row1][ci]++;
if ((row2 = ri + di) < n) sign[row2][ci]++;
if ((col1 = ci - di) >= ) 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 = ;
while (i++ < k) {
cin >> ri >> ci >> di;
ri--; ci--;
row.push_back(ri);
col.push_back(ci);
draw();
}
i = ;
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, -);
sort(tar->second.begin(), tar->second.end());
cout << *(tar->second).begin() << " " << tar->first;
return ;
}
題目舉例2:
有一堆人,每個人都有身高,将人按身高排序,同樣身高的放到一行,同時按名字排序,每一行的最後輸出這一行人的始編号和末編号,
代碼如下:
#include<stdio.h>
#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<utility>
#include<algorithm>
using namespace std;
multimap<int, string>num;
int n;
int main() {
cin >> n;
int t = , height;
string s;
while (t < n) {
cin >> s >> height;
num.insert(make_pair(height, s));
t++;
}
vector<string>store[];
multimap<int, string>::iterator it = num.begin(), itt = num.begin();
int k = ;
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 = , init = ;
for (int j = ; 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+;
}
return ;
}
題目舉例3:
有一個n*n的地圖,你從(0,0)出發,目的地是(n,n), 一路上隻能像下走或者向右走,每個格子都有血藥或者毒藥,走上去會按藥的數量扣血或加血,計算開始時的應該有的最低血量。
代碼如下:
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
int r, c, health;
int rev_div[][] = { {-,},{,-} };
//vector<vector<int>>step;
struct {
int potion;
bool state = false;
}unit[][];
void init() {
int i = , j = ;
while (i < r) {
unit[i][].potion += unit[i + rev_div[][]][].potion;
i++;
}
while (j < c) {
unit[][j].potion += unit[][j + rev_div[][]].potion;
j++;
}
}
int main() {
cin >> r >> c;
for (int i = ; i < r; i++) {
for (int j = ; j < c; j++) {
cin >> unit[i][j].potion;
}
}
// struct type position;
init();
int mid = ;
while () {
int i = , j = ;
while (i < r) {
while (j < c) {
mid = unit[i + rev_div[][]][j + rev_div[][]].potion > unit[i + rev_div[][]][j + rev_div[][]].potion ? unit[i + rev_div[][]][j + rev_div[][]].potion : unit[i + rev_div[][]][j + rev_div[][]].potion;
unit[i][j].potion += mid;
j++;
}
j = ;
i++;
}
health = (unit[r-][c-].potion > ? : - unit[r-][c-].potion);
break;
}
cout << health;
return ;
}
題目舉例4:
這道題就是生命遊戲的實作,要求左邊界與右邊界相接,上邊界與下邊界相接,輸出疊代n次後的結果。
代碼如下:
1: 啰裡啰唆版:
#include<cstdio>
#include<iostream>
using namespace std;
char cell[][], new_cell[][];
int n, m, c;
int get_neigh(int row,int col) {
int count=, i, j;
//左上
if (row == && col == ) {
if (cell[][] == '*') count++;
if (cell[][] == '*') count++;
if (cell[][] == '*') count++;
if (cell[n-][] == '*') count++;
if (cell[n-][] == '*') count++;
if (cell[n-][m-] == '*') count++;
if (cell[][m-] == '*') count++;
if (cell[][m-] == '*') count++;
return count;
}
//左下
else if (row == n- && col == ) {
if (cell[n-][] == '*') count++;
if (cell[n-][] == '*') count++;
if (cell[n-][] == '*') count++;
if (cell[n - ][m-] == '*') count++;
if (cell[n - ][m-] == '*') count++;
if (cell[][] == '*') count++;
if (cell[][] == '*') count++;
if (cell[][m - ] == '*') count++;
return count;
}
//右上
else if (row == && col == m - ) {
if (cell[][] == '*') count++;
if (cell[][] == '*') count++;
if (cell[][m - ] == '*') count++;
if (cell[][m - ] == '*') count++;
if (cell[][m - ] == '*') count++;
if (cell[n - ][] == '*') count++;
if (cell[n - ][m - ] == '*') count++;
if (cell[n - ][m - ] == '*') count++;
return count;
}
//右下
else if (row == n- && col == m-) {
if (cell[][] == '*') count++;
if (cell[n-][m-] == '*') count++;
if (cell[n-][m - ] == '*') count++;
if (cell[n-][m - ] == '*') count++;
if (cell[n-][] == '*') count++;
if (cell[n - ][] == '*') count++;
if (cell[][m - ] == '*') count++;
if (cell[][m - ] == '*') count++;
return count;
}
//首行
else if (row == ) {
for (i = row; i <= row + ; i++) {
for (j = col - ; j <= col + ; j++) {
if (cell[i][j] == '*')
count++;
}
}
for (j = col - ; j <= col + ; j++) {
if (cell[n-][j] == '*')
count++;
}
if (cell[row][col] == '*')
count--;
return count;
}
//末行
else if (row == n - ) {
for (i = row-; i <= row; i++) {
for (j = col - ; j <= col + ; j++) {
if (cell[i][j] == '*')
count++;
}
}
for (j = col - ; j <= col + ; j++) {
if (cell[][j] == '*')
count++;
}
if (cell[row][col] == '*')
count--;
return count;
}
//首列
else if (col == ) {
for (i = row-; i <= row + ; i++) {
for (j = col ; j <= col + ; j++) {
if (cell[i][j] == '*')
count++;
}
}
for (i = row - ; i <= row + ; i++) {
if (cell[i][m-] == '*')
count++;
}
if (cell[row][col] == '*')
count--;
return count;
}
//末列
else if (col == m) {
for (i = row - ; i <= row + ; i++) {
for (j = col-; j <= col ; j++) {
if (cell[i][j] == '*')
count++;
}
}
for (i = row - ; i <= row + ; i++) {
if (cell[i][] == '*')
count++;
}
if (cell[row][col] == '*')
count--;
return count;
}
//一般
else {
for (i = row - ; i <= row + ; i++) {
for (j = col - ; j <= col + ; j++) {
if (cell[i][j] == '*')
count++;
}
}
if (cell[row][col] == '*')
count--;
return count;
}
}
int main() {
cin >> n >> m >> c;
int i, j;
for (i = ; i < n; i++) {
for (j = ; j < m; j++) {
cin >> cell[i][j];
}
}
int kase = ;
while (kase < c) {
for (i = ; i < n; i++)
{
for (j = ; j < m; j++)
{
switch (get_neigh(i, j))
{
case :
case :
case :
case :
case :
case :
case :
new_cell[i][j] = '-';
break;
case :
new_cell[i][j] = cell[i][j];
break;
case :
new_cell[i][j] = '*';
break;
}
}
}
kase++;
}
cout << endl;
for (i = ; i < n; i++) {
for (j = ; j < m; j++) {
cout << cell[i][j];
}
cout << endl;
}
return ;
}
代碼舉例2:
這個最後改的挺多的,其實按照第一個的大思路然後把拿這個的鄰居計算思路就行。
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
int n, m, c;
//擷取鄰居數;
int get_neigh(int row, int col, char (*cell)[]) {
int count = , i, j;
for (i = -; i <= ; i++) {
for (j = -; j <= ; j++) {
if (cell[(row + i + n) % n][(col + j + m) % m] == '*')
count++;
}
}
if (cell[row][col] == '*')
count--;
return count;
}
void change(char (*mat1)[], char (*mat2)[]) {
int i, j, num;
for (i = ; i < n; i++)
{
for (j = ; j < m; j++)
{
num = get_neigh(i, j, mat1);
if (num == ) mat2[i][j] = mat1[i][j];
else if (num == ) {
mat2[i][j] = '*';
}
else {
mat1[i][j] = '-';
}
}
}
}
int main() {
char cell[][], new_cell[][] = {};
cin >> n >> m >> c;
int i, j;
for (i = ; i < n; i++) {
for (j = ; j < m; j++) {
cin >> cell[i][j];
}
}
int kase = ;
while (kase <= c) {
if (kase % != )
change(cell, new_cell);
else
change(new_cell, cell);
kase++;
}
for (i = ; i < n; i++) {
for (j = ; j < m; j++) {
cout << new_cell[i][j];
}
cout << endl;
}
return ;
}
題目舉例5:有好多個長方形,每個長方形中間都會包一個小的長方形,大長方形已經内部已經一個一個小格的依次編号,題目會給出你小長方形的兩個對角線頂點,計算每個長方形在小長方形之外的所有數字的異或和
代碼如下:
#include<stdio.h>
#include<iostream>
#include<set>
#include<algorithm>
#include<cmath>
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[];
int main() {
cin >> t;
int i = , j = ;
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 + ) % rec[i].l;
a2 = (rec[i].d2 - rec[i].val + ) % rec[i].l;
b1 = (rec[i].d1 - rec[i].val + ) / rec[i].l;
b2 = (rec[i].d2 - rec[i].val + ) / 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) + ;
rec[i].width = abs(b1 - b2) + ;
i++;
}
set<int>::iterator it ,itt;
for (i = ; i < t; i++) {
j = ;
while (j < rec[i].l*rec[i].h) {
s.insert(rec[i].val++);
j++;
}
it = s.begin();
advance(it, rec[i].position-);
itt = it;
advance(it, rec[i].length);
for (j = ; 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 = ;
s.clear();
}
return ;
}
題目舉例6:求斐波那契數列的前n項值(這個n上界是22400)
代碼舉例:
#include <iostream>
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
struct Bigintegar {
static const int BASE = ;
static const int WIDTH = ;
vector<int>s;
Bigintegar(long long num = ) { *this = num; }
Bigintegar operator = (long long num) {
s.clear();
do {
s.push_back(num%BASE);
num /= BASE;
} while (num > );
return *this;
}
Bigintegar operator=(const string& str) {
s.clear();
int x, len = (str.length() - ) / WIDTH + ;
for (int i = ; i < len; i++) {
int end = str.length() - i*WIDTH;
int start = max(, 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 = , g = ;; i++) {
if (g == && 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() - ; i >= ; i--) {
char buf[];
sprintf(buf, "%08d", x.s[i]);
for (int j = ; j < strlen(buf); j++) out << buf[j];
}
return out;
}
Bigintegar fibMinus1 = ;
Bigintegar fibMinus2 = ;
Bigintegar fibN;
Bigintegar step(int n)
{
for (int i = ; i < n; i++)
{
fibN = fibMinus1 + fibMinus2;
fibMinus1 = fibMinus2;
fibMinus2 = fibN;
}
return fibN;
}
int main()
{
int s, kase;
int t, i = , k;
cin >> t;
while (i<t) {
cin >> k;
i++;
}
i = ;
}