某农业大学c/c++第三次实验

1.3-20 递归求和

【问题描述】

请编制递归函数计算 sn=1+2+3+……+n,并在main函数中调用它。

【输入形式】

一行一个整数n
【输出形式】
求和的结果
【样例输入】

5
【样例输出】

15
【评分标准】

要求使用递归实现

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n,sum;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		sum+=i;
	}
	cout << sum;
}

2.3-11 最大公因数和最小公倍数

【问题描述】请输入两个整型数,编写两个函数分别实现这两个数的最小公倍数和最大公约数,并在main函数中调用它。

【输入形式】第一行一个整数n,表示有n组输入,后面紧跟n行,每一行两个正整数。
【输出形式】输出n行,每一组对应输出一行,每行第一个数表示两个数的最大公因数,第二个表示最小公倍数。
【样例输入】

3

20 28

36 54

12 33

【样例输出】

4 140

18 108

3 132

#include<bits/stdc++.h>
using namespace std;

int f1(int x,int y)
{
	int s;
	while(y!=0)
	{
		s=x%y;
		x=y;
		y=s;
	}
	return x;
}

int f2(int x,int y)
{
	int s=f1(x,y);
	return x*y/s;
}

int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		cout << f1(a,b)<<" ";
		cout << f2(a,b)<<endl;
	}
}

3.3-6-2 回文串

【问题描述】

请输入一个字符串至一维字符数组s中,并判断它是否是为回文串。

【输入形式】

一行一个字符串
【输出形式】

如果字符串是回文串输出"yes",否则输出"no"
【样例输入1】

abcddcba
【样例输出1】

yes

【样例输入2】

abcedcba
【样例输出2】

no

#include<bits/stdc++.h>
using namespace std;

int main()
{
	string a;
	cin >> a;
	//cout << a<<endl;
	int i=0;
	int j=a.size()-1;
	int x=1;
	
	for(i=0;i<j;i++,j--)
	{
		if(a[i]!=a[j])
		{
			x=0;
			break;
		}	
	}
	
	if(x==0)
	cout << "no";
	else
	cout << "yes";
}

4.3-4 删除数据

【问题描述】

请输入10个整数至一维数组a中,并输入一个待删除的整数n,若n在数组a中则数组中所有值为n的数都从数组a中删除,否则输出“输入数据不存在”。

【输入形式】

输入包括两行,第一行十个整数,第二行一个整数n。
【输出形式】

如果数组中出现过数值为n的数,那么输出删除完数值为n后的数组。

如果数组中不存在数值为n的数,那么输出”输入数据不存在“
【样例输入1】

1 2 3 4 5 6 7 8 9 10

6
【样例输出1】

1 2 3 4 5 7 8 9 10

【样例输入2】

1 2 3 4 5 6 7 8 9 10

0
【样例输出2】

输入数据不存在

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int a[10],n,s=0;
	for(int i=0;i<=9;i++)
	{
		cin>>a[i];
	}
	cin >> n;
	
	int flag=0;
	for(int i=0;i<10-s;i++)
	{
		if(n==a[i])
		{
			flag=1;
			for(int j=i;j<10-s;j++)
			{
				a[j]=a[j+1];
			}
			s++;
			i=-1;
		}
		else
		{
			continue;
		}
	}
	if(flag==1)
	{
		for(int i=0;i<10-s;i++)
		{
			cout << a[i]<<" ";
		}
	}
	else
	{
		cout<< "输入数据不存在";
	}
}
5.统计字母与单词

【问题描述】

给定一句英语句子和一个英语字母,统计给出英文字母在句子中出现的次数以及包含给出英文字母的单词数。

测试样例保证句子中只含空格,","和"."三种特殊符号,不含有任何缩写。

【输入形式】从标准输入输入句子与英文字母,第一行为英文字母,第二行为句子,以"."结尾。

【输出形式】输出给出的英文字母在文章中出现的次数以及包含给出英文字母的单词数,空格隔开,区分大小写。

【样例输入】

o

stay foolish,stay hungery.

【样例输出】2 1
【样例说明】
【评分标准】

#include<bits/stdc++.h>
using namespace std;

int main()
{
	char s1[100],s2[100],ch;
	int i=0,a1=0,a2=0;
	
	cin>>s1;
	while(s1[a1]!='\0') a1++;
	
	
	cin >> ch;
	while(ch!='.')
	{
		if(ch!='\0')
		{
			s2[i++]=ch;
		}
		cin>>ch;
	}
	a2=i;
	
	
	int num=0;
	for(int i=0;i<a2;i++)
	{
		for(int j=0;j<=a1;j++)
		{
			if(s1[j]==s2[i])
			{
				num++;
			}
		}
	}
	cout << num<<" "<< a1;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值