ACM集訓第一次選拔AC程式碼(2018)
阿新 • • 發佈:2018-11-25
比賽連結:
https://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/problemlist/cid/2703
下面寫一下自己的解題報告和感受:
目錄:
A - 雙色Hanoi塔問題
#include <bits/stdc++.h> using namespace std; void move(int n, char a, char b, char c); int main() { int n; cin >> n; move(n, 'A', 'B', 'C'); cout << endl; return 0; } void move(int n, char a, char b, char c) { if (n == 1) printf("%d %c %c\n", n, a, b); else { move(n - 1, a, c, b); printf("%d %c %c\n", n, a, b); move(n - 1, c, b, a); } }
B - 進位制轉換
#include <bits/stdc++.h> using namespace std; int main() { long long n; int r; char s[6] = {'A', 'B', 'C', 'D', 'E', 'F'}; while (~scanf("%lld %d", &n, &r)) { int cnt = 0, flag = 0; char str[100000]; if (n < 0) { n = abs(n); flag = 1; } else if (n == 0) flag = 2; for (int i = 0;; i++) { if (n == 0) break; int t = n % r; if (t >= 10) str[i] = s[t - 10]; else str[i] = t + '0'; n /= r; cnt++; } if (flag == 2) printf("0"); else if (flag == 1) { printf("-"); for (int i = cnt - 1; i >= 0; i--) putchar(str[i]); } else { for (int i = cnt - 1; i >= 0; i--) putchar(str[i]); } printf("\n"); } return 0; }
C - 質數中的質數
#include <bits/stdc++.h> using namespace std; //尤拉篩 int p, prime[10000001]; bool isprime[10000001]; void sieve() { p = 1; for (int i = 0; i <= 1e7; i++) isprime[i] = true; isprime[0] = false, isprime[1] = false; for (int i = 2; i <= 1e7; i++) { if (isprime[i]) { prime[p++] = i; for (int j = 2 * i; j <= 1e7; j += i) isprime[j] = false; } } } int main() { int n, i; sieve(); while (~scanf("%d", &n)) { for (i = 1; i < p; i++) { if (prime[i] >= n && isprime[i]) break; } printf("%d\n", prime[i]); } return 0; }
D - 王小二切餅
#include <bits/stdc++.h>
using namespace std;
int main()
{
int dp[111];
int n;
cin >> n;
dp[1] = 2;
for (int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + i;
cout << dp[n] << endl;
return 0;
}
E - 組合數的計算
#include <bits/stdc++.h> //動態規劃寫法
using namespace std;
long long c[45][45];
int main()
{
int n, a, b;
scanf("%d", &n);
for (int i = 0; i <= 41; i++)
c[i][0] = 1;
for (int i = 1; i <= 41; i++)
for (int j = 1; j <= i; j++)
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
while (n--)
{
scanf("%d %d", &a, &b);
printf("%lld\n", c[a][b]);
}
return 0;
}
F - 完美字串
#include <bits/stdc++.h>
using namespace std;
int main()
{
char str[10001];
while (gets(str))
{
int ans = 26, a[100] = {0};
for (int i = 0; i < strlen(str); i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
str[i] -= 32;
a[str[i]]++;
}
int sum = 0;
sort(a + 65, a + 91);
for (int i = 90; i >= 65; i--)
{
if (a[i] == 0)
continue;
else
{
sum += a[i] * ans;
ans--;
}
}
printf("%d\n", sum);
}
return 0;
}
G - 取數字問題
#include <bits/stdc++.h>
using namespace std; //暴力搜尋
int m, n, a[11][11];
int ans = 501966782;
void search(int i, int j, int sum)
{
sum += a[i][j];
if (i < m)
search(i + 1, j, sum);
if (j < n)
search(i, j + 1, sum);
if (i == m && j == n && sum < ans && sum > 0)
ans = sum;
}
int main()
{
scanf("%d%d", &m, &n);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
search(1, 1, 0);
if (ans == 501966782)
ans = -1;
printf("%d\n", ans);
return 0;
}
H - 皮卡丘的夥伴是?
#include <bits/stdc++.h>
using namespace std;
int isok(char *s1, char *s2)
{
if (strcmp("Water", s1) == 0)
{
if (strcmp("Fire", s2) == 0)
return 1;
if (strcmp("Grass", s2) == 0 || strcmp(s1, s2) == 0)
return 0;
}
else if (strcmp("Fire", s1) == 0)
{
if (strcmp("Grass", s2) == 0)
return 1;
if (strcmp("Water", s2) == 0 || strcmp(s1, s2) == 0)
return 0;
}
else if (strcmp("Grass", s1) == 0)
{
if (strcmp("Water", s2) == 0)
return 1;
if (strcmp("Fire", s2) == 0 || strcmp(s1, s2) == 0)
return 0;
}
else if (strcmp("Electric", s1) == 0)
{
if (strcmp("Water", s2) == 0)
return 1;
if (strcmp("Grass", s2) == 0 || strcmp(s1, s2) == 0)
return 0;
}
return -1;
}
int main()
{
char s1[10], s2[10], cc[10];
float a, b, c;
while (~scanf("%s %s", s1, s2))
{
scanf("%f %f %s", &a, &b, cc);
a *= 1.5;
if (isok(s1, s2) == 1)
{
a *= 2;
if (strcmp("Normal", cc) == 0 || isok(cc, s2) == -1)
c = 60.0;
else if (isok(cc, s2) == 1)
c = 60 * 2.0;
else if (isok(cc, s2) == 0)
c = 60.0 / 2;
}
else if (isok(s1, s2) == 0)
{
a /= 2;
if (strcmp("Normal", cc) == 0 || isok(cc, s2) == -1)
c = 60.0;
else if (isok(cc, s2) == 1)
c = 60 * 2.0;
else if (isok(cc, s2) == 0)
c = 60.0 / 2;
}
else if (isok(s1, s2) == -1)
{
if (strcmp("Normal", cc) == 0 || isok(cc, s2) == -1)
c = 60.0;
else if (isok(cc, s2) == 1)
c = 60 * 2.0;
else if (isok(cc, s2) == 0)
c = 60.0 / 2;
}
float num[4];
int cnt[4] = {0, 1, 2, 3};
num[cnt[1]] = a, num[cnt[2]] = b, num[cnt[3]] = c;
for (int i = 1; i < 3; i++)
{
for (int j = i + 1; j <= 3; j++)
{
if (num[i] < num[j])
{
float t = num[i];
num[i] = num[j];
num[j] = t;
int o = cnt[i];
cnt[i] = cnt[j];
cnt[j] = o;
}
}
}
printf("%d\n", cnt[1]);
}
return 0;
}