C++编程 -- 基础练习(6)

文章介绍了使用C++编写的几个编程练习,包括判断回文数和对称回文数、递归求解学生年龄、计算梯形面积、选择法排序以及统计字符串字符种类。
摘要由CSDN通过智能技术生成

1. 回文数

题目:寻找300以内的所有的对称回文数并输出。回文数是指某数与其反序数相等,如5、131、1551、345676543.对称回文数指某数与其平方都是回文数。例如,n=11时,112=121;n=111时,1112=12321。 编写函数int huiwen(long n),判断n是否回文数,如是返回1,否则返回0。 在main函数中遍历300以内的数,寻找对称回文数并输出。

知识点:根据功能定义子函数,然后再调用

方法1:

#include <stdio.h>

int huiwen(long n) {
    long temp = n;
    long reverse = 0;
    
    while (temp != 0) {
        reverse = reverse * 10 + temp % 10;
        temp /= 10;
    }
    
    if (reverse == n) {
        return 1;
    } else {
        return 0;
    }
}

int main() {
    for (long i = 1; i <= 300; i++) {
        if (huiwen(i) && huiwen(i * i)) {
            printf("%ld ", i);
        }
    }
    
    return 0;
}

方法2:

#include <iostream>
using namespace std; 

int huiwen(long n)
{
	long a=n,m=0;
	while(n!=0){
		m=m*10+n%10;
		n=n/10;
	}
	if(m==a)
		return 1;
	else
		return 0;    
}
int main(void)
{
	long i;
	for(i=1;i <=300;i++){
		if(huiwen(i)&&huiwen(i*i))
			cout<<i<<" ";
	}
	return 0;
}

2. 求学生年龄

题目:有5个学生坐在一起,问第5个学生多少岁,他说比第4个学生大2岁。问第4个学生岁数,他说比第3个学生大2岁。问第3个学生岁数,他说比第2个学生大2岁。问第2个学生岁数,他说比第1个学生大2岁。最后问第1个学生,他说10岁,请问第5个学生多大。

知识点:根据功能定义子函数,然后再调用

方法1:

#include <stdio.h>

int getAge(int n) {
    if (n == 1) {
        return 10;
    } else {
        return getAge(n - 1) + 2;
    }
}

int main() {
    int age = getAge(5);
    printf("%d", age);
    return 0;
}

方法2:

#include <iostream>
using namespace std; 
int main()
{
	int age(int n);
	cout<<age(5)<<endl;
	return 0;
}

int age(int n) {
	int c;
	if(n==1)
		c=10;
	else
		c=age(n-1)+2;
	return(c);
}

3. 编写函数求梯形面积

题目:定义函数 areaT,功能是求梯形面积。要求在主函数中输入上底(用变量 a存储)、下底(用变量 b 存储)、和高(用变量 h 存储),在主函数中调用函数 areaT,输出梯形面积(用变量 s 存储)的值。其中,变量 a,b,h,s 数据类型均为 double。公式:s=(a+b)*h/2

方法1:

#include <stdio.h>

float areaT(float x, float y, float z) {
    return ((x + y) * z / 2);
}

int main() {
    float a, b, h, s;
    scanf("%f%f%f", &a, &b, &h);
    s = areaT(a, b, h);
    printf("%.0f", s);
    return 0;
}

方法2:

#include <iostream>
using namespace std; 
double areaT(double a,double b,double h) {
	
	return (a+b)*h/2;
}
int main() {
	double a,b,h,s;

	cin>>a>>b>>h;
	s=areaT(a,b,h);
	cout<<s<<endl;

	return 0;
}

4. 递归求函数值

题目:输入x和n,用递归的方法计算函数px(x,n)=x+x2+x3+…+xn的值。

知识点:px(x,n)= x+x2+x3+…+xn=x*(1+x+x2+x3+…+xn-1)=x*(1+px(x,n-1)),当n=1时结束递归,返回结果

方法1:

#include <stdio.h>

