2018北京小学生程序设计友谊赛详细答案

第1题 找平方数

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++)
    {
        cin >> a[i];
        int root = sqrt(a[i]);
        if(root * root == a[i])
        {
            cout << a[i] << endl;
        }
    }

    return 0;
}

第2题 回文密码

#include <iostream>
using namespace std;

bool isPalin(string s)
{
    int len = s.size();
    int i = 0;
    int j = len - 1;
    while(i < j)
    {
        if(s[i] != s[j])
        {
            return false;
        }
        i++;
        j--;
    }

    return true;
}

int main()
{
    string s;
cin >> s;

    int len = s.size();
    int i = 0;
    if(isPalin(s))
    {
        i = 1;
    }

    while(i < len)
    {
        cout << s[i];
        i += 2;
    }
    cout << endl;

    return 0;
}

第3题 采购奖品

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

struct goods
{
	int cost;
	int amount;
};

bool cmp(goods a,goods b)
{
	return a.cost<b.cost;
}

int main()
{
	freopen("prize.in","r",stdin);
	freopen("prize.out","w",stdout);

	int money, n;
	cin >> money >> n;

	goods g[n];
	int i = 0;
	for(; i < n; i++)
	{
		cin >> g[i].cost >> g[i].amount;
	}

	sort(g, g + n, cmp);

	int ans = 0;
	int cnt = 0;

	i = 0;
	while(money >= g[i].cost) // 若剩余的钱连一个都不买不了,则循环结束
	{
		int tmpCnt = money / g[i].cost; // 不考虑商品数量限制,理论上最多可买的数量
		cnt = min(tmpCnt, g[i].amount);
		money -= (cnt*g[i].cost);
		ans += cnt;
		i++;
	}

	cout << ans << endl;

	return 0;
}
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值