1、编写程序,计算下列分段函数 y=f(x)的值。
y = -x + 2.5, 0 <= x < 2
y = 2 - 1.5 (x - 3) (x - 3), 2 <= x < 4
y = x / 2 - 1.5, 4 <= x < 6
#include <iostream>
using namespace std;
int main() {
double x;
while(cin >> x) {
if(x >= 0 && x < 2) {
cout << 2.5 - x << endl;
}
else if(x >= 2 && x < 4) {
cout << 2 - 1.5 * (x - 3) * (x - 3) << endl;
}
else if(x >= 4 && x < 6) {
cout << x / 2 - 1.5 << endl;
}
else {
cout << "请输入0-6的数字";
continue;
}
}
return 0;
}
2、编写程序,读入一个整数 N。若 N 为非负数,则计算 N 到 2N 之间的整数和;若 N 为一个负数,则求 2N 到 N 之间的整数和。
#include <iostream>
using namespace std;
int main() {
int n;
while(cin >> n) {
int sum = 0;
if(n > 0) {
for(int i = n; i <= 2 * n; i++) {
sum += i;
}
}
else {
for(int i = -n; i <= 2 * (-n); i++) {
sum -= i;
}
}
cout << sum << endl;
}
return 0;
}
3、设 N 是一个四位数,它的 9 倍恰好是其反序数(例如:1234 的反序数是 4321),求 N 的值。
#include <iostream>
using namespace std;
int main() {
int n, m;
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
for(int k = 0; k < 10; k++) {
for(int l = 0; l < 10; l++) {
n = i * 1000 + j * 100 + k * 10 + l;
m = l * 1000 + k * 100 + j * 10 + i;
if(n * 9 == m && m > 0) cout << n << endl; //无限制m>0,会多输出0
}
}
}
}
return 0;
}
4、N 个人围成一圈顺序编号,从 1 号开始按 1、2、3 顺序报数,报 3 者退出圈外,其余的人再从 1、2、3 开始报数,报 3 的人再退出圈外,依次类推。请按退出顺序输出每个退出人的原序号。要求使用环形链表编程。
来源:https://www.cnblogs.com/ache/p/12518950.html