第三届海淀区智慧杯小学组

第三届海淀区智慧杯小学组

圣诞帽
#include <bits/stdc++.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	
	//前n行 
	for (int i = 0; i < n; i ++) {
		//每行宽度不超过2n+1列 
		for (int j = 0; j <= 2*n; j ++) {
			if (j == n-i || j == n+i) cout << '*';
			else if (j < n+i) cout << ' ';
		}
		cout << endl;
	}
	
	//最后1行 
	for (int j = 0; j< 2*n+1; j ++) cout << '*';

	return 0;
}
选组长
#include <bits/stdc++.h>
using namespace std;

int main() {
	long long a, b, c, d;
	cin >> a >> b >> c >> d;
	
	long long ans = (a+b+c+d) * (a+b+c+d) - a*a - b*b - c*c - d*d;
	cout << ans / 2;

	return 0;
}
青蛙跳
#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, x1, x2, y1, y2, dx, dy;
	cin >> n;
	while (n --) {
		cin >> x1 >> x2 >> y1 >> y2;
		
		dx = x1 - x2;
		dy = y1 - y2;
		
		if (dx == 0) puts("YES");
		else if (dy == 0) puts("NO");
		else if (dx % dy == 0 && dx / dy < 0) puts("YES");
		else puts("NO");
	}

	return 0;
}
结合律
#include <bits/stdc++.h>
using namespace std;

int main() {
	int a[2][2] = {};
	cin >> a[0][0] >> a[0][1] >> a[1][0] >> a[1][1];
	
	bool flag = true;
	for (int i = 0; i < 8; i ++) {
		int x = i % 2;
		int y = i / 2 % 2;
		int z = i / 4;
		if (a[a[x][y]][z] != a[x][a[y][z]]) {
			flag = false;
		}
	}

	if (flag) puts("Yes");
	else puts("No");
	
	return 0;
}
石子游戏
#include <bits/stdc++.h>
using namespace std;

int main() {
	int T, a, b;
	cin >> T;
	while (T --) {
		cin >> a >> b;
		
		if (a*b % 2 == 0) puts("Alice");
		else puts("Bob");
	}

	return 0;
}
吉利的数字
  • 代码一
#include <bits/stdc++.h>
using namespace std;

const int N = 100009, M = 10;
int n, m, a[N], ans[N], num[M];

int main() {
	string s;
	cin >> s >> m;
	for (int i = 0; i < m; i ++) cin >> num[i];
	sort(num, num + m);
	
	//先从低位 -> 高位,尽可能用大的数字 
	int t = 0;	//1:被低位借1当10;		0没被借位 
	for (int i = s.size() - 1; i >= 0; i --) {
		int x = s[i] - '0' - t;
		if (x < num[0] && i != 0) t = 1, x += 10;
		else t = 0;
		
		for (int j = m - 1; j >= 0; j --) {
			if (num[j] <= x) {
				ans[i] = num[j];
				break;
			}
		}
	}	

	//再从高位 -> 低位,如果高位有富余,则借给低位,借1当10 
	t = 0; 
	for (int i = 0; i < s.size(); i ++) {
		if (t == 1) {
			ans[i] = num[m-1];
			t = 1;
		}
		else {
			if (s[i] - '0' > ans[i]) t = 1;
			else t = 0;
		}	
	} 
	
	//去掉前导 0 
	int l = -1;
	for (int i = 0; i < s.size(); i ++) {
		if (ans[i] != 0) {
			l = i;
			break;
		}
	}
	
	if (l == -1)  cout << -1;
	else {
		for (int i = l; i < s.size(); i ++) cout << ans[i];
	}

	return 0;
}
  • 代码二
#include <bits/stdc++.h>

using namespace std;


namespace IN
{
    string n;
    char a[10];
    int m;
}
using namespace IN;


int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;
    for (size_t i = 1; i <= m; i ++) cin >> a[i];

    sort(a + 1, a + m + 1);

    int len = n.size();
    bool lst = 0;   // 0表示正常,1表示是退回来的
    string ans = "";

    for (size_t i = 0; i < len; )   // 每一位数字
    {
        int cnt = 0;
        for (size_t j = m; j >= 1; j --)   // 有m种选择
        {
            if (a[j] == n[i] && !lst)
            {
                ans += a[j], i ++;
                break;
            }
            else if (a[j] < n[i])   // 因为是从大往小找,所以找到了肯定是最接近的
            {
                ans += a[j], i ++;
                for (; i < len; i ++) ans += a[m];

                break;
            }
            else cnt ++;
        }

        if (cnt == m)   // 说明没有≤n[i]的
        {
            if (len == 1)
            {
                cout << -1;
                return 0;
            }

            if (i == 0) for (i ++; i < len; i ++) ans += a[m];
            else ans[-- i] = ' ', lst = 1;   // 退回去
        }
    }

    for (size_t i = 0; i < ans.size(); i ++)
        if (ans[i] != ' ' && !(i==0 && ans[i]=='0')) cout << ans[i];

    return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值