算法习题

// 打折.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 int a;
 double total;
 cout<<"请输入买衣服的件数";
 cin>>a;
 if(95*a>300)total=(95*a)*0.85;
 else total=95*a;
 cout<<total;
 return 0;
}


//// 距离.cpp : 定义控制台应用程序的入口点。


#include "stdafx.h"
#include<iostream>
//#include <math.h> 
//#include <cmath>
#include <math.h>//定义数学函数
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 float x1,y1,x2,y2,d;


 cout<<"请输入两个坐标(x1,y1),(x2,y2)";
 cin>>x1>>y1>>x2>>y2;
 d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
 cout<<d;

 return 0;
}

////绝对值.cpp : 定义控制台应用程序的入口点。


#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 float a;

 cout<<"请输入一个浮点数";
 cin>>a;

 cout<<(a>0?a:-a);
 return 0;
}

// 连续求和.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 int n,sum=0;
 cout<<"请输入一个正整数n:";
 cin>>n;
 while(n)
 {sum+=n--;}
 cout<<"sun="<<sum;
 return 0;
}

// 年份.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 int a;
 cout<<"请输入某一年:";
 cin>>a;
 if(a%4==0&&a%100!=0||a%400==0)cout<<"yes";
 else cout<<"no";
 return 0;
}

// 偶数.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 int a;
 cout<<"请输入一个整数";
 cin>>a;
 if(a%2==0)cout<<"yes";
 else cout<<"no";
 return 0;
}

// 求平均数.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 int a,b,c;
 float average=0.0;
 cout<<"please enter three numbers:";
 cin>>a>>b>>c;
 average=(a+b+c)/3.0;
 cout<<fixed<<setprecision(3)<<average;
 return 0;
}

// 求正弦和余弦.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
//#include <math.h> 
//#include <cmath>
#include <math.h>//定义数学函数
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 int x;
 double ysin,ycos;
 double pi=3.14;
 cout<<"请输入一个角的度数x:";
 cin>>x;
 ysin=sin((2*pi*x)/360.0);
 ycos=cos((2*pi*x)/360.0);
 cout<<"sin(x)="<<ysin<<endl;
 cout<<"cos(x)="<<ycos<<endl;
 return 0;
}

// 三角形.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 int a,b,c;
 
 cout<<"请输入三角形的三边";
 cin>>a>>b>>c;
 if(a+b>c && a+c>b && b+c>a)
  if(a*a==b*b+c*c || b*b==a*a+c*c || c*c==a*a+b*b)
   cout<<"yes";
  else cout<<"no";
 else cout<<"not a triangle";
 
 return 0;
}

// 温度转换.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 float f,c;
 cout<<"请输入华氏温度f:";
 cin>>f;
 c=5*(f-32)/9;
 cout<<fixed<<setprecision(3)<<c;
 return 0;
}

原文地址:https://www.cnblogs.com/ljy2013/p/3265093.html