c++编程题目 100
展开全部
#include <iostream>
using namespace std;
#define SWAP(a, b) {a^=b; b^=a; a^=b;}
#define PI 3.1415926
#define P 1e-6
// 求和
int f(int n, int m)
{
int sum = 0;
while( n <= m )
sum += (n++);
return sum;
}
// 圆环面积
double area(double r1, double r2)
{
if( r1<P || r2<P || r1-r2 > P )
return 0.0;
else
return PI * (r2*r2 - r1*r1);
}
// 圆环周长
double length(double r1, double r2)
{
if( r1<P || r2<P )
return 0.0;
else
return 2.0 * PI * (r1 + r2);
}
// 素数判断
int isPrime(int x)
{
if( x == 2 )
return 1;
if( x<2 || !(x&1) )
return 0;
for(int i = 3; i*i <= x; i+=2)
if( x%i == 0 )
return 0;
return 1;
}
// 最大公约数
unsigned gcd(unsigned x, unsigned y)
{
if( x < y ) SWAP(x, y);
while( y > 0 ){
unsigned t = x % y;
x = y;
y = t;
}
return x;
}
// 最小公倍数
unsigned lcm(unsigned x, unsigned y)
{
return x * y / gcd(x,y);
}
int main(void)
{
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
因为字数限制,我就只写方法,你自己写实现的过程
1.
#include <iostream>
using namespace std;
int fum(int n,int m){
int sum=0,i,temp;
if(n>m){
temp=n;
n=m;
m=temp;
}
for(i=n;i<=m;i++){
sum+=i;
}
return sum;
}
2.
#include <iostream>
using namespace std;
void area(double r1,double r2){
double temp;
if(r1<r2){
temp=r1;
r1=r2;
r2=temp;
}
cout<<"内环为"<<r1<<",外环为"<<r2<<"的圆环面积是:"<<3.14*(r2-r1)*(r2-r1)<<endl;
cout<<"内环为"<<r1<<",外环为"<<r2<<"的圆环周长是:"<<2*3.14*(r2+r1)<<endl;
}
3.
#include <iostream>
using namespace std;
int prime(int n){
int count=0,i;
for(i=2;i<=n;i++){
if(n%i==0){
count++;
}
}
if(count>=2){
return 0;
}else{
return 1;
}
}
4.
#include <iostream>
using namespace std;
int maxGYS(int numA,int numB){
int temp,result=0;
if(numA<numB){
temp=numA;
numA=numB;
numB=temp;
}
result=numA%numB;
while(result!=0){
numA=numB;
numB=result;
result=numA%numB;
}
return numB;
}
int minGBS(int numA,int numB){
return numA*numB/maxGYS(numA,numB);
}
1.
#include <iostream>
using namespace std;
int fum(int n,int m){
int sum=0,i,temp;
if(n>m){
temp=n;
n=m;
m=temp;
}
for(i=n;i<=m;i++){
sum+=i;
}
return sum;
}
2.
#include <iostream>
using namespace std;
void area(double r1,double r2){
double temp;
if(r1<r2){
temp=r1;
r1=r2;
r2=temp;
}
cout<<"内环为"<<r1<<",外环为"<<r2<<"的圆环面积是:"<<3.14*(r2-r1)*(r2-r1)<<endl;
cout<<"内环为"<<r1<<",外环为"<<r2<<"的圆环周长是:"<<2*3.14*(r2+r1)<<endl;
}
3.
#include <iostream>
using namespace std;
int prime(int n){
int count=0,i;
for(i=2;i<=n;i++){
if(n%i==0){
count++;
}
}
if(count>=2){
return 0;
}else{
return 1;
}
}
4.
#include <iostream>
using namespace std;
int maxGYS(int numA,int numB){
int temp,result=0;
if(numA<numB){
temp=numA;
numA=numB;
numB=temp;
}
result=numA%numB;
while(result!=0){
numA=numB;
numB=result;
result=numA%numB;
}
return numB;
}
int minGBS(int numA,int numB){
return numA*numB/maxGYS(numA,numB);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第1题
#include stdio.h
int f(int n,int m);
void main()
{
int n,m;
scanf("%d%d",&n,&m);
printf("%d",f(n,m));
}
int f(int n,int m)
{
int i,s=0;
for(i=n;i<=m;i++)
s+=i;
return(s);
}
#include stdio.h
int f(int n,int m);
void main()
{
int n,m;
scanf("%d%d",&n,&m);
printf("%d",f(n,m));
}
int f(int n,int m)
{
int i,s=0;
for(i=n;i<=m;i++)
s+=i;
return(s);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询