写出洛谷P4089的C++代码
时间: 2023-11-13 19:42:50 浏览: 52
当然,以下是洛谷P4089的C++代码:
```cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n+1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> b(n);
for (int i = 1; i <= n; i++) {
b[i-1] = a[i] - a[i-1];
}
vector<int> c(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
}
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += b[i] * c[i];
}
cout << ans << endl;
return 0;
}
```
这段代码可以实现洛谷P4089的功能,即输入一个整数n,以及两个长度为n的数组a和c,然后计算并输出a和c的内积。
相关问题
写出洛谷P1001的C++代码
当然,以下是洛谷P1001的C++代码:
```cpp
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
```
这段代码可以实现洛谷P1001的功能,即输入两个整数a和b,然后输出它们的和。
写一下洛谷P4718的C++代码
当然,以下是洛谷P4718的C++代码:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main() {
int n;
cin >> n;
vector<int> primes;
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
primes.push_back(i);
}
}
int count = 0;
for (int i = 1; i < primes.size(); i++) {
if (primes[i] - primes[i-1] == 2) {
count++;
}
}
cout << count << endl;
return 0;
}
```
这段代码可以实现洛谷P4718的功能,即输入一个整数n,然后计算并输出小于等于n的素数对中,差为2的素数对的个数。
相关推荐
![png](https://img-home.csdnimg.cn/images/20210720083516.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)