C语言/C++常见习题问答集锦(十四)

C语言/C++常见习题问答集锦(十四)

程序之美

在这里插入图片描述

1、输入n个字符串,将它们按字母由小到大的顺序排列并输出(不用指针,不定义函数,用二维数组)

#include<iostream.h>
//排序函数
void order(char *p,char *q){
	char t;
	t=*p;
	*p=*q;
	*q=t;
}
void main(){
	int n,i,j,k;
	cout<<"Please input the number:";
	cin>>n;//自己定义数组长度
	char *arr=new char[n];
	for(i=0;i<n;i++)
		cin>>arr[i];
	for(i=0;i<n-1;i++)
	{
		k=i;
		for(j=i+1;j<n;j++)
			if(arr[j]<arr[k]) k=j;//找出最小字符
		if(i!=k)
			order(&arr[k],&arr[i]);//交换位置
	}
	for(i=0;i<n;i++)
		cout<<arr[i];
	delete[] arr;//释放内存
}

2、c语言求数字、字母和其他字符出现的个数,然后把这个字符串逆序

解法一:

#include <stdio.h>
#include <string.h>
int main()
{
      int i,n,na=0,n1=0,np =0;
      char s[1000];
      gets(s);
      n = strlen(s);
      for(i=0;i<n;i++)
      {
          if(s[i]>='0' && s[i] <='9')
              n1++;
          else if((s[i] >='A' && s[i]<='Z') || (s[i]>='a' && s[i] <='z'))
             na++;
          else
             np++;
      }
      for(i=n-1;i>=0;i--)
          printf("%c",s[i]);
      printf("\n");
      printf("数字%d个,字母%d个,其它%d个",n1,na,np);
      return 0;
}

解法二:

#include<stdio.h>
 
void Reverse(char str[]){
    int n=strlen(str);
    int i;
    char temp;
    for(i=0;i<(n/2);i++){
        temp=str[i];
        str[i]=str[n-i-1];
        str[n-i-1]=temp;
}
 
int main(int arge,char *argv[])
{
	char chs[2048];
	char ch;
	scanf("%s", chs);
	int alp=0,num=0,oth=0,len,alp_start,num_start,oth_start,i;
	int len = strlen(chs);
	int i = 0;
	while(i < len)
	{
		  ch=chs[i];
		  if(ch>='0'&&ch<='9') num++;
		  else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
		  alp++;
		  else
		  oth++;
		  i ++;
	}
	len=alp>num?(alp>oth?alp:oth):(num>oth?num:oth);
	alp_start=len+1-alp;
	num_start=len+1-num;
	oth_start=len+1-oth;
	printf("alp: %d num: %d oth: %d\n",alp,num,oth);
	printf("%5s%8s%5s%8s%5s\n","alp"," ","num"," ","oth");
	 
	Reverse(chs);
	printf("%s\n",chs);
	return 0;
}

在这里插入图片描述

3、编写一个程序,将获得一系列无符号长数字,一个接一个,直到得到-1。-1不是该系列的一部分。该系列至少有2个数字(在-1之前)。所有数字都是合法的。程序将显示所有数字的gcd。然后它将显示第二大gcd,然后它将显示第三大gcd。所有3个gcd将依次显示在同一行中,而它们之间正好显示1个空格。该行中没有其他空格。如果不存在第三大gcd,则将显示1。第二大gcd也是如此。

后续补充此题解答。

4、身高段分150以下(1档)、150-154(2档)、155-159(3档)…180-184(8档)、185-189(9 档)、189(10档)以上10个档次。要求程序中尽可能简短,程序可不考虑输入数据错误。
【输入形式】
输入20名学生的身高,其间以1个空格分隔。
【输出形式】
顺序输出档次及档次所在人数,其间以1个空格分隔。档次之间换行。
【样例入】
145150155160165170175180185190148154158163168173178181177180
【样例输出】
12
22
32
42

#include <stdio.h>
int main()
{
    int m;
    int a,b,c,d,e,f,g,h,j,k=0;
    printf("请输入20位学生的身高:");
    for (int i=0;i<20;i++){
        scanf("%d",&m);
        if(m<150)
            a++;
        else if(m>=150&&m<155)
            b++;
        else if(m>=155&&m<160)
            c++;
        else if(m>=160&&m<165)
            d++;
        else if(m>=165&&m<170)
            e++;
        else if(m>=170&&m<175)
            f++;
        else if(m>=175&&m<180)
            g++;
        else if(m>=180&&m<185)
            h++;
        else if(m>=185&&m<190)
            j++;
        else if (m>=190)
            k++;
    }
    printf("150以下的有:%d,150~154:%d,155~160:%d,160~165:%d,165~170:%d,170~175:%d,175~180:%d,180~185:%d,185~190:%d,大于190:%d,\n",a,b,c,d,e,f,g,h,j,k);
 
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五一编程

程序之路有我与你同行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值