c++基础编程(一)

输入3个double 类型的值,判断这3个值是否可以表示一个三角形的三条边

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	double a,b,c;
	cin>>a>>b>>c;
	if(a+b>c&&a+c>b&&b+c>a)
	cout<<"可以构造三角形"<<endl;
	else
	cout<<"不可以构造三角形"<<endl;
	return 0;
 } 

输入20个数,求其最大,最小和平均值

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

int main()
{
	double sum=0;
	int num[1001];
	int i;
	for(i=0;i<20;i++)
	{
		cin>>num[i];
	}
	for(i=0;i<20;i++)
	{
		sum+=num[i];
	}
	sort(num,num+20);
	cout<<"max="<<num[19]<<endl;
	cout<<"min="<<num[0]<<endl;
	cout<<"average="<<sum/20.0<<endl;
	return 0;
	
}

求和 s=1! + 2! + 3! +…+ 10! 采用递归的方法

#include<bits/stdc++.h>
using namespace std;
//使用递归的方法
int fun(int n)
{
	if(n==1)
	return 1;
	else
	return fun(n-1)*n;  //这个地方不要写反了,不要写成function(n)*(n-1) 
 } 
 int main()
 {
 	int sum,i;
 for(i=1;i<=10;i++)
 {
 	int t=fun(i);
 	sum+=t;
 }
 cout<<sum<<endl;
}
 

计算PI/4=1-1/3+1/5-1/7+… PI的近似值, 最后一项的绝对值小于1e-6为止

#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;


int main()
{
	double pi = 0,t=1;
	int s = 1;
	int i = 1;
	while(fabs(t)>=1e-6)
	{
		pi += t;
		s = -s;
		i = i + 2;
		t = double(s) / i;
	}
	cout << 4*pi << endl;
	return 0;
}

写一函数,计算x的y次方

#include<bits/stdc++.h>
using namespace std;
int pow(int x,int y)
{
	int s=1,i;
	for(i=1;i<=y;i++)
	{
		s=s*x;
	}
	return s;
}
int main()
{
	int x,y;
	cin>>x>>y;
	int cnt = pow(x,y);
	cout<<cnt<<endl;
}

编写一个函数,确定一个整数是否为完全数(一个数,等于他的因子之和)。用这个函数确定和打印1到1000之间的所有完全数

#include<bits/stdc++.h>
using namespace std;
void Wanshu( )
{
	int i,j;
	for(i=1;i<=1000;i++)
	{
		int s=0;
		for(j=1;j<i;j++)
		{
			if(i%j==0)
			{
				s+=j;
			}
		}
		if(s==j)
		{
			cout<<j<<" ";
		}
	}
}
int main()
{
	Wanshu();
	return 0;
}

写一函数,求斐波那契数列的第n项。

#include<bits/stdc++.h>
using namespace std;
//返回的是第几斐波那契数列 
int fun(int n)
{
	if(n==1||n==2)
	return 1;
	else
	 return fun(n-1)+fun(n-2);
}
int main()
{
	int n,i;
	cin>>n;
	for(i=1;i<=n;i++)
	{
	
	cout<<fun(i)<<" ";   //调用i 
	if(i%4==0)
	{
		cout<<endl;
	}
}
	return 0;
}

写一个函数,交换两个整型变量的值

#include<bits/stdc++.h>
using namespace std;
void swap(int &a,int &b)
{
	int temp;
    temp=a;
    a=b;
    b=temp;
}
int main()
{
	int a,b;
	cin>>a>>b;
	swap(a,b);
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;
}

求两个数的最大公约数,欧几里德算法(辗转相除法)

#include<bits/stdc++.h>
using namespace std;
int gdc(int a,int b)
{
	if(a%b==0)
	return b;
	else
	return gdc(b,a%b);
}
int main()
{
	int a,b;
	cin>>a>>b;
	cout<<gdc(a,b);
}

求两个数的最小公倍数
//两个数的乘积等于这两个数的最大公约数与最小公倍数的积

#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;

int gcd(int a, int b)
{
	if (a%b == 0)
		return b;
	else
		return gcd(b, a%b);
}
int main()
{
	int a, b;
	cin >> a >> b;
	cout << "最小公倍数:" << a*b/gcd(a, b) << endl;
	return 0;
}


输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数
// while((x=getchar())!='\n)

#include<stdio.h>
int main()
{
	int a=0,b=0,c=0,d=0;//a存储英文字母个数,b存储空格的个数,c存储数字的个数,d存储其他字符个数 
	char x;
	printf("输入一行字符串:\n");
	while((x=getchar())!='\n')//对字符串中的每一个字符进行比较判断 
	{
		if(x>='a'&&x<='z'||x>='A'&&x<='Z')
		a++;
		else if(x==' ')
		b++;
		else if(x>='1'&&x<='9')
		c++;
		else d++;
	}
	printf("英文字母的个数为:%d\n",a);
	printf("空格的个数为: %d\n",b);
	printf("数字的个数为: %d\n",c);
	printf("其他字符的个数为: %d\n",d);
}

写一个程序,进行体操评分,依次输入10名评委所评分数,去除一个最高分和一个最低分,再算出平均分作为选手的得分。

int main()
{ int i;
  float max,min,s,x;
  max = 0; min = 10; s=0;
  for(i=1;i<=10;i++)
  { cin >> x;
    s = s + x;
    if(x<min) min = x;
    if(x>max) max = x;
  }
  s = s - min - max;
  cout << s/8;
} 

写一函数,将一数组中的元素反转

//数组元素反转 
#include<bits/stdc++.h>
using namespace std;
void reverse(int a[],int n)
{
	int temp;
	for(int i=0;i<n/2;i++)
	{
		 temp =a[i];
		a[i]=a[n-1-i];
		a[n-i-1]=temp;
	}
}
int main()
{
	int n;
	int a[100];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	reverse(a,n);
	for(int i=0;i<n;i++)
	{
		cout<<a[i]<<" ";
	}
}

写一函数,在一个数组中找出最大元素的位置
//设置最大的数为 a[0],查找的时候还是可以从a[0]开始查找

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;

int fun(int a[], int n)
{
	int max=a[0], x;
	for (int i = 0; i < n; i++)
	{
		if (a[i] >= max)  //这里要设置为>=
		{
			max = a[i];
			x = i;
		}
	}
	return x;
}

int main()
{
	int n, a[100];
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cout << "最大值的位置:"<<fun(a, n) << endl;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值