day8 homework

This commit is contained in:
2023-06-24 10:01:33 +08:00
parent af5944bc12
commit 6859943be2
22 changed files with 776 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
// 输入一个整数 n,然后输入 n 个整数,输出它们的平均值。
#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
int nums[100];
cout << "请输入整数 n: ";
cin >> n;
int n_copy = n;
cout << "请输入 " << n << " 个整数: ";
while (--n >= 0)
{
cin >> nums[n];
sum += nums[n];
}
cout << "平均值为:" << sum / n_copy << endl;
return 0;
}
+50
View File
@@ -0,0 +1,50 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有质数的个数。
#include <iostream>
using namespace std;
bool isPrime(int num)
{
if (num <= 1) // 1 不是质数
return false;
int i = 2;
while (i * i <= num)
{
if (num % i == 0)
return false;
else
i++;
}
return true;
}
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (isPrime(nums[n]) == true)
cnt++;
else
continue;
}
cout << "输入的整数中所有质数的个数为: " << cnt << endl;
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 整除的数的个数。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 3 == 0)
cnt++;
else
continue;
}
cout << "输入的整数中所有能被 3 整除的数的个数: " << cnt << endl;
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 5 整除的数的个数。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 5 == 0)
cnt++;
else
continue;
}
cout << "输入的整数中所有能被 5 整除的数的个数: " << cnt << endl;
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 7 整除的数的个数。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 7 == 0)
cnt++;
else
continue;
}
cout << "输入的整数中所有能被 7 整除的数的个数: " << cnt << endl;
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 和 5 整除的数的个数。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 3 == 0 && nums[n] % 5 == 0)
cnt++;
else
continue;
}
cout << "输入的整数中所有能被 3 和 5 整除的数的个数: " << cnt << endl;
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 整除的数的个数。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 3 == 0 || nums[n] % 5 == 0)
cnt++;
else
continue;
}
cout << "输入的整数中所有能被 3 或 5 整除的数的个数: " << cnt << endl;
return 0;
}
+35
View File
@@ -0,0 +1,35 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 但不能被同时整除的数的个数。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 3 == 0 && nums[n] % 5 == 0)
continue;
else if (nums[n] % 3 == 0 || nums[n] % 5 == 0)
cnt++;
else
continue;
}
cout << "输入的整数中所有能被 3 或 5 但不能被同时整除的数的个数: " << cnt << endl;
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 整除的数的和。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 3 == 0 || nums[n] % 5 == 0)
cnt += nums[n];
else
continue;
}
cout << "输入的整数中所有能被 3 或 5 整除的数的和: " << cnt << endl;
return 0;
}
+35
View File
@@ -0,0 +1,35 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 但不能被同时整除的数的和。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 3 == 0 && nums[n] % 5 == 0)
continue;
else if (nums[n] % 3 == 0 || nums[n] % 5 == 0)
cnt += nums[n];
else
continue;
}
cout << "输入的整数中所有能被 3 或 5 但不能被同时整除的数的和: " << cnt << endl;
return 0;
}
+38
View File
@@ -0,0 +1,38 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 整除的数的平均值。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int sum = 0; // 和
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 3 == 0 || nums[n] % 5 == 0)
{
cnt++;
sum += nums[n];
}
else
continue;
}
cout << "输入的整数中所有能被 3 或 5 整除的数的平均值: " << sum / (double)cnt << endl;
return 0;
}
+25
View File
@@ -0,0 +1,25 @@
// 输入一个整数 n,然后输入 n 个整数,输出它们的平均值。
// 方法 2
#include <iostream>
using namespace std;
int main()
{
int n;
double avg = 0;
int nums[100];
cout << "请输入整数 n: ";
cin >> n;
int n_copy = n;
cout << "请输入 " << n << " 个整数: ";
while (--n >= 0)
{
cin >> nums[n];
avg += nums[n] / (double)n_copy;
}
cout << "平均值为:" << avg << endl;
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
// 输入一个整数 n,然后输入 n 个整数,输出它们的平均值。
// 方法 3
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入整数 n: ";
cin >> n;
int *nums = new int[n];
double avg = 0.0;
int i = 0;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
avg += nums[i] / (double)n;
i++;
}
cout << "平均值为:" << avg << endl;
return 0;
}
+34
View File
@@ -0,0 +1,34 @@
// 输入一个整数 n,然后输入 n 个整数,输出它们的最大值和最小值。
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入整数 n: ";
cin >> n;
int *nums = new int[n];
int max = nums[0], min = nums[0];
int i = 0;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
if (max < nums[i])
max = nums[i];
if (min > nums[i])
min = nums[i];
i++;
}
cout << "最大值为: " << max << ",\t"
<< "最小值为: " << min << endl;
return 0;
}
+39
View File
@@ -0,0 +1,39 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 但不能被同时整除的数的平均值。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int sum = 0; // 和
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 3 == 0 && nums[n] % 5 == 0)
continue;
else if (nums[n] % 3 == 0 || nums[n] % 5 == 0)
{
cnt++;
sum += nums[n];
}
else
continue;
}
cout << "输入的整数中所有能被 3 或 5 但不能被同时整除的数的平均值: " << sum / (double)cnt << endl;
return 0;
}
+52
View File
@@ -0,0 +1,52 @@
// 输入一个整数 n,然后输入 n 个整数,将它们按从小到大的顺序排序,并输出排序后的结果。
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入整数 n: ";
cin >> n;
int *nums = new int[n];
int i = 0;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
cout << "排序前的结果: ";
for (int i = 0; i < n; i++)
{
cout << nums[i] << " ";
}
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (nums[j] > nums[j + 1])
{
/* int temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp; */
nums[j] = nums[j] ^ nums[j + 1];
nums[j + 1] = nums[j] ^ nums[j + 1];
nums[j] = nums[j] ^ nums[j + 1];
}
}
}
cout << "从小到大排序后的结果: ";
for (int i = 0; i < n; i++)
{
cout << nums[i] << " ";
}
return 0;
}
+49
View File
@@ -0,0 +1,49 @@
// 输入一个整数 n,然后输入 n 个整数,将它们按从大到小的顺序排序,并输出排序后的结果。
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入整数 n: ";
cin >> n;
int *nums = new int[n];
int i = 0;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
cout << "排序前的结果: ";
for (int i = 0; i < n; i++)
{
cout << nums[i] << "\t";
}
cout << endl;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (nums[j] < nums[j + 1])
{
nums[j] = nums[j] ^ nums[j + 1];
nums[j + 1] = nums[j] ^ nums[j + 1];
nums[j] = nums[j] ^ nums[j + 1];
}
}
}
cout << "从大到小排序后的结果: ";
for (int i = 0; i < n; i++)
{
cout << nums[i] << "\t";
}
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有奇数的和。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int sum = 0;
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 2 == 1)
sum += nums[n];
}
cout << "输入的整数中所有奇数的和为: " << sum << endl;
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有偶数的和。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int sum = 0;
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 2 == 0)
sum += nums[n];
}
cout << "输入的整数中所有偶数的和为: " << sum << endl;
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有正数的和。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int sum = 0;
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] > 0)
sum += nums[n];
}
cout << "输入的整数中所有正数的和为: " << sum << endl;
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有负数的和。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int sum = 0;
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] < 0)
sum += nums[n];
}
cout << "输入的整数中所有负数的和为: " << sum << endl;
return 0;
}
+50
View File
@@ -0,0 +1,50 @@
// 输入一个整数 n,然后输入 n 个整数,输出其中所有素数的个数。
#include <iostream>
using namespace std;
bool isPrime(int num)
{
if (num <= 1) // 1 不是质数
return false;
int i = 2;
while (i * i <= num)
{
if (num % i == 0)
return false;
else
i++;
}
return true;
}
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (isPrime(nums[n]) == true)
cnt++;
else
continue;
}
cout << "输入的整数中所有素数的个数为: " << cnt << endl;
return 0;
}