2026.5.28
T765218 卡牌游戏I
题意
有一种卡牌游戏,玩家可以在
目的
求出当前回合小来会赢的概率是多少。
思路
用三层嵌套循环就出赢的组合数和输的组合数,最后照输出要求输出赢的组合数
代码
#include <bits/stdc++.h>
using namespace std;
int a[105];
double k, n, ans = 0, cnt = 0;
void dfs(int x, int y, int z)
{
if(x > n || y > n || z > n)
{
return ;
}
if(a[x] > k || a[y] > k || a[z] > k)
{
ans++;
}
for(int i = x + 1; i <= n && i != y && i != z; i++)
{
dfs(i, y, z);
}
for(int i = y + 1; i <= n && i != x && i != z; i++)
{
dfs(x, i, z);
}
for(int i = z + 1; i <= n && i != x && i != y; i++)
{
dfs(x, y, i);
}
cnt++;
}
int main()
{
cin >> k >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
dfs(1, 2, 3);
cout << fixed << setprecision(2) << (ans / cnt * 100.0) << '%';
return 0;
}
AC 代码
#include <bits/stdc++.h>
using namespace std;
int a[105];
int main()
{
int k, n;
double win = 0, all = 0;
cin >> k >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
for(int i = 1; i <= n; i++)
{
for(int j = i + 1; j <= n; j++)
{
for(int l = j + 1; l <= n; l++)
{
if(a[i] > k || a[j] > k || a[l] > k)
{
win++;
}
all++;
}
}
}
cout << fixed << setprecision(2) << win / all * 100.0 << '%';
return 0;
}
错因
思路错误。
T670770 小田喜欢数字
题意
小田在一块巨大的黑板上从左往右写数字。
- 一开始,小田写下了数字 1。
- 接着,小田又写下了数字 1 和 2。
- 然后,小田又写下了数字 1,2,3。
… - 最后,小田写下了数字 1,2,…,
n 。
目的
给一个喜欢的数字列表
思路
遍历数组
代码
#include <bits/stdc++.h>
using namespace std;
int a[205];
int main()
{
int T;
cin >> T;
while(T--)
{
int n, m, l, r, maxx = 0;
bool b = 1;
cin >> n >> m;
cin >> r;
maxx = max(maxx, r);
for(int i = 2; i <= m; i++)
{
l = r;
cin >> r;
if(l >= r)
{
b = 0;
}
maxx = max(maxx, r);
}
if(!b)
{
cout << 1 << '\n';
}
else
{
cout << n - maxx + 1 << '\n';
}
}
return 0;
}
T752535 简单的子序列
题意
这是一个由
目的
输出最小长度的
思路
计算出
代码
无……
AC 代码
#include <bits/stdc++.h>
using namespace std;
long long a[int(3e5) + 5];
int main()
{
int T;
cin >> T;
while(T--)
{
long long n, k = 0;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
for(int i = 1; i < n; i++)
{
k += abs(a[i] - a[i + 1]);
}
if(n == 1 || !k)
{
cout << 1 << '\n';
continue;
}
int cnt = 2;
short oper = 2;
for(int i = 1; i < n; i++)
{
if(oper == 2)
{
oper = a[i] < a[i + 1];
}
else if(oper != a[i] < a[i + 1] && a[i] != a[i + 1])
{
cnt++;
oper = a[i] < a[i + 1];
}
}
cout << cnt << '\n';
}
return 0;
}