【CPE】2023-05-23 紀錄

CPE(大學程式能力檢定)

第一次考CPE,考前沒時間準備所以基本上就是裸考。

隨便寫寫,做了4題

pA - UVA11934

題意:

給你一個二次函數、一個除數 $d$ 及 $L$,求 $f(0)$ 到 $f(L)$ 的值有幾個可被 $d$ 整除

$-1000 \le a,b,c \le 1000, 1<d<10^6, 0 \le L < 1000$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'

int f(int a, int b, int c, int x) { return a * x * x + b * x + c; }
signed main() {
  int a, b, c, d, L;
  while (cin >> a >> b >> c >> d >> L) {
    if (a == 0 && b == 0 && c == 0 && d == 0 && L == 0)
      break;
    int ans = 0;
    for (int i = 0; i <= L; i++) {
      if (f(a, b, c, i) % d == 0)
        ans++;
    }
    cout << ans << endl;
  }
}

pB - UVA11349

題意:

給一個方矩陣問是否為對稱矩陣。

矩陣最大不超過 100*100

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'

signed main() {
  int t;
  cin >> t;
  for (int _ = 1; _ <= t; _++) {
    string tmp;
    cin >> tmp >> tmp;
    int n;
    cin >> n;
    vector<int> v(n * n + 1);
    for (int i = 0; i < n * n; i++)
      cin >> v[i];
    bool flag = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (v[i * n + j] != v[(n - i - 1) * n + (n - j - 1)] ||
            v[i * n + j] < 0) {
          flag = false;
          break;
        }
      }
    }

    cout << "Test #" << _ << ": ";
    if (flag)
      cout << "Symmetric." << endl;
    else
      cout << "Non-symmetric." << endl;
  }
}

pC - UVA834

題意:

給一個分數請轉成連分數,為了保證唯一性,規定最後一項必須大於 $1$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'

vector<int> ans;
void dfs(int n, int d) {
  if (d == 0)
    return;
  ans.push_back(n / d);
  dfs(d, n % d);
}
signed main() {
  int n, d;
  while (cin >> n >> d) {
    ans.clear();
    dfs(n, d);
    cout << "[" << ans[0] << ";";
    for (int i = 1; i < ans.size(); i++) {
      cout << ans[i];
      cout << ",]"[i == ans.size() - 1];
    }
    cout << endl;
  }
}

pD - UVA12455

題意:

有好幾根已知長度的金屬棒,問能不能選幾根焊起來長度剛好為所需的長度

裸裸的01背包問題,但因為根數只有20,用位元枚舉或Dynamic Programming都能解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'

signed main() {
  int t;
  cin >> t;
  while (t--) {
    int n, p;
    cin >> n >> p;
    vector<int> v(p);
    for (int i = 0; i < p; i++)
      cin >> v[i];
    bool flag = false;
    for (int i = 0; i < (1 << p); i++) {
      int sum = 0;
      for (int j = 0; j < p; j++) {
        if ((i >> j) & 1)
          sum += v[j];
      }
      if (sum == n) {
        flag = true;
        break;
      }
    }
    if (flag)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}