加减乘除执行效率依次降低,执行时间依次增加
直接移位的执行效率相对较高
半质数问题
1、简单解法
//百钱百鸡问题
#include <cstdio>
#include <iostream>
using namespace std;
int main (void) {
int x = 0;
int y = 0;
int z = 0;
int n;
while (cin>>n) {
for (x=0; x <= 20; x++) {
for (y = 0; y < 33; y++) {
z = 100 - x - y;
if (z % 3 == 0 && 15*x +9*y + z == 300) {
printf("%d %d %d\n", x, y, z);
}
}
}
}
return 0;
}
2、高级解法
公元前五世纪,我国古代数学家张丘建在《算经》一书中提出了“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
详细描述:
接口说明原型: int GetResult(vector &list)
输入参数: 无输出参数(指针指向的内存区域保证有效):
list: 鸡翁、鸡母、鸡雏组合的列表
返回值: -1 失败 0 成功
#include <iostream>
#include <vector>
using namespace std;
int main (void)
{
string n;
while (cin >> n) {
int x, y, z;
for (x = 0; x <= 20; x++) {
for (y = 0; y < 33; y++) {
z = 100 - x - y;
if (z % 3 == 0 && 15*x + 3*y + z == 300) {
vector<int>line;
line.push_back(x);
line.push_back(y);
line.push_back(z);
res.push_back(line);
}
}
}
}
for (auto e:res) {
cout<<e[0]<<" "<<e[1]<<" "<<e[2]<<endl;
}
return 0;
}