1/8 [編程題]魔法幣
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof(a))
#define line cout<<"------------"<<endl
typedef long long ll;
const int maxn = 1e6 + 10;
int a[maxn];
int main(){
// freopen("data.txt", "r", stdin);
ll n;
scanf("%lld", &n);
int cnt = 0;
while(n){
if(n%2 == 1){
n = (n-1)/2;
a[cnt++] = 1;
//cout << "1" << endl;
}
else {
n = (n-2)/2;
a[cnt++] = 2;
//cout << "2" << endl;
}
}
for(int i = cnt-1; i >= 0; i--){
printf("%d", a[i]);
}
return 0;
}
2/8 [編程題]相反數
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof(a))
#define line cout<<"------------"<<endl
typedef long long ll;
const int maxn = 1e6 + 10;
int a[maxn];
int main(){
// freopen("data.txt", "r", stdin);
int n;
while(scanf("%d", &n) != EOF){
int a[6], len = 0, tem = n;
while(n){
a[len++] = n % 10;
n /= 10;
}
int tes = 0;
for(int i = 0; i < len; i++){
tes = tes * 10 + a[i];
}
cout << tes + tem << endl;
}
return 0;
}
3/8 [編程題]字符串碎片
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof(a))
#define line cout<<"------------"<<endl
typedef long long ll;
const int maxn = 1e6 + 10;
int main(){
char s[110];
gets(s);
int len = strlen(s);
int a[110], cnt = 0;
for(int i = 0; i < len; i++){
int id = i;
while(s[id] == s[i]) id++;
a[cnt++] = id-i;
i = id-1;
}
//cout << cnt << endl;
double ans = (double)(len) / (double)(cnt);
printf("%.2lf\n", ans);
return 0;
}
4/8 [編程題]遊歷魔法王國
#include<bits/stdc++.h>
using namespace std;
#define line cout << "-----------" << endl;
typedef long long ll;
const int maxn = 2e5 + 10;
const int MAXN = 1e6 + 10;
const int N = 2020;
int main(){
int n, m;
scanf("%d%d", &n, &m);
int pre[maxn], de[maxn];
int maxx = 0;
for(int i = 1; i < n; i++){
scanf("%d", &pre[i]);
de[i] = de[pre[i]] + 1;
if(de[i] > maxx) maxx = de[i];
}
int cnt = 0;
if(maxx >= m) cnt = m;
else cnt = (m-maxx) / 2 + maxx;
if(cnt >= n-1) cnt = n - 1;
cout << cnt + 1 << endl;
return 0;
}
5/8 [編程題]重排數列
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof(a))
#define line cout<<"------------"<<endl
typedef long long ll;
const int maxn = 1e5 + 10;
const int N = 55;
int a[maxn], b[maxn];
int main(){
int T, n;
scanf("%d", &T);
while(T--){
clr(a); clr(b);
scanf("%d", &n);
int cnt0 = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt = 0;
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
int x = a[i];
while(x % 2 == 0){
b[i] ++;
x /= 2;
}
//cout << b[i] << endl;
if(b[i] == 0) cnt0++;
else if(b[i] == 1) cnt1++;
else if(b[i] > 1){
if(b[i] % 2 == 0) cnt2 ++;
else cnt3++;
cnt++;
}
}
bool flag = true;
cnt1 = cnt1 % 2;
//printf("%d %d %d \n", cnt0, cnt1, cnt);
if(cnt1 + cnt0 - 1 > cnt) flag = false;
if(flag) printf("Yes\n");
else printf("No\n");
}
return 0;
}
6/8 [編程題]最長公共子括號序列
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof(a))
#define line cout<<"------------"<<endl
typedef long long ll;
const int maxn = 1e5 + 10;
const int N = 55;
char s[N];
bool check(string s){
int n1 = 0, n2 = 0;
for(int i = 0; i < s.length(); i++){
if(s[i] == '(') n1++;
else n2++;
if(n1 < n2) return false;
}
return true;
}
int main(){
string s;
set<string> se;
cin >> s;
for(int i = 0; i < s.length(); i++){
string str = s;
str.erase(i, 1);
for(int j = 0; j < s.length()-1; j++){
string ss = str;
ss.insert(j, 1, s[i]);
if(check(ss))
se.insert(ss);
}
}
printf("%d\n", se.size()-1);
return 0;
}
7/8 [編程題]合唱
#include<bits/stdc++.h>
using namespace std;
#define line cout << "-----------" << endl;
typedef long long ll;
const int maxn = 2e5 + 10;
const int MAXN = 1e6 + 10;
const int N = 2020;
int n;
int a[N], dp[N][N];
int abs(int x, int y){
return x > y ? x - y : y - x;
}
int main(){
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < n; i++) dp[i][n-1] = 0;
for(int i = 0; i < n; i++) dp[n-1][i] = 0;
int next, res, fir;
for(int i = n-2; i >= 0; i--){
for(int j = n-2; j >= 0; j--){
next = max(i, j) + 1;
dp[i][j] = min( (dp[next][j]+abs(a[next], a[i])), (dp[i][next]+abs(a[next], a[j])) );
}
}
res = dp[0][1];
fir = 0;
for(int i = 1; i < n-1; i++){
fir += abs(a[i-1], a[i]);
res = min(res, dp[i][i+1]+fir);
}
printf("%d\n", res);
return 0;
}
8/8 [編程題]射擊遊戲
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof(a))
#define line cout<<"------------"<<endl
typedef long long ll;
const int maxn = 1e5 + 10;
const int N = 55;
struct node{
int x, y;
}p[N];
int x[N], y[N];
int main(){
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d", &p[i].x), x[i] = p[i].x;
for(int i = 0; i < n; i++) scanf("%d", &p[i].y), y[i] = p[i].y;
int maxx = 0;
if(n < 4) maxx = n;
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
int x1 = p[j].x - p[i].x;
int y1 = p[j].y - p[i].y;
for(int k = 0; k < n; k++){
if(k == i || k == j) continue;
int cnt = 3;
for(int l = 0; l < n; l++){
if(l == i || l == j || l == k) continue;
int x2 = p[l].x - p[k].x;
int y2 = p[l].y - p[k].y;
int x3 = p[l].x - p[i].x;
int y3 = p[l].y - p[i].y;
if(x1*x2 + y1*y2 == 0 || x1*y3 == x3*y1)
cnt++;
}
if(cnt > maxx) maxx = cnt;
}
}
}
printf("%d\n", maxx);
return 0;
}