第一题、闰年展示![](https://i-blog.csdnimg.cn/direct/4afe9c301a9f4425bcff09e2f8e35a09.png)
题解:
#include <iostream>
using namespace std;
//闰年
//能被4整除,但不能被100整除
//能被400整除
int main()
{
int x = 0, y = 0, num = 0;
int arr[1000] = { 0 };
cout << "请输入两个年份:" << endl;
cin >> x >> y;
for (int i = x; i <= y; i++)
{
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
{
arr[num] = i;
num++;
}
}
cout << num << endl;
for (int i = 0; i < num; i++)
{
cout << arr[i] << " ";
//if ((i + 1) % 10 == 0)//每行输出10个数
//{
// cout << endl;
//}
}
system("pause");
return 0;
}
第二题:质数筛
![](https://i-blog.csdnimg.cn/direct/4b31d8cbfade4e3a84ce3eb6a955454a.png)
题解:
01、
#include <iostream>
using namespace std;
bool func(int n)
{
if (n == 1||n == 0 )
{
return false;
}
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
int main()
{
int n = 0, mark;
int arr[100];
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n; i++)
{
if (func(arr[i]))
{
cout << arr[i] << endl;;
}
}
system("pause");
return 0;
}
02、
#include <iostream>
using namespace std;
int main()
{
int n = 0, mark;
int arr[100];
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n; i++)
{
int mark=0;
for (int j = 2; j < arr[i]; j++)
{
if (arr[i] % j == 0)
{
mark = 1;
break;
}
}
if (mark == 0)
{
cout << arr[i]<<" ";
}
}
system("pause");
return 0;
}
三、距离函数
![](https://i-blog.csdnimg.cn/direct/2cb788566f4a449d9d1f2829865005c4.png)
题解:
#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double dis(double x1, double y1, double x2, double y2)
{
double ans;
ans = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
ans = sqrt(ans);
return ans;
}
int main()
{
double x[3] = { 0 }, y[3] = { 0 }, ab, ac, bc, C;
cin >> x[0] >> y[0] >> x[1] >> y[1] >> x[2] >> y[2];
ab=dis(x[0], y[0], x[1], y[1]);
ac=dis(x[0], y[0], x[2], y[2]);
bc=dis(x[1], y[1], x[2], y[2]);
C = ab + ac + bc;
printf("%.2f", C);
system("pause");
return 0;
}