数组实验答疑

数组实验答疑


第一题:

#include"iostream"
using namespace std;
int main()
{
	double a[10],sum=0;
	for(int i=0;i<10;i++)
	{
		cin>>a[i];
		sum=sum+a[i];
	}
	double avg=sum/10;
	cout<<"平均成绩为:"<<avg<<endl;
	for(int i=0;i<10;i++)
	{
		if(a[i]<avg)
		{
			cout<<a[i]<<endl;
		}
	}
	return 0;
}

第二题:

#include"iostream"
using namespace std;
#include"string.h"
int main()
{
	char a[100],b[100];
	cin>>a>>b;
	int x=0;
	int alen=strlen(a);
	int blen=strlen(b);
	int maxlen=alen>blen?alen:blen;
	int small=alen<blen?alen:blen;
	
	for(int i=0;i<small;i++)
	{
		if(a[i]>b[i])
		{
			x=1;
			cout<<1<<endl;
			break;
		}
		if(a[i]<b[i])
		{
			x=-1;
			cout<<-1<<endl;
			break;
		}
	}
	if(x==0&&maxlen==small)
	{
		cout<<0<<endl;
	}
	if(x==0&&maxlen==alen)
	{
		cout<<1<<endl;
	}
	else
	{
		cout<<-1<<endl;
	}
	return 0;
}

第三题:

#include"iostream"
using namespace std;
int main()
{
	int a[10];
	for(int i=0;i<10;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<10;i++)
	{
		for(int j=i;j<10;j++)
		{
			if(a[i]<a[j])
			{
				int temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
		}
	}
	return 0;
}


第四题:

#include"iostream"
using namespace std;
int main()
{
	int a[5][5];
	
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<=i;j++)
		{
			a[i][j]=1;
		}
		cout<<endl;
	}
	for(int i=2;i<5;i++)
	{
		for(int j=1;j<i;j++)
		{
			a[i][j]=a[i-1][j-1]+a[i-1][j];
		}
	}
	
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<=i;j++)
		{
			cout<<a[i][j]<<"  ";
		}
		cout<<endl;
	}
	return 0;
}

第五题:

#include"iostream"
using namespace std;
#include"string.h"
int main()
{
	char a[100];
	gets(a);
	cout<<a<<endl;
	for(int i=0;a[i]!='\0';i++)
	{
		if(a[i]==' ')
		{
			int j=i;
			while(a[j]!='\0')
			{
				a[j]=a[j+1];
				j++;
			}
		}
		i--;
	}
	cout<<a;
	return 0;
}

第五题解法2:

#include"iostream"
using namespace std;
#include"string.h"
int main()
{
	char arr[100];
	cin.getline(arr,100);
	char p[100];
	int j=strlen(arr);
	int static temp=0;
	for(int i=0;arr[i]!='\0';i++)
	{
		
		if(arr[i]!=' ')
		{
			p[temp++]=arr[i];
		}
	}
	cout<<p;
	return 0;
}

第六题

#include"iostream"
using namespace std;
#include"string.h"
int main()
{
	char a[3][80];
	int big=0,small=0,num=0,space=0,other=0;
	
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<80;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<80;j++)
		{
			if(a[i][j]>='A'&&a[i][j]<='Z')
			{
				big++;
			}
			else if(a[i][j]>='a'&&a[i][j]<='z')
			{
				small++;
			}
			else if(a[i][j]>=' ')
			{
				space++;
			}
			else
			{
				other++;
			}
		}
	}
	return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值