A题:一起奏响历史之音!
思路:
检查是否不含4或7即可
代码:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> a(7);
bool flag = true;
for(int i = 0; i < 7; i++)
{
cin >> a[i];
}
for(int i = 0; i < 7; i++)
{
if(a[i] == 4 || a[i] == 7)
{
flag = false;
cout << "NO" <<endl;
break;
}
}
if(flag){
cout <<"YES" << endl;
}
return 0;
}
B题:能去你家蹭口饭吃吗
思路:
求中位数。将数组排序,然后找到中间的数,建议即为答案。【注:不必考虑如果相邻两个数相同的情况,因为如果取了相同的那个数,就不能保证小于一半数量的碗容量了。
代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//找出一堆数字的中位数
//可以排序,然后取中间的数字
//容量没有超过int
int main()
{
int n; //碗的数量
cin >> n;
vector<int> a(n); //每一个碗的容量
for(int i = 0 ; i < n; i++)
{
cin >> a[i];
}
sort(a.begin(), a.end());
int mid = n / 2; //记录中间数
cout << a[mid] - 1 << endl;
return 0;
}
F题:一起找神秘的数!
补充:位运算基本知识
OR :有1则1
AND:全1则1
XOR:不同为1
思路:
根据题意计算多几组数据,发现只有当x = y的时候才符合条件,故转为计算区间大小即可。
代码:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int count; //数据组数
cin >> count;
while(count--)
{
long long l, r; //区间
cin >> l >> r;
long long res; //代表满足条件的不同x,y对数
res = r - l + 1; //相同的一定满足条件
cout << res << endl;
}
return 0;
}
G题:一起铸最好的剑!
思路:
要多多考虑特殊情况,比如这题当m = 1 的时候,无论怎么添加柴,温度都不会改变【这个细节很重要】,可以和m = n的情况一起输出1。然后一次次增加,注意比较前后温度的绝对值,选出最接近理想温度的添柴次数。
代码:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int count;
cin >> count;
while(count--)
{
long long n, m;
cin >> n >> m;
long long res = 1; //记录最少添柴次数
while(true) //数据过大的时候超时了
{
//注意注意m=1时,无论几次都不能变大,不考虑该情况会陷入死循环
if(m == n || m == 1){
cout << 1 << endl;
break;
}
else if( pow(m,++res) > n)
{
//比较一下绝对值
if(abs(pow(m,res)-n) < abs( pow(m, res-1)-n)){
cout << res << endl;
}
else{
cout << res-1 << endl;
}
break;
}
}
}
return 0;
}