double px(double x, int n) {
    if (n == 1) {
        return x;
    } else {
        return x * (1 + px(x, n - 1));
    }
}

int main() {
    double X = 0;
    int N = 0;
    scanf("%lf%d", &X, &N);
    double result = px(X, N);
    printf("%.0f\n", result);
    return 0;
}

方法2:

#include <iostream>
using namespace std; 

double px(double x,int n)
{  if(n==1)
       return x;
    if(n>1)
       return (x*(1+px(x,n-1)));
}
int main()
{   double x;
    int n;
    cin>>x>>n;
    cout<<px(x,n)<<endl;
    return 0;
}

5. 用选择法对数组中10个整数按由小到大排序

题目:在数组中存入一串数字,数组长度不超过10,编写一个函数,实现用选择法对数组中10个整数按由小到大排序。

知识点:用一维数组a存放10个整数,选择法就是先将10个数中最小的数与a[0]交换;再将a[1]~a[9]中最小的数与a[1]交换……每比较一轮,找出一个未经排序的数中最小的一个。共比较9轮。

方法1:

#include <stdio.h>

void selectionSort(int arr[], int n) {
    int i, j, minIndex, temp;
    
    for (i = 0; i < n-1; i++) {
        minIndex = i;
        for (j = i+1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        temp = arr[minIndex];
        arr[minIndex] = arr[i];
        arr[i] = temp;
    }
}

int main() {
    int i, arr[10];   
    for (i = 0; i < 10; i++) {
        scanf("%d", &arr[i]);
    }
    selectionSort(arr, 10); 
    for (i = 0; i < 10; i++) {
        printf("%d ", arr[i]);
    }  
    return 0;
}

方法2:

#include <iostream>
using namespace std; 
int main()
{
	void sort(int array[],int n);
	int a[10],i;
	for(i=0;i<10;i++)
		cin>>a[i];
	sort(a,10);                      //调用sort函数
	for(i=0;i<10;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	return 0;
}
void sort(int array[],int n)
{	int i,j,k,t;
	for(i=0;i<n-1;i++)
	{	k=i;
		for(j=i+1;j<n;j++)
			if(array[j]< array[k]) 
				k=j;
		t=array[k];array[k]=array[i];array[i]=t;
	}
}

6. 统计字符个数

题目:编写一个函数,由实参传来一个字符串,统计该字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述结果。

方法1:

#include <stdio.h>

void countCharacters(char str[]) {
    int i, alphabets = 0, digits = 0, spaces = 0, others = 0;

    for (i = 0; str[i] != '\0'; i++) {
        if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
            alphabets++;
        } else if (str[i] >= '0' && str[i] <= '9') {
            digits++;
        } else if (str[i] == ' ') {
            spaces++;
        } else {
            others++;
        }
    }

    printf("letter:%d\n", alphabets);
    printf("digit:%d\n", digits);
    printf("space:%d\n", spaces);
    printf("others:%d\n", others);
}

int main() {
    char str[100];
    printf("string:My address is #256 Meisong Road,Meizhou.\n");
    gets(str);
    countCharacters(str);
    return 0;
}

方法2:

#include <iostream>
using namespace std; 

int Letter=0,Digit=0,Space=0,Others=0;
int main()
{   void count(char str[]);
    char string[80];
    gets(string);
    cout<<"string:";
    puts(string);
    count(string);
    cout<<"letter:"<<Letter<<endl;
    cout<<"digit:"<<Digit<<endl;
    cout<<"space:"<<Space<<endl;
    cout<<"others:"<<Others<<endl;
    return 0;
}
void count(char str[])
{   int i;
    for(i=0;str[i]!='\0';i++)
        if((str[i]>='a' && str[i]<='z')||(str[i]>='A' && str[i]<='Z'))
             Letter++;
        else if(str[i]>='0' && str[i]<='9')
             Digit++;
        else if(str[i]==' ')
             Space++;
        else
             Others++;
}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值