害 瞎寫一通玩玩 不用放在心上
以下均為自己寫題過程中的代碼,不是最優解,也不是標准答案,歡迎大佬指正錯誤QAQ
我居然寫着寫着睡着了QAQ
A
括號匹配 我是把所有情況都枚舉出來再判斷
問題描述
由1對括號,可以組成一種合法括號序列:()。
由2對括號,可以組成兩種合法括號序列:()()、(())。
由4對括號組成的合法括號序列一共有多少種?
答案提交
這是一道結果填空的題,你只需要算出結果后提交即可。本題的結果為一個整數,在提交答案時只填寫這個整數,填寫多余的內容將無法得分。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#include <stack>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
// 長度一共為 8
bool check(string str) {
// 待匹配的左括號數量
int left = 0;
for (int i = 0;i < str.size() ;i ++) {
char c = str[i];
if (c == '(')
left++;
else // 遇到右括號
left--;
if (left < 0)
return false;
}
return left == 0;
}
int a = 4 , b = 4;
void dfs(string s, int k)
{
if(k > 8)return;
if(k == 8)
{
if(check(s)){
cout << s << endl;
}
return;
}
if(a > 0)
{
a--;
dfs(s + '(', k + 1);
a++;
}
if(b > 0)
{
b--;
dfs(s + ')', k + 1);
b++;
}
}
int main()
{
SIS;
dfs("",0);
return 0;
}
B
問題描述
在計算機存儲中,12.5MB是多少字節?
答案提交
這是一道結果填空的題,你只需要算出結果后提交即可。本題的結果為一個整數,在提交答案時只填寫這個整數,填寫多余的內容將無法得分。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int main()
{
12.5 * 1024 *1024;
return 0;
}
C
暴力枚舉
問題描述
將LANQIAO中的字母重新排列,可以得到不同的單詞,如LANQIAO、AAILNOQ等,注意這7個字母都要被用上,單詞不一定有具體的英文意義。
請問,總共能排列如多少個不同的單詞。
答案提交
這是一道結果填空的題,你只需要算出結果后提交即可。本題的結果為一個整數,在提交答案時只填寫這個整數,填寫多余的內容將無法得分。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <map>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
string str = "LANQIAO";
bool vis[7];
int ans = 0;
map<string , bool> m;
void dfs(string s,int k)
{
if(s.size() == str.size())
{
if(!m[s])
{
m[s] = 1;
ans++;
}
return;
}
for(int i = 0;i < str.size() ;i ++)
{
if(!vis[i])
{
vis[i] = 1;
dfs(s + str[i] , k + 1);
vis[i] = 0;
}
}
}
int main()
{
dfs("", 0);
cout << ans << endl;
return 0;
}
D
問題描述
一個包含有2019個結點的無向連通圖,最少包含多少條邊?
答案提交
這是一道結果填空的題,你只需要算出結果后提交即可。本題的結果為一個整數,在提交答案時只填寫這個整數,填寫多余的內容將無法得分。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int main()
{
2018
return 0;
}
E
模擬
問題描述
給定一個單詞,請使用凱撒密碼將這個單詞加密。
凱撒密碼是一種替換加密的技術,單詞中的所有字母都在字母表上向后偏移3位后被替換成密文。即a變為d,b變為e,...,w變為z,x變為a,y變為b,z變為c。
例如,lanqiao會變成odqtldr。
輸入格式
輸入一行,包含一個單詞,單詞中只包含小寫英文字母。
輸出格式
輸出一行,表示加密后的密文。
樣例輸入
lanqiao
樣例輸出
odqtldr
評測用例規模與約定
對於所有評測用例,單詞中的字母個數不超過100。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
void work(string str)
{
string t;
int s = int('a');
int e = int('z');
for(int i = 0;i < str.size() ;i ++)
{
if(str[i] >= 'a' && str[i] <= 'z')
{
char ch = str[i];
int tmp = int(ch) + 3;
if(tmp > e)
{
tmp = tmp - e + s - 1;
}
t += (char)tmp;
}else t += str[i];
}
cout << t << endl;
}
int main()
{
SIS;
string s;
getline(cin, s);
work(s);
return 0;
}
F
模擬
問題描述
給定三個整數 a, b, c,如果一個整數既不是 a 的整數倍也不是 b 的整數倍還不是 c 的整數倍,則這個數稱為反倍數。
請問在 1 至 n 中有多少個反倍數。
輸入格式
輸入的第一行包含一個整數 n。
第二行包含三個整數 a, b, c,相鄰兩個數之間用一個空格分隔。
輸出格式
輸出一行包含一個整數,表示答案。
樣例輸入
30
2 3 6
樣例輸出
10
樣例說明
以下這些數滿足要求:1, 5, 7, 11, 13, 17, 19, 23, 25, 29。
評測用例規模與約定
對於 40% 的評測用例,1 <= n <= 10000。
對於 80% 的評測用例,1 <= n <= 100000。
對於所有評測用例,1 <= n <= 1000000,1 <= a <= n,1 <= b <= n,1 <= c <= n。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , a , b , c , ans;
int main()
{
cin >> n;
cin >> a >> b >> c;
for(int i = 1;i <= n ;i ++)
{
if((i%a != 0) &&(i%b!=0) &&(i%c!=0))
{
ans++;
}
}
cout << ans << endl;
return 0;
}
G
應該是個dp題 無奈太菜了不想去轉換玄學方程了 太困了
問題描述
如果一個序列的奇數項都比前一項大,偶數項都比前一項小,則稱為一個擺動序列。即 a[2i]<a[2i-1], a[2i+1]>a[2i]。
小明想知道,長度為 m,每個數都是 1 到 n 之間的正整數的擺動序列一共有多少個。
輸入格式
輸入一行包含兩個整數 m,n。
輸出格式
輸出一個整數,表示答案。答案可能很大,請輸出答案除以10000的余數。
樣例輸入
3 4
樣例輸出
14
樣例說明
以下是符合要求的擺動序列:
2 1 2
2 1 3
2 1 4
3 1 2
3 1 3
3 1 4
3 2 3
3 2 4
4 1 2
4 1 3
4 1 4
4 2 3
4 2 4
4 3 4
評測用例規模與約定
對於 20% 的評測用例,1 <= n, m <= 5;
對於 50% 的評測用例,1 <= n, m <= 10;
對於 80% 的評測用例,1 <= n, m <= 100;
對於所有評測用例,1 <= n, m <= 1000。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1005;
const int mod = 10000;
int n , m;
// now 表示前一個是第幾位
// num 表示當前數字是什么
int f[N][N];
int dfs(int now,int num)
{
if(f[now][num] != 0)return f[now][num];
if(now == m)return 1;
int ans = 0;
if(now & 1) // 如果前一位是奇數位
{
for(int i = num - 1; i >= 1; i--)
{
ans =(ans + dfs(now + 1, i)) % mod;
}
}else {
for(int i = num + 1; i <= n ;i ++)
{
ans =(ans + dfs(now + 1, i)) % mod;
}
}
return f[now][num] = ans % mod;
}
int main()
{
cin >> m >> n;
int ans = 0;
for(int i = 2;i <= n ;i ++)
{
ans = (ans + dfs(1 , i)) % mod;
}
cout << ans << endl;
return 0;
}
H
模擬
問題描述
對於一個 n 行 m 列的表格,我們可以使用螺旋的方式給表格依次填上正整數,我們稱填好的表格為一個螺旋矩陣。
例如,一個 4 行 5 列的螺旋矩陣如下:
1 2 3 4 5
14 15 16 17 6
13 20 19 18 7
12 11 10 9 8
輸入格式
輸入的第一行包含兩個整數 n, m,分別表示螺旋矩陣的行數和列數。
第二行包含兩個整數 r, c,表示要求的行號和列號。
輸出格式
輸出一個整數,表示螺旋矩陣中第 r 行第 c 列的元素的值。
樣例輸入
4 5
2 2
樣例輸出
15
評測用例規模與約定
對於 30% 的評測用例,2 <= n, m <= 20。
對於 70% 的評測用例,2 <= n, m <= 100。
對於所有評測用例,2 <= n, m <= 1000,1 <= r <= n,1 <= c <= m。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1005;
int f[N][N];
int n , m , r , c;
void work()
{
int k = n * m;
int id = 1;
// a 表示第一行
// b 表示第一列
// c 表示第 n 行
// d 表示第 m 列
int a = 1, b = 1, c = n,d = m;
while(id <= k)
{
// 從左往右
for(int i = b; i <= d ;i ++)
{
f[a][i] = id;
id++;
} a++;
for(int i = a; i <= c ;i ++)
{
f[i][d] = id;
id++;
}d--;
for(int i = d;i >= b ;i --)
{
f[c][i] = id;
id++;
}c--;
for(int i = c ;i >= a; i--)
{
f[i][b] = id;
id++;
}b++;
}
}
int main()
{
cin >> n >> m >> r >> c;
work();
/*for(int i = 1;i <= n ;i ++)
{
for(int j = 1;j <= m ;j ++)
{
cout << f[i][j] << "\t";
}
cout << endl;
}*/
cout << f[r][c];
return 0;
}
I
猜測是貪心 所以將樹苗按照半徑大小進行排序,優先考慮較大的樹苗,將和樹苗沖突的樹苗都排除掉就是最終答案,注意最終結果去掉Π的 在中間計算過程直接計算好加上就好了
問題描述
小明和朋友們一起去郊外植樹,他們帶了一些在自己實驗室精心研究出的小樹苗。
小明和朋友們一共有 n 個人,他們經過精心挑選,在一塊空地上每個人挑選了一個適合植樹的位置,總共 n 個。他們准備把自己帶的樹苗都植下去。
然而,他們遇到了一個困難:有的樹苗比較大,而有的位置挨太近,導致兩棵樹植下去后會撞在一起。
他們將樹看成一個圓,圓心在他們找的位置上。如果兩棵樹對應的圓相交,這兩棵樹就不適合同時植下(相切不受影響),稱為兩棵樹沖突。
小明和朋友們決定先合計合計,只將其中的一部分樹植下去,保證沒有互相沖突的樹。他們同時希望這些樹所能覆蓋的面積和(圓面積和)最大。
輸入格式
輸入的第一行包含一個整數 n ,表示人數,即准備植樹的位置數。
接下來 n 行,每行三個整數 x, y, r,表示一棵樹在空地上的橫、縱坐標和半徑。
輸出格式
輸出一行包含一個整數,表示在不沖突下可以植樹的面積和。由於每棵樹的面積都是圓周率的整數倍,請輸出答案除以圓周率后的值(應當是一個整數)。
樣例輸入
6
1 1 2
1 4 2
1 7 2
4 1 2
4 4 2
4 7 2
樣例輸出
12
評測用例規模與約定
對於 30% 的評測用例,1 <= n <= 10;
對於 60% 的評測用例,1 <= n <= 20;
對於所有評測用例,1 <= n <= 30,0 <= x, y <= 1000,1 <= r <= 1000。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1005;
int n;
struct node{
int x, y , r;
}a[N];
bool cmp(node a,node b)
{
return a.r > b.r;
}
double getdis(int sx,int sy,int ex,int ey)
{
int a = (sx-ex)*(sx-ex);
int b = (sy-ey)*(sy-ey);
return sqrt(a + b);
}
bool vis[N];
int main()
{
SIS;
cin >> n;
for(int i = 1;i <= n ;i ++)
{
cin >> a[i].x >> a[i].y >> a[i].r;
}
sort(a + 1, a + 1 + n , cmp);
int ans = 0;
// 優先種植半徑最大的
for(int i = 1;i <= n ;i ++)
{
if(!vis[i])
{
ans += a[i].r * a[i].r;
for(int j = i + 1;j <= n ;j ++)
{
double dis = getdis(a[i].x,a[i].y,a[j].x,a[j].y);
if(dis < a[i].r + a[j].r)// 表示這一棵樹苗不能放
{
vis[j] = 1;
}
}
}
}
cout << ans << endl;
return 0;
}
J
最小生成樹 但是需要自己構造邊而已,應該大家一眼就能看出來最小生成樹吧,但是有個坑卡了我好久 讀題不仔細 我以為計算權值的時候要把h的差的平方也給放進去呢 坑了我好半天 於是我就睡着了。。。再讀題才發現
問題描述
2015年,全中國實現了戶戶通電。作為一名電力建設者,小明正在幫助一帶一路上的國家通電。
這一次,小明要幫助 n 個村庄通電,其中 1 號村庄正好可以建立一個發電站,所發的電足夠所有村庄使用。
現在,這 n 個村庄之間都沒有電線相連,小明主要要做的是架設電線連接這些村庄,使得所有村庄都直接或間接的與發電站相通。
小明測量了所有村庄的位置(坐標)和高度,如果要連接兩個村庄,小明需要花費兩個村庄之間的坐標距離加上高度差的平方,形式化描述為坐標為 (x_1, y_1) 高度為 h_1 的村庄與坐標為 (x_2, y_2) 高度為 h_2 的村庄之間連接的費用為
sqrt((x_1-x_2)(x_1-x_2)+(y_1-y_2)(y_1-y_2))+(h_1-h_2)*(h_1-h_2)。
在上式中 sqrt 表示取括號內的平方根。請注意括號的位置,高度的計算方式與橫縱坐標的計算方式不同。
由於經費有限,請幫助小明計算他至少要花費多少費用才能使這 n 個村庄都通電。
輸入格式
輸入的第一行包含一個整數 n ,表示村庄的數量。
接下來 n 行,每個三個整數 x, y, h,分別表示一個村庄的橫、縱坐標和高度,其中第一個村庄可以建立發電站。
輸出格式
輸出一行,包含一個實數,四舍五入保留 2 位小數,表示答案。
樣例輸入
4
1 1 3
9 9 7
8 8 6
4 5 4
樣例輸出
17.41
評測用例規模與約定
對於 30% 的評測用例,1 <= n <= 10;
對於 60% 的評測用例,1 <= n <= 100;
對於所有評測用例,1 <= n <= 1000,0 <= x, y, h <= 10000。
prime版本
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1005;
const int MAX = 0x3fffff;
struct node{
int x, y ,h;
}a[N];
int n;
double e[N][N] , dis[N] , ans;
bool vis[N];
void prime()
{
dis[1] = 0;
for(int i = 1; i < n; i++)
{
int x = 0;
for(int j = 1; j <= n; j++)
{
if(!vis[j] && (x == 0 || dis[j] < dis[x]))
x = j;
}
vis[x] = 1;
for(int y = 1; y <= n; y++)
{
if(!vis[y])
{
dis[y] = min(dis[y], e[x][y]);
}
}
}
for(int i = 2; i <= n; i++) ans += dis[i];
printf("%.2f", ans);
}
double getdis(int sx, int sy ,int sh ,int ex,int ey,int eh)
{
return sqrt((sx-ex)*(sx-ex) + (sy-ey)*(sy-ey)) + (sh-eh)*(sh-eh);
}
void getedge()
{
// 計算出來每條邊
for(int i = 1;i <= n - 1;i ++)
{
for(int j = i + 1;j <= n ;j ++)
{
double t =
getdis(a[i].x,a[i].y,a[i].h,a[j].x,a[j].y,a[j].h);
e[i][j] = e[j][i] = min(e[i][j],t);
}
}
}
void init()
{
for(int i = 0;i <= n ;i ++)
{
for(int j = 0;j <= n ;j ++)
{
e[i][j] = MAX;
}
}
for(int i = 0;i <= n ;i ++)dis[i] = MAX;
}
int main()
{
SIS;
cin >> n;
init();
for(int i = 1;i <= n ;i ++)
{
cin >> a[i].x >> a[i].y >> a[i].h;
}
getedge();
prime();
return 0;
}
kuskal版本
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1005;
const int MAX = 0x3fffff;
struct node{
int x, y , h;
}a[N];
int n , f[N];
struct edge{
int a, b;
double v;
}e[N*N];
int cnt = 0; // 記錄邊的數量
int find(int k)
{
if(k == f[k])return k;
else return f[k] = find(f[k]);
}
double getdis(int sx, int sy ,int sh ,int ex,int ey,int eh)
{
return sqrt((sx-ex)*(sx-ex) + (sy-ey)*(sy-ey)) + (sh-eh)*(sh-eh);
}
void getedge()
{
for(int i = 1;i <= n - 1;i ++)
{
for(int j = i + 1;j <= n ;j ++)
{
double t =
getdis(a[i].x,a[i].y,a[i].h,a[j].x,a[j].y,a[j].h);
e[cnt].a = i;e[cnt].b = j;e[cnt].v = t;
cnt++;
}
}
}
bool cmp(edge a,edge b)
{
return a.v < b.v;
}
void kuskal()
{
for(int i = 1;i <= n ;i ++)f[i] = i;
sort(e , e + cnt , cmp);
int t = 0;double ans = 0;
for(int i = 0;i < cnt ;i ++)
{
int x = e[i].a;
int y = e[i].b;
if(x == y)continue;
ans += e[i].v;
f[x] = y;
t++;
if(t == n - 1)break;
}
printf("%.2f\n",ans);
}
void input()
{
cin >> n;
for(int i = 1;i <= n ;i ++)
{
cin >> a[i].x >> a[i].y >> a[i].h;
}
getedge();
}
int main()
{
SIS;
input();
kuskal();
return 0;
}
