题目https://codeforc.es/contest/1520
a
一个字符串,出现在一个地方的字符,不能再第二个地方出现
A
A
A
A
Z
A
A
A
A
A
AAAAZAAAAA
AAAAZAAAAA -------NO
F
F
G
Z
Z
Z
Y
FFGZZZY
FFGZZZY ------------- YES
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e4;
int u[N+10], s[N+10];
void solve()
{
int n; cin >> n;
string str; cin >> str;
if (n == 1) puts("YES"),return;
char ch;
for (int i = 0; i < n; i++)
{
if (str[i] != str[i+1])
{
ch = str[i]; // find the first different index
for (int j = i+1; j < n; j++)
{
if (str[i] == str[j]) puts("NO"), return;
}
}
}
puts("YES");
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int T; cin >> T;
while (T--)
solve();
return 0;
}
b
1到n共有多少位数都相同的数
位数相同的数 1 2 3…9, 11, 22…111…11111…
暴力,肯定是不行的
虽然是div3,否则还不如给大家布置c++课后作业?
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll fun (int a, int b)
{
ll ans = 0;
int cnt = 1;
for (int i = 1; i <= a; ++i){
ans += cnt * b;
cnt *= 10;
}
return ans;
}
void solve()
{
ll n, ans = 0; cin >> n;
for (int i = 1; i <= 9; ++i)
for (int j = 1; j <= 9; ++j)
if (fun(i , j) <= n) ans++;
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int T; cin >> T;
while (T--)
solve();
return 0;
}
或者可以打表
int paiduan(int n)
{
int count=0;
if(n<10)return n;
else if(n<=100)
{
n=n/11;
return 9+n;
}
else if(n<=1000)
{
n=n/111;
return 18+n;
}
else if(n<=10000)
{
n=n/1111;
return 27+n;
}
else if(n<=100000)
{
n=n/11111;
return 36+n;
}
else if(n<=1000000)
{
n=n/111111;
return 45+n;
}
else if(n<=10000000)
{
n=n/1111111;
return 54+n;
}
else if(n<=100000000)
{
n=n/11111111;
return 63+n;
}
else
{
n=n/111111111;
return 72+n;
}
}
c
输入一个数n
上下左右四个值相差大于1
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int n; cin >> n;
if (n == 2) {
cout << "-1\n";
} else {
for (int i = 1; i <= n * n; i += 2) {
cout << i << ' ';
if ((i + 1) % n == 0) cout << endl;
}
for (int i = 2; i <= n * n; i += 2) {
cout << i << ' ';
if ((i + 1) % n == 0) cout << endl;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int T; cin >> T;
while (T--)
solve();
return 0;
}