A
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。
翻译如下:
float calculate(float x){
if(x>=0 and x<2){
return 2.5+x*-1;
}else if(x>=2 and x<4){
return 2-1.5*(x-3)*(x-3);
}else if(x>=4 and x<6){
return x/2-1.5;
}else{
return 0;
}
}
缺失点: 首先,C++中是int/int=整除,所以这里需要定义x为float单精度或者双精度,这里应该需要定义为双精度浮点数。
其次,上面对x的范围提示不全,如果用户输入超出数据范围的数据x,我们应该提醒用户数输入范围错误,而不是返回0,让用户猜测是怎么回事。如下:
#include<iostream>
#include<cmath>//<math.h>
using namespace std;
int main()
{
double x,y=0;
cout<<"请输入x的值:"<<endl;
cin>>x;
if(x>=0&&x<2) cout<<"函数的值为:"<<-1*x+2.5<<endl;
if(x>=2&&x<4) cout<<"函数的值为:"<<2-1.5*pow(x-3,2)<<endl;
if(x>=4&&x<6) cout<<"函数的值为:"<<x/2-1.5<<endl;
else if(x<0||x>6)
cout<<"输入的x的范围不正确,函数的定义域为0-6"<<endl;
return 0;
}
2、编写程序,读入一个整数 N。若 N 为非负数,则计算 N 到 2N 之间的整数和;若 N 为一个负数,则求 2N 到 N 之间的整数和。
#include<iostream>
#include<cmath>//<math.h>
using namespace std;
int main()
{
int x,y=0;
cout<<"请输入整数 N:"<<endl;
cin>>x;
if(x>=0) cout<<"N 到 2N 之间的整数和为:"<<(x+1)*(2*x+x)/2.0<<endl;
else
cout<<"2N 到 N 之间的整数和为:"<<(-x+1)*(2*x+x)/2.0<<endl;
return 0;
}
3、设 N 是一个四位数,它的 9 倍恰好是其反序数(例如:1234 的反序数是 4321),求 N 的值。
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n[4], i=1234;
for(int i=1000;i<10000;i++){
//传统方式:
int ge = i % 10;//4
int qian = i / 1000;//1
int bai = (i - qian*1000) / 100;//2
int shi = (i - qian*1000-bai*100) / 10;//3
if(i*9==ge*1000+shi*100+bai*10+qian)
cout<<i<<" ";
}
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n[4], i=1234;
for(int i=1000;i<10000;i++){
int j=i,k=0;
//循环方式
while(j){
n[k]=j%10;//取末位
j=j/10;//去末位
k++ ;
}
if(i*9==n[0]*1000+n[1]*100+n[2]*10+n[3])
cout<<i;
}
return 0;
}
4、N 个人围成一圈顺序编号,从 1 号开始按 1、2、 3 顺序报数,报 3 者退出圈外,其余的人再从 1、2、 3 开始报数,报 3 的人再退出圈外,依次类推。请按退出顺序输出每个退出人的原序号。要求使用环形链表编程。
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
/*
约瑟夫环
*/
typedef struct cir{
int num;
struct cir * next;
}cir;
int main()
{
//成环
cir *p,*head;
head = new cir;//结构体,new没有()
head->num = 1;
p = head;
cout<<"请输入人数:";
int n;
cin>>n;
for(int i=1;i<n;i++){
cir* s = new cir;//<==>(cir*)malloc(sizeof(cir));
p->next = s;
p = s;
p->num = i+1;
}
p->next = head;
//出
int k=1,count=n;
while(count){
if(k%3==0){
cir* c = head;
cout<<c->num<<" ";
p->next = head->next; //链表删除
head = p->next;
free(c);
k=1;
count--;
}
k++;
p = head; //同步指针
head = head->next;
}
return 0;
}
注意:head = new cir;结构体指针创建内存空间的写法。
B
1、请输入高度 h,输入一个高为 h,上底边长为 h的等腰梯形(例如 h=4,图形如下)。
****
******
********
**********
#include<iostream>
using namespace std;
int main()
{
int h;
cout<<"输入高度 h:";
cin>>h;
for(int i=0;i<h;i++){//高度
for(int j=1;j<=((h-1)*2+h);j++){
if(j<h-i or j>(2*h-1)+i)//h-1+h+i 考虑后面的空格情况
cout<<" ";
else cout<<"*";//除了上面的,就是中间的* 两个for循环是矩形
}
cout<<endl;
}
return 0;
}
原文中有另一种解法:
#include<iostream>
using namespace std;
int main()
{
int h,i,j;
cout<<"请输入高度:"<<endl;
cin>>h;
cout<<"图形如下:"<<endl;
for(i=1; i<=h; i++)
{
for(j=1; j<=h-i; j++)//不考虑后面的空格情况
cout<<" ";
for(j=1; j<=h+(i-1)*2; j++)#*是每层加两个,刚好对应层数
cout<<"*";
cout<<endl;
}
return 0;
}
2、请编写一个程序,从键盘上输入 n(n 的范围是1~20),求 n 的阶乘。
#include<iostream>
using namespace std;
int factorial(int n){
if(n==1){
return 1;
}else{
return n*factorial(n-1);
}
}
int main()
{
cout<<"从键盘上输入 n(n 的范围是1~20):";
int n;
cin>>n;
if(n<1 or n>20){
cout<<"范围错误"<<endl;
}else{
cout<<"结果是:"<<factorial(n)<<endl;
}
return 0;
}
3、从键盘上任意输入一个长度不超过 20 的字符串,对所输入的字符串,按照 ASCII 码的大小从小到大进行排序,请输出排序后的结果。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
cout<<"输入一个长度不超过 20 的字符串:";
string n;
cin>>n;
if(n.length()>20){
cout<<"长度错误"<<endl;
}else{
sort(n.begin(), n.end());//重点掌握 ★★★★
cout<<"结果是:"<<n<<endl;
}
return 0;
}