计算机程序设计C++ MOOC
测试与作业C++基础练习100题
##第二周基本练习
- 求过平面上两点的直线的斜率
#include <iostream>
using namespace std;
int main()
{
double x1,y1,x2,y2;
cin >> x1;
cin >> y1;
cin >> x2;
cin >> y2;
cout <<(y2-y1)/(x2-x1)<< endl;
return 0;
}
- 计算平面上两点之间的距离
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x1,y1,x2,y2;
cin >> x1;
cin >> y1;
cin >> x2;
cin >> y2;
cout <<sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))<< endl;
return 0;
}
- 判断大小写
#include "iostream"
using namespace std;
int main()
{
char a;
cin >> a;
if (a >= 'A'&&a <= 'Z')
{
cout << "1" << endl;
}
else
{
cout << "0" << endl;
}
return 0;
}
- 判断数字
#include "iostream"
using namespace std;
int main()
{
char a;
cin >> a;
if (a >= '0'&&a <= '9')
{
cout << "1" << endl;
}
else
{
cout << "0" << endl;
}
return 0;
}
- 判断闰年
#include "iostream"
using namespace std;
int main()
{
int year;
cin >> year;
if ((year%4==0&&year%100!=0)||year%400==0)
{
cout << "IsLeapYear" << endl;
}
else
{
cout << "NotLeapYear" << endl;
}
return 0;
}
- 求商和余数
#include "iostream"
using namespace std;
int main()
{
int a, b;
cin >> a;
cin >> b;
cout << a / b << " " << a%b << endl;
return 0;
}
- 计算平均分取整
#include "iostream"
using namespace std;
int main()
{
int grade[7],ave,i;
double sum;
ave = 0;
sum = 0;
for (i = 0; i < 7; i++)
{
cin >> grade[i];
sum = sum + grade[i];
}
ave = int(sum / 7.0 + 0.5);
cout << ave << endl;
return 0;
}
- 计算点到直线的距离保留两位小数
#include "iostream"
#include <cmath>
using namespace std;
int main()
{
double A, B, C, x, y,distance;
cin >> A >> B >> C;
cin >> x >> y;
distance = abs(A*x + B*y + C) / sqrt(A*A + B*B);
distance = int(100 * distance + 0.5) / 100.00;
cout << distance << endl;
return 0;
}
- 输入字符显示ASCII值
#include "iostream"
using namespace std;
int main()
{
char a;
cin >> a;
cout << a + 0 << endl;
return 0;
}
- 输入整数显示ASCII字符
#include "iostream"
using namespace std;
int main()
{
int a;
cin >> a;
cout << (char)a << endl;
return 0;
}
- 输入整数显示十六进制
#include "iostream"
using namespace std;
int main()
{
int n;
cin >> n;
cout << hex << n << endl;
return 0;
}
- 输入整数显示十六进制和八进制
#include "iostream"
#include <cmath>
using namespace std;
int main()
{
int n;
cin >> n;
if (n >= 0)
{
cout << dec << n<<" "<< hex << n <<" "<< oct << n;
}
else
{
cout << "-" << dec << abs(n) << " -" << hex << abs(n) << " -" << oct << abs(n);
}
return 0;
}
以上为第二次基础练习。