- 按照从上到下,从左往右顺序输入一颗二叉树中每一个结点对应的权重,判断该满二叉树是否存在这样的非叶子节点,使得左数所有叶子结点之和等于右树所有叶子结点之和,如果存在输出Yes,不存在输出No。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <sstream>
using namespace std;
int sum(vector<int>v, int index)
{
if (index<v.size())
return v[index] + sum(v, 2 * index + 1) + sum(v, 2 * index + 2);
else return 0;
}
int main()
{
//input demo
vector<vector<int>> v;
int count;
cin >> count;
getchar();
for (int i = 0; i < count; i++)
{
vector<int> temp;
string str;
getline(cin, str);
stringstream ss;
ss << str;
string number;
while (getline(ss, number, ' '))
{
temp.push_back(stoi(number));
}
v.push_back(temp);
}
//
for (int k = 0; k < v.size(); k++)
{
int flag = 0;
for (int i = 0; i <= (v[k].size() / 2 - 1); i++)
{
if (sum(v[k], 2 * i + 1) == sum(v[k], 2 * i + 2))
{
cout << "Yes" << endl;
flag = 1;
break;
}
}
if (flag == 0) cout << "No" << endl;
}
}
- 输入正整数n,判断1-n之间整数出现5的次数(暴力解法)
#include <iostream>
using namespace std;
int findFive(int n){
int res = 0;
for (int i = 1; i <= n; i++) {
int temp = i;
while (temp > 0) {
if (temp % 10 == 5) {
res++;
}
temp = temp / 10;
}
}
return res;
}
int main() {
int n;
cin >> n;
cout << findFive(n) << endl;
return 0;
}