帮我写一个生消问题的代码
时间: 2023-05-21 09:01:27 浏览: 60
以下是一个简单的生消问题的代码示例:
```
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "请输入一个字符串:";
cin >> input;
int i = 0;
while (i < input.length() - 1) {
if (input[i] == input[i+1]) {
input.erase(i, 2);
i = 0;
} else {
i++;
}
}
cout << "生消后的字符串为:" << input << endl;
return 0;
}
```
这段代码可以将输入的字符串进行生消操作,即将相邻的相同字符消去,直到无法再进行生消为止。例如,输入字符串 "abbaca",经过生消操作后变为 "ca"。
相关问题
用Python写一个关于生肖的代码
好的,以下是一个简单的Python代码,可以根据出生年份判断属于哪个生肖:
```python
# 定义生肖列表
zodiacs = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪']
# 获取用户输入的出生年份
year = int(input('请输入出生年份:'))
# 计算生肖索引
index = (year - 1900) % 12
# 输出生肖
print('你的生肖是:', zodiacs[index])
```
使用方法:
1. 打开Python编辑器,将以上代码粘贴到编辑器中。
2. 运行代码。
3. 输入你的出生年份,按回车键。
4. 程序会自动计算你的生肖,并输出结果。
注意:以上代码只是一个简单的示例,可能存在一些漏洞和不足之处,仅供参考。
用安卓写一个生肖计算器
好的,下面是一个简单的安卓生肖计算器示例代码:
1. 创建一个新的Android Studio项目,并在activity_main.xml文件中添加以下代码:
```xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/year_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入出生年份"/>
<Button
android:id="@+id/calculate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算"/>
<TextView
android:id="@+id/result_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算结果"/>
</LinearLayout>
```
2. 在MainActivity.java文件中添加以下代码:
```java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText yearInput;
private Button calculateButton;
private TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yearInput = findViewById(R.id.year_input);
calculateButton = findViewById(R.id.calculate_button);
resultText = findViewById(R.id.result_text);
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int year = Integer.parseInt(yearInput.getText().toString());
int zodiacIndex = (year - 4) % 12;
String[] zodiacs = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
String result = "你的生肖是:" + zodiacs[zodiacIndex];
resultText.setText(result);
}
});
}
}
```
3. 运行应用程序,输入出生年份并单击“计算”按钮,即可显示计算结果。
这是一个简单的示例代码,你可以根据自己的需求进行修改和扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)