【id:301】【10points】H. 字符串碎片 Description 一个由小写字母组成的字符串可以看成一些同一字母的最大碎片组成的。例如,"aaabbaaac"是由下面碎片组成的:"aaa","bb",‘c’。输入字符串,请计算字符串的所有碎片及所有碎片的平均长度。 所有碎片的平均长度 = 所有碎片长度之和/碎片个数 Input 测试次数t 每组测试数据为一行字符串 Output 对每组测试数据,输出字符串中按顺序出现的碎片和所有碎片的平均长度(相同碎片只输出一次,平均长度保留2位小数)。
时间: 2024-03-13 21:44:39 浏览: 19
这是一道字符串模拟题。我们可以用两个指针 `i` 和 `j` 来遍历整个字符串,表示当前碎片的起始位置和结束位置。每当碰到一个新的字符,就把当前的碎片结束,并把它的长度加入总长度中,然后输出这个碎片。
以下是一份可能的 Python 代码实现:
```python
t = int(input()) # 测试次数
for _ in range(t):
s = input() # 输入字符串
# 初始化碎片起始位置和总长度
i, n = 0, 0
# 遍历整个字符串
for j in range(len(s)):
if s[j] != s[i]:
# 当前碎片结束,输出它的长度
print(s[i:j], end=' ')
n += j - i
i = j
# 输出最后一个碎片和所有碎片的平均长度
print(s[i:], '{:.2f}'.format(n/(j-i+1)))
```
输出的结果就是按顺序出现的碎片和所有碎片的平均长度。注意在输出最后一个碎片和平均长度时,我们需要用格式化字符串来保留小数点后两位。
相关问题
请用C#语言写一个完整程序 动态显示从串口监测的数据,图形显示可以精美一些
以下是一个完整的C#程序,它可以从串口监测数据,并使用OxyPlot库动态展示数据的图形。这个程序使用了WPF框架,因此需要创建一个WPF应用程序,并在MainWindow.xaml中添加一个OxyPlot控件。在MainWindow.xaml.cs文件中添加以下代码:
```csharp
using System;
using System.IO.Ports;
using System.Windows;
using System.Windows.Threading;
using OxyPlot;
using OxyPlot.Series;
namespace SerialPlotter
{
public partial class MainWindow : Window
{
private SerialPort serialPort; // 串口对象
private DispatcherTimer timer; // 定时器
private LineSeries series; // OxyPlot线型图对象
public MainWindow()
{
InitializeComponent();
// 初始化串口对象
serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
serialPort.DataReceived += SerialPort_DataReceived;
serialPort.Open();
// 初始化OxyPlot图形
var plotModel = new PlotModel();
series = new LineSeries();
plotModel.Series.Add(series);
plot.Model = plotModel;
// 初始化定时器
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(50);
timer.Tick += Timer_Tick;
timer.Start();
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// 从串口读取数据
string data = serialPort.ReadLine();
// 解析数据
double value;
if (double.TryParse(data, out value))
{
// 将数据添加到OxyPlot图形中
Dispatcher.Invoke(() =>
{
series.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), value));
});
}
}
private void Timer_Tick(object sender, EventArgs e)
{
// 更新OxyPlot图形
Dispatcher.Invoke(() =>
{
// 只显示最近的100个数据点
if (series.Points.Count > 100)
{
series.Points.RemoveRange(0, series.Points.Count - 100);
}
// 自动调整X轴范围
if (series.Points.Count > 1)
{
var lastPoint = series.Points[series.Points.Count - 1];
var firstPoint = series.Points[0];
plot.Model.Axes[0].Minimum = DateTimeAxis.ToDouble(firstPoint.X);
plot.Model.Axes[0].Maximum = DateTimeAxis.ToDouble(lastPoint.X);
}
plot.InvalidatePlot(true);
});
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// 关闭串口连接
serialPort.Close();
}
}
}
```
在MainWindow.xaml中添加以下XAML代码,用于展示OxyPlot图形:
```xaml
<Window x:Class="SerialPlotter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Serial Plotter" Height="450" Width="800">
<Grid>
<oxy:Plot x:Name="plot" Margin="10" />
</Grid>
</Window>
```
最后,在App.xaml.cs文件中添加以下代码,以便在程序启动时设置OxyPlot主题:
```csharp
using System.Windows;
using OxyPlot.Wpf;
namespace SerialPlotter
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
// 设置OxyPlot主题
ThemeManager.SetTheme(Theme.Dark);
base.OnStartup(e);
}
}
}
```
这样就完成了一个动态展示从串口监测数据的图形程序。程序会在启动时打开串口连接,并使用定时器每50毫秒读取一次串口数据,并将数据添加到OxyPlot图形中。图形会自动调整X轴范围,最多显示100个数据点。当程序关闭时,会自动关闭串口连接。
没有指针的汇编程序测试一字符串是否为“回文串”。
下面是一段没有指针的x86汇编程序,用于测试一个字符串是否是回文串:
```
section .data
msg db 'Enter a string: ', 0
msg_rev db 'The string is a palindrome.', 0
msg_not_rev db 'The string is not a palindrome.', 0
input db 256, 0
len equ $-input
section .text
global _start
_start:
; print message
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 16
int 0x80
; read input string
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, len
int 0x80
; set up loop variables
mov esi, input ; esi points to start of string
mov edi, esi ; edi points to end of string
add edi, edx ; adjust end pointer to last byte
; loop through string and compare characters
loop:
cmp esi, edi ; compare start and end pointers
jae is_palindrome ; if they have met or crossed, it is a palindrome
; compare characters at start and end pointers
mov al, [esi]
mov bl, [edi-1]
cmp al, bl
jne not_palindrome
; adjust pointers and continue loop
inc esi
dec edi
jmp loop
; output result if string is a palindrome
is_palindrome:
mov eax, 4
mov ebx, 1
mov ecx, msg_rev
mov edx, 26
int 0x80
jmp exit
; output result if string is not a palindrome
not_palindrome:
mov eax, 4
mov ebx, 1
mov ecx, msg_not_rev
mov edx, 30
int 0x80
; exit program
exit:
mov eax, 1
xor ebx, ebx
int 0x80
```
该程序首先提示用户输入一个字符串,然后读取输入的字符串。接下来,程序使用两个指针(start和end)遍历字符串,比较每个字符是否相等。如果字符不相等,则字符串不是回文串。如果指针相遇或越过,字符串是回文串。
程序输出相应的消息来指示字符串是否是回文串。如果字符串是回文串,程序输出“该字符串是回文串”。如果字符串不是回文串,则程序输出“该字符串不是回文串”。
请注意,该程序假定输入字符串不包含空格或其他分隔符。如果要处理包含空格或其他分隔符的字符串,需要更复杂的代码来跳过这些字符。