LHL 《计算机程序设计C++》第5周中级练习

1比较字符串(不区分大小写)(10分)
题目内容:

编写函数,比较两个字符串str1,str2的大小(不区分大小写),前者大返回1后者大返回-1,相等返回0。

编写主函数,输入两个字符串,调用函数比较,输出返回值。字符串长度超过100。

输入格式:

一行,两个字符串用空格隔开。

输出格式:

1,-1,0三个整数之一。

输入样例:

four for

输出样例:

1

扫描二维码关注公众号,回复: 10588223 查看本文章
#include <iostream>
using namespace std;
int comstr(char * s1,char * s2){
	if(!(*s1) &&!(*s2))return 0;
	if(*s1<='Z')*s1+=32;
	if(*s2<='Z')*s2+=32;
	if(*s1-*s2>0)return 1;
	if(*s1-*s2<0)return -1;
	s1++;s2++;
	comstr(&*s1,&*s2);
}
int main(){
	char s1[1000],s2[1000];
	cin>>s1>>s2;
	cout<<comstr(s1,s2);
	return 0;
}

2二分法解方程(10分)
题目内容:

编写函数,用二分法求方程2x3−4x2+3x−6=0在[-10,10]之间的根。|f(x)|<eps时停止迭代。

二分法:对于区间[a,b]上连续 且f(a)·f(b)<0的函数y=f(x),通过不断地把函数f(x)的零点所在的区间一分为二,使区间的两个端点的距离逐步逼近零点,进而得到零点近似值的方法。

编写主函数,输入区间个精度,调用函数求根,显示返回值。数据类型double。

输入格式:

一行,三个实数,前两个是区间的左右端点,第三个是精度。

输出格式:

一个实数,表示近似根。

输入样例:

-10 10 0.00001

输出样例:

2

#include <iostream>
using namespace std;
double eps,fx1,fx2;
double f(double x){
	return 2*x*x*x-4*x*x+3*x-6;
}
double abs(double x){
	if(x<0)x=0-x;
	return x;
}
void printx(double x1,double x2){
	double x0=(x1+x2)/2;
	double fx0=f(x0);
	if(abs(fx0)<=eps){
		cout<<x0;
		return;
	}
		if (fx0*fx1<0){
			x2=x0;
			fx2=fx0;
		}
		else{
			x1=x0;
			fx1=fx0;
		}
	printx(x1,x2);
}
int main()
{
	double x1,x2;
	cin>>x1>>x2>>eps;
	fx1=f(x1);
	fx2=f(x2);
	printx(x1,x2);
}

3牛顿法解方程(10分)
题目内容:

编写函数,用牛顿法求方程f(x)=x³+2x²+3x+4=0在x0附近的根。要求|f(x)|<eps时停止。数据类型用double。牛顿法的求根公式为:

k=1,2,3,…

编写主函数,输入初始根和精度,调用函数求根,输出返回值。数据类型double。不能使用数学库函数。

输入:输入初始根x0和精度eps。

输出:输出一个实数。

【输入输出样例】

输入:

1 0.0001

输出:

-1.65064

技巧提示:f(x),导数,求绝对值,均定义为函数。

时间限制:500ms内存限制:32000kb

#include <iostream>
using namespace std;
double fabs(double x){
	if(x<0)x=0-x;
	return x;
}
double f(double x){
	return x*x*x+2*x*x+3*x+4;
}
double f1(double x){
	return 3*x*x+4*x+3;
}
double outqs(double x,double eps) {
	double f0=f(x)/f1(x);
	while(fabs(f(x))>eps){
             x-=f0;
             f0=f(x)/f1(x);
}
return x;
}
int main(){
	double x=1,eps=0.0001;
	cin>>x>>eps;
	cout<<outqs(x,eps)<<endl;
	return 0;
}

4输入、排序、查找(10分)
题目内容:

分别编写函数输入数组元素(输入-9999表示结束)、从小到大排序数组元素、用二分法在有序数组中查找(返回下标)。

编写主函数,调用函数进行输入、排序和查找。数组元素不超过100个,均为整数。

输入格式:

两行,第1行为输入的数组元素,以-9999结束;第2行输入5个待查找的元素。数据用空格隔开。

输出格式:

5行,每行是查找到的元素的下标,找不到时结果为-1。

输入样例:

2 1 3 4 5 -9999

2 3 4 -10 10

输出样例:

1

2

3

-1

-1

时间限制:500ms内存限制:32000kb

#include<iostream>
using namespace std;
void swap(int *a,int *b){
	int t=*a;
	if(*a>*b){
		*a=*b;
		*b=t;
	}
}
void sort(int a[],int n){
	for(int i=1;i<n-1;++i)
		for(int j=i+1;j<n;++j)
			swap(&a[i],&a[j]);
}
int twofind(int a[],int t,int l,int r){
//	for(int i=l;i<=r;++i){
//		if(t==a[i])return i;
//	}
//	return 0;
	if(l>r)return 0;
	int m=l+(r-l)/2;
	if(t==a[m])return m;
	if(t<a[m])r=m-1;
	else l=m+1;
	twofind(a,t,l,r);
}
int main(){
	int a[110]{0},b[5],i=0;
	do{
		cin>>a[++i];
	}while(a[i]!=-9999);
	for(int i=0;i<5;++i)
		cin>>b[i];
	sort(a,i);
	for(int j=0;j<5;++j)
		cout<<twofind(a,b[j],1,i-1)-1<<endl;	
    return 0;
}

5单词排序(10分)
题目内容:

编写程序,对输入的若干单词按词典排序,不区分大小写。要求字符串的比较、复制、大小写转换、排序等工作均用函数实现。

在主函数中输入单词,调用函数排序,在主函数中输出结果。字符串个数不超过100个,每个单词的长度不超过20。

不能使用系统的字符串处理库函数,不能使用标准模板库的类和函数。

输入格式:

两行:第1行为单词个数n,第2行为n个单词,用空格隔开。

输出格式:

n行,每行一个单词。

输入样例:

5

sin cos log exp sqrt

输出样例:

cos

exp

log

sin

sqrt

时间限制:500ms内存限制:32000kb

#include <iostream>

using namespace std;

char strr[100][20];//定义一个全局变量,因为str的大小写虽然忽略但是不能改变,
//所以设置一个全局变量,改变全局变量的大小写,用全局变量进行同样的比较、排序,但最后用str输出

void strcpy(char *str1,char *str2)
{//字符串复制函数
    while((*str1++=*str2++)!='\0');
}

void reserve(char str[100][20],int n)
{
    char *s;
    for(int i=0;i<n;i++)
    {
        s=&strr[i][0];//定义一个指针变量*s指向数组每一行字符串的首地址
        while(*s!='\0')
        {//大写转小写
            if(*s>='A'&&*s<='Z')//*s代表的是对应字符串第几个元素的值
                *s+=32;
            *s++;//指针向后移动
        }
    }
}
int strcmp(char *str1,char *str2)
{//比较字符串大小
    while(*str1!='\0' && *str2!='\0' && *str1==*str2)
    {
        *str1++;*str2++;
    }
    if(*str1>*str2)
        return 1;
    else if(*str1<*str2)
        return -1;
    else
        return 0;
}
void arrange(char str[100][20],char strr[100][20],int n)
{//字符串排列
    for(int i=0;i<n-1;i++)
    {
        int j=i;
        for(int k=i+1;k<n;k++)
        {
            if(strcmp(strr[j],strr[k])==1)
            {//前者比后者小,就换位置
                char s[20];
                strcpy(s,str[j]);
                strcpy(str[j],str[k]);
                strcpy(str[k],s);

                strcpy(s,strr[j]);
                strcpy(strr[j],strr[k]);
                strcpy(strr[k],s);
            }
        }
    }
}
int main()
{
    char str[100][20];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
         cin>>str[i];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<20;j++)
        {
            strr[i][j]=str[i][j];
        }
    }
    reserve(str,n);//由于不区分大小写,所有的大写字母都转换成小写字母统一比较
    arrange(str,strr,n);//字符串排序
    for(int i=0;i<n;i++)
        cout<<str[i]<<endl;
    return 0;
}

6识别数字(10分)
题目内容:

编写函数,输出字符串中的所有整数。要考虑正、负数。

编写主函数,输入带空格的字符串,调用函数输出其中的整数。

输入格式:

一行,表示一个句子,中间可能有空格,有若干整数。

输出格式:

一行,若干整数,用一个空格隔开,末尾无空格。

注意:单独符号不算数字,但-0为0,+0也为0。

输入样例:

CHINA DAILY | Updated: 2020-03-21 07:57

输出样例:

2020 -3 -21 7 57

#include<iostream>
using namespace std;
int is(char s){
	int b=0;
	if(s>='0'&&s<='9')b=1;
	return b;
}
int main(){
	int n=110;
	char str[n];
	cin.getline(str,n);
	int i=0;
	int a[n],j=0,t=0;bool jw=0,fh=0;
	while(str[i]){
		if(str[i]=='-'&& is(str[i+1]))fh=1;
		if(is(str[i])){
			if(jw)t*=10;
			t+=str[i]-'0';
			if(is(str[i+1]) )jw=1;
			if(!is(str[i+1]) ){
				if(fh)t*=-1;
				a[j++]=t;
				t=0;fh=jw=0;
			}
		}
		++i;
	}
	for(int i=0;i<j-1;++i)
		cout<<a[i]<<" ";
	cout<<a[j-1];
	
    return 0;
}

发布了40 篇原创文章 · 获赞 19 · 访问量 2715

猜你喜欢

转载自blog.csdn.net/Simple_questions/article/details/105380126