U2-1-循环综合运用
2025-11-09 12:25:15
发布于:江苏
1阅读
0回复
0点赞
1. 第10课 选做题 差距
#include <iostream>
#include <cmath>
using namespace std;
int a[100005], b[100005], n;
int main(){
cin >> n;
for (int i=1; i<=n; i++) cin>>a[i];
for (int i=1; i<=n; i++) cin >> b[i];
for (int i=1; i<=n; i++){
cout << abs(a[i]-b[i]) << " ";
}
return 0;
}
/*
[差距]
题目描述
给定两组数据,求出每个数字之间相差多少。
提示
1≤n≤10^5,每组的数据在 ?1000~3000 内。
输入格式
共计三行,第一行一个整数 n 表示两组数据各 n 个数据。
第二行共 n 个数据,表示其中一组的数据。
第三行共 n 个数据,表示另外一组的数据。
输出格式
输出两组之间每个数据的差值。
样例组
输入#1
3
1 5 3
2 4 9
输出#1
1 1 6
*/
2. 打印分数
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,a[105]={};
cin >> n;
double sum = 0;
for(int i=1;i <= n;i++){
cin >> a[i];
sum += a[i];
}
double v = sum/n;
for(int i=1;i <= n;i++){
if(v>a[i]){
cout << a[i] << " ";
}
}
return 0;
}
2、输出因数
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
//输出n的所有因数
for (int i=1; i<=n; i++){
if (n%i==0){
cout << i << ' ';
}
}
// 2 3 5 7 11 13 17
// 约数/因数
//
// 12
// 1 2 3 4 6 12
//
//
// 13(质数)
// 1 13
return 0;
}
3. 质数判断的方法(两种)
#include <bits/stdc++.h>
using namespace std;
int main(){
//方法2: 除了1和它本身, 没有其他的约数
int n; cin>>n;
if (n<2) {
cout << "no";
return 0;
}
for(int i=2; i<=n-1; i++){
if (n%i==0){
cout<<"no";
return 0;
}
}
cout <<"yes";
return 0;
}
#if 0
int n, cnt = 0;
cin >> n;
//方法1: 质数的因数只有 2个
for (int i=1; i<=n; i++){
if (n%i == 0) cnt++;
}
if (cnt==2) cout<<"yes";
else cout << "no";
#endif
4.最大公约数
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
for (int i=n; i>=1; i--){
if (n%i==0 && m%i==0){
cout << i <<' ';
return 0;
}
}
return 0;
}
/*
12: 1 2 3 4 6 12
16: 1 2 4 8 16
*/
这里空空如也



有帮助,赞一个