洛谷T417452 快乐水怎么写用c++
时间: 2024-02-15 11:00:22 浏览: 41
根据提供的引用内容,以下是使用C++编写洛谷T417452快乐水的示例代码:
```cpp
#include <iostream>
using namespace std;
int main() {
float s;
cin >> s;
int x = 0;
float i = 2.0, sum = 0;
while (s > sum) {
sum = sum + i;
i = i * 0.98;
x++;
}
cout << x;
return 0;
}
```
这段代码通过循环计算快乐水的总量,直到总量超过给定的s。每次循环,快乐水的总量会乘以0.98,表示每次喝掉的快乐水量会逐渐减少。最后输出循环的次数x,即为喝完快乐水所需的天数。
相关问题
洛谷P1681用C++怎么写
洛谷 P1681 是一个字符串匹配问题,要求给一个字符串,判断是否能够通过删除的某些字符,使得剩下的与目标字符串匹配。
以下是一个 C++ 编写的解答示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
bool matchStrings(string target, string pattern) {
int tLen = target.length();
int pLen = pattern.length();
int tIndex = 0, pIndex = 0;
while (tIndex < tLen && pIndex < pLen) {
if (target[tIndex] == pattern[pIndex]) {
tIndex++;
pIndex++;
} else {
tIndex++;
}
}
return (pIndex == pLen);
}
int main() {
string target, pattern;
cin >> target >> pattern;
if (matchStrings(target, pattern)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
```
在上述代码中,我们首先定义了一个名为 `matchStrings` 的函数,该函数接受两个参数:目标字符串 `target` 和匹配字符串 `pattern`。函数使用双指针的方法进行匹配,通过遍历目标字符串和匹配字符串,判断是否能够将目标字符串中的一部分字符删除后得到匹配字符串。
在 `main` 函数中,我们从标准输入中读取目标字符串和匹配字符串,然后调用 `matchStrings` 函数进行匹配判断。最后根据匹配结果输出相应的结果。
请注意,以上代码仅为示例,可能需要根据具体题目要求进行适当修改。同时,为了简化示例,未进行输入验证,请确保输入的字符串符合题目要求。
写一下洛谷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的素数对的个数。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.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)