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 >= && x < ) {
cout << 2.5 - x << endl;
}
else if(x >= && x < ) {
cout << - 1.5 * (x - ) * (x - ) << endl;
}
else if(x >= && x < ) {
cout << x / - 1.5 << endl;
}
else {
cout << "请输入0-6的数字";
continue;
}
}
return ;
}
2、编写程序,读入一个整数 N。若 N 为非负数,则计算 N 到 2N 之间的整数和;若 N 为一个负数,则求 2N 到 N 之间的整数和。
#include <iostream>
using namespace std;
int main() {
int n;
while(cin >> n) {
int sum = ;
if(n > ) {
for(int i = n; i <= * n; i++) {
sum += i;
}
}
else {
for(int i = -n; i <= * (-n); i++) {
sum -= i;
}
}
cout << sum << endl;
}
return ;
}
3、设 N 是一个四位数,它的 9 倍恰好是其反序数(例如:1234 的反序数是 4321),求 N 的值。

#include <iostream>
using namespace std; int main() {
int n, m;
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
for(int k = ; k < ; k++) {
for(int l = ; l < ; l++) {
n = i * + j * + k * + l;
m = l * + k * + j * + i;
if(n * == m && m > ) cout << n << endl; //无限制m>0,会多输出0
}
}
}
}
return ;
}
4、N 个人围成一圈顺序编号,从 1 号开始按 1、2、3 顺序报数,报 3 者退出圈外,其余的人再从 1、2、3 开始报数,报 3 的人再退出圈外,依次类推。请按退出顺序输出每个退出人的原序号。要求使用环形链表编程。

#include <iostream>
using namespace std; struct Node
{
int data;
Node* next;
}; int main() {
int m;
cin >> m;
while (m--) {
int n;
cin >> n;
Node *head = new Node;
Node *p = new Node;
head = p;
p -> next = NULL;
p -> data = ;
for(int i = ; i <= n; i++) {
Node *q = new Node;
q -> data = i;
q -> next = NULL;
p -> next = q;
p = q;
}
p -> next = head;
int cnt = ;
while(head -> next != head) {
Node *t = new Node;
t = head -> next;
cnt++;
if(cnt % == ) {
cout << head -> next -> data << " ";
head -> next = t -> next;
}
else head = head -> next;
}
cout << head -> data << endl;
}
return ;
}

最新文章

  1. SQL 表连接查询出现重复列,由此理清LEFT JOIN、INNER JOIN的区别
  2. Web端测试和移动端测试的区别
  3. 21SpringMvc_异步发送表单数据到Bean,并响应JSON文本返回(这篇可能是最重要的一篇了)
  4. 使用main方法调用http请求本地服务器的某个servlet报错问题
  5. CLREX
  6. 初探Linux进程管理机制
  7. 转:1.1 cdev_init cdev_alloc 使用说明
  8. CentOS 6.5安全加固及性能优化
  9. web简易MP3播放插件 Aplayer篇章一
  10. oracle习题1~13
  11. arcgis api for js入门开发系列十一地图统计图
  12. DirectX11--深入理解HLSL常量缓冲区打包规则
  13. webloigc 控制台修改登录密码
  14. 以选项卡的故事扯扯js面向对象
  15. gdbserver
  16. ACM模拟赛
  17. 【Win10】实现控件倒影效果
  18. 关于java调用Dll文件的异常 Native library (win32-x86-64/CtrlNPCDLL.dll) not found in resource pat
  19. Android ComponentName的用法
  20. 60、二叉搜索树的第k个结点

热门文章

  1. B. Sorted Adjacent Differences(思维构造)
  2. 用 GitHub Action 构建一套 CI/CD 系统
  3. 一篇文章解决MongoDB的所有问题
  4. Linux内核驱动学习(七)应用层直接操作GPIO
  5. Autojs - 用 JavaScript 实现自己的安卓手机自动化工具脚本
  6. 设计模式之GOF23备忘录模式
  7. 超过百万的StackOverflow Flutter 问题-第二期
  8. 初识spring boot maven管理--配置文件
  9. java -&gt;IO流_File类
  10. 使用windows(win7和win10),最好用chocolatey