目录
直接枚举就行了
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
for (int i = 1; i <= 1e9; i++) {
if (i % 23 == 0 && i % 11 == 0 && i % 8 == 0) {
printf("%04d is a special year!", i);
return 0;
}
}
}
if else 讨论一下
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int n;
cin >> n;
if (n >= 15 && n < 30) {
cout << n << ' ' << "Mama";
}
else if (n >= -10 && n < 15) {
cout << n << ' ' << "YuRongFu";
}
else {
cout << n << ' ' << "Zhai";
}
}
按比例 n/10? n/40? n划分
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
while (m--)
{
int x;
cin >> x;
if (x <= n / 10) {
cout << 1 << '\n';
}
else if(x<=n/10*4){
cout << 2 << '\n';
}
else {
cout << 3 << '\n';
}
}
}
从左到右,如果是不带'+'的就两个合成下一个,带'+'的3个合成下一个,同时取模的话就是剩下合成不了的
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int cnt = 0;
for (int i = 1; i <= 9; i++) {
int x;
cin >> x;
x += cnt;
if (i == 9) {
cout << x;
return 0;
}
if (i & 1) {
cnt = x / 2;
cout << x % 2 << ' ';
}
else {
cnt = x / 3;
cout << x % 3<<' ';
}
}
}
本来想写kmp发现这复杂度直接暴力匹配就能过了
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
string s;
getline(cin, s);
int n = s.size();
s += " ";
int f = inf, cnt = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'N' && s[i + 1] == 'i' && s[i + 2] == 'a' && s[i + 3] == 'n') {
cnt++;
f = min(f, i);
}
}
if (!cnt) {
cout << 0 <<' '<< -1 ;
}
else {
cout << cnt << ' ' << f << '\n';
}
}
直接枚举矩阵上的每一个点,从每一个点的上下左右出发 ,看看能发现多少烟花,同时如果找身高比h大的就直接结束,最后更新一下最优解,和总个数
const int N = 50 + 5;
int a[N][N];
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int n, m, h;
cin >> n >> m >> h;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
PII res = { 0,0 };
int tot = 0;
int maxx = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int cnt = 0;
if (a[i][j] == 0) {
for (int k = j + 1; k <= m; k++) {
if (a[i][k] >= h) break;
else {
if (a[i][k] < 0) {
cnt++;
}
}
}
for (int k = j - 1; k >= 1; k--) {
if (a[i][k] >= h) break;
else {
if (a[i][k] < 0) {
cnt++;
}
}
}
for (int k = i + 1; k <= n; k++) {
if (a[k][j] >= h) break;
else {
if (a[k][j] < 0) {
cnt++;
}
}
}
for (int k = i - 1; k >= 1; k--) {
if (a[k][j] >= h) break;
else {
if (a[k][j] < 0) {
cnt++;
}
}
}
if (cnt >= 3) {
tot++;
}
if (cnt > maxx) {
maxx = cnt;
res = { i-1,j-1 };
}
}
}
}
cout << tot << '\n';
cout << res.first << ' ' << res.second;
}
直接分解质因数,把奇因子存入vector中,最后再对vector进行排序
如果只有一个数就取他的最大值
如果不止一个数就把他的最大值删掉,这样vector.back()就是次大值了
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int n;
cin >> n;
int res = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
vector<int>p;
for (int i = 1; i <= sqrt(x); i++) {
if (x % i == 0) {
if (i & 1) {
p.push_back(i);
}
if ((x / i) & 1) {
if (p.size() && p.back() != (x / i)) {
p.push_back(x / i);
}
}
}
}
sort(p.begin(), p.end());
if (p.size() == 1) {
res += p.back();
}
else {
p.pop_back();
res += p.back();
}
}
cout << res << '\n';
}
令P=131
用一个hash记录a,b,c-->(a * P * P + b * P + c)帮助我们快速匹配
再用一个map,用来存放结果
const int P = 131;
map<int, pair<int, string>>mp;
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int a, b, c;
cin >> a >> b >> c;
string s;
cin >> s;
int res = a * P * P + b * P + c;
mp[res] = { i,s };
}
int k;
cin >> k;
while (k--)
{
int a, b, c;
cin >> a >> b >> c;
int res = a * P * P + b * P + c;
if (mp.count(res)) {
cout << mp[res].first << ' ' << mp[res].second << '\n';
}
else {
cout << "Not Found\n";
}
}
}