A. Squats

本文介绍了一个有趣的编程问题:如何在最少的时间内调整站立和蹲下仓鼠的数量,使得两者数量相等。通过输入仓鼠初始状态,算法计算并输出所需的最小操作次数及调整后的状态。

Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.

For another exercise, Pasha needs exactly  hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?

Input

The first line contains integer n (2 ≤ n ≤ 200n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.

Output

In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.

Sample test(s)
input
4
xxXx
output
1
XxXx
input
2
XX
output
1
xX
input
6
xXXxXx
output
0
xXXxXx
题意:X,x分别表示站着的和蹲着的仓鼠,想让站的的和蹲着的数量一样多,每操作一次需要1分钟,求最小操作数
#include<iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        string s;
        cin>>s;
        int num1=0;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='X')
            num1++;
        }
        if(num1==n/2)
        {
            cout<<"0\n"<<s<<endl;
        }
        else if(num1>n/2)
        {
            cout<<num1-n/2<<endl;
            int i,k;
            for(i=0,k=0;i<n&&k<num1-n/2;i++)
            {
                if(s[i]=='X')
                {
                    s[i]='x';
                    k++;
                }
            }
            cout<<s<<endl;
        }
        else if(num1<n/2)
        {
            cout<<n/2-num1<<endl;
            int i,k;
            for(i=0,k=0;i<n&&k<n/2-num1;i++)
            {
                if(s[i]=='x')
                {
                    s[i]='X';
                    k++;
                }
            }
            cout<<s<<endl;
        }
    }
    return 0;
}


#include "alarm.h" #include "oled.h" #include "motion.h" #include "audio.h" // 闹钟全局变量 static ALARM_TimeTypeDef alarm_time = {7, 0}; // 默认闹钟时间:7:00 static ALARM_WakeModeTypeDef wake_mode = {1, 10, 1, 20, 1, 5}; // 默认唤醒模式:全部开启 static uint8_t alarm_enabled = 0; // 闹钟是否启用 static uint8_t alarm_triggered = 0; // 闹钟是否触发 // 当前时间 static TIME_TimeTypeDef current_time = {12, 0, 0}; // 默认当前时间:12:00:00 // 闹钟初始化 void ALARM_Init(void) { // 初始化RTC(实时时钟) RTC_Init(); } // 检查闹钟是否应该触发 uint8_t ALARM_Check(void) { if(!alarm_enabled || alarm_triggered) return 0; // 更新当前时间 TIME_Update(); // 检查是否到达闹钟时间 if(current_time.hour == alarm_time.hour && current_time.minute == alarm_time.minute && current_time.second == 0) { alarm_triggered = 1; return 1; } return 0; } // 切换闹钟启用/禁用状态 void ALARM_Toggle(void) { alarm_enabled = !alarm_enabled; } // 设置闹钟时间 void ALARM_Set(u8 key) { static u8 set_index = 0; // 0-小时,1-分钟 if(key == KEY1_PRES) { // 切换设置项 set_index = (set_index + 1) % 2; } else if(key == KEY2_PRES) { // 增加数值 if(set_index == 0) // 小时 { alarm_time.hour = (alarm_time.hour + 1) % 24; } else // 分钟 { alarm_time.minute = (alarm_time.minute + 1) % 60; } } else if(key == KEY3_PRES) { // 减少数值 if(set_index == 0) // 小时 { alarm_time.hour = (alarm_time.hour - 1 + 24) % 24; } else // 分钟 { alarm_time.minute = (alarm_time.minute - 1 + 60) % 60; } } // 显示设置界面 OLED_Clear(); OLED_ShowString(0, 0, "Set Alarm Time", 16); OLED_ShowString(40, 2, "Hour", 12); OLED_ShowString(90, 2, "Minute", 12); OLED_ShowNum(45, 4, alarm_time.hour, 2, 16); OLED_ShowNum(95, 4, alarm_time.minute, 2, 16); // 显示光标 if(set_index == 0) OLED_ShowString(40, 5, "__", 12); else OLED_ShowString(90, 5, "__", 12); OLED_Refresh_Gram(); } // 设置唤醒模式 void ALARM_SetWakeMode(u8 key) { static u8 set_index = 0; // 0-摇晃模式,1-步数模式,2-深蹲模式 if(key == KEY1_PRES) { // 切换设置项 set_index = (set_index + 1) % 3; } else if(key == KEY2_PRES) { // 切换启用/禁用 if(set_index == 0) wake_mode.shake_enabled = !wake_mode.shake_enabled; else if(set_index == 1) wake_mode.step_enabled = !wake_mode.step_enabled; else wake_mode.squat_enabled = !wake_mode.squat_enabled; } else if(key == KEY3_PRES) { // 增加目标数量 if(set_index == 0 && wake_mode.shake_enabled) wake_mode.shake_count = (wake_mode.shake_count + 1) % 50; else if(set_index == 1 && wake_mode.step_enabled) wake_mode.step_count = (wake_mode.step_count + 1) % 50; else if(set_index == 2 && wake_mode.squat_enabled) wake_mode.squat_count = (wake_mode.squat_count + 1) % 30; } // 显示设置界面 OLED_Clear(); OLED_ShowString(0, 0, "Set Wake Mode", 16); // 摇晃模式 OLED_ShowString(10, 2, "Shake:", 12); if(wake_mode.shake_enabled) OLED_ShowString(50, 2, "ON", 12); else OLED_ShowString(50, 2, "OFF", 12); OLED_ShowString(70, 2, "Count:", 12); OLED_ShowNum(110, 2, wake_mode.shake_count, 2, 12); // 步数模式 OLED_ShowString(10, 4, "Steps:", 12); if(wake_mode.step_enabled) OLED_ShowString(50, 4, "ON", 12); else OLED_ShowString(50, 4, "OFF", 12); OLED_ShowString(70, 4, "Count:", 12); OLED_ShowNum(110, 4, wake_mode.step_count, 2, 12); // 深蹲模式 OLED_ShowString(10, 6, "Squats:", 12); if(wake_mode.squat_enabled) OLED_ShowString(50, 6, "ON", 12); else OLED_ShowString(50, 6, "OFF", 12); OLED_ShowString(70, 6, "Count:", 12); OLED_ShowNum(110, 6, wake_mode.squat_count, 2, 12); // 显示光标 switch(set_index) { case 0: OLED_ShowString(5, 2, ">", 12); break; case 1: OLED_ShowString(5, 4, ">", 12); break; case 2: OLED_ShowString(5, 6, ">", 12); break; } OLED_Refresh_Gram(); } // 检查是否满足唤醒条件 uint8_t ALARM_CheckWakeCondition(void) { uint8_t result = 1; // 检查摇晃条件 if(wake_mode.shake_enabled && MOTION_GetShakeCount() < wake_mode.shake_count) result = 0; // 检查步数条件 if(wake_mode.step_enabled && MOTION_GetStepCount() < wake_mode.step_count) result = 0; // 检查深蹲条件 if(wake_mode.squat_enabled && MOTION_GetSquatCount() < wake_mode.squat_count) result = 0; // 如果满足所有条件,重置闹钟触发状态 if(result) { alarm_triggered = 0; } return result; } // 显示闹钟状态 void ALARM_DisplayStatus(void) { // 显示当前时间 OLED_ShowString(40, 0, "Time:", 12); OLED_ShowNum(80, 0, current_time.hour, 2, 12); OLED_ShowString(96, 0, ":", 12); OLED_ShowNum(104, 0, current_time.minute, 2, 12); OLED_ShowString(120, 0, ":", 12); OLED_ShowNum(128, 0, current_time.second, 2, 12); // 显示闹钟状态 OLED_ShowString(10, 2, "Alarm:", 12); if(alarm_enabled) { OLED_ShowString(50, 2, "ON", 12); OLED_ShowNum(70, 2, alarm_time.hour, 2, 12); OLED_ShowString(86, 2, ":", 12); OLED_ShowNum(94, 2, alarm_time.minute, 2, 12); } else { OLED_ShowString(50, 2, "OFF", 12); } // 显示唤醒模式 OLED_ShowString(10, 4, "Wake Mode:", 12); uint8_t mode_count = 0; if(wake_mode.shake_enabled) mode_count++; if(wake_mode.step_enabled) mode_count++; if(wake_mode.squat_enabled) mode_count++; OLED_ShowNum(80, 4, mode_count, 1, 12); OLED_ShowString(90, 4, " modes", 12); } // 显示唤醒进度 void ALARM_ShowWakeProgress(void) { // 显示各唤醒模式的进度 uint8_t y_pos = 4; if(wake_mode.shake_enabled) { OLED_ShowString(10, y_pos, "Shake:", 12); OLED_ShowNum(60, y_pos, MOTION_GetShakeCount(), 2, 12); OLED_ShowString(80, y_pos, "/", 12); OLED_ShowNum(90, y_pos, wake_mode.shake_count, 2, 12); y_pos += 1; } if(wake_mode.step_enabled) { OLED_ShowString(10, y_pos, "Steps:", 12); OLED_ShowNum(60, y_pos, MOTION_GetStepCount(), 2, 12); OLED_ShowString(80, y_pos, "/", 12); OLED_ShowNum(90, y_pos, wake_mode.step_count, 2, 12); y_pos += 1; } if(wake_mode.squat_enabled) { OLED_ShowString(10, y_pos, "Squats:", 12); OLED_ShowNum(60, y_pos, MOTION_GetSquatCount(), 2, 12); OLED_ShowString(80, y_pos, "/", 12); OLED_ShowNum(90, y_pos, wake_mode.squat_count, 2, 12); } } // 时间显示 void TIME_Display(void) { // 更新时间 TIME_Update(); // 显示时间 OLED_ShowString(40, 2, "Current Time", 16); OLED_ShowNum(60, 4, current_time.hour, 2, 24); OLED_ShowString(100, 4, ":", 24); OLED_ShowNum(110, 4, current_time.minute, 2, 24); } // 设置时间 void TIME_Set(u8 key) { static u8 set_index = 0; // 0-小时,1-分钟,2-秒 if(key == KEY1_PRES) { // 切换设置项 set_index = (set_index + 1) % 3; } else if(key == KEY2_PRES) { // 增加数值 if(set_index == 0) // 小时 { current_time.hour = (current_time.hour + 1) % 24; } else if(set_index == 1) // 分钟 { current_time.minute = (current_time.minute + 1) % 60; } else // 秒 { current_time.second = (current_time.second + 1) % 60; } } else if(key == KEY3_PRES) { // 减少数值 if(set_index == 0) // 小时 { current_time.hour = (current_time.hour - 1 + 24) % 24; } else if(set_index == 1) // 分钟 { current_time.minute = (current_time.minute - 1 + 60) % 60; } else // 秒 { current_time.second = (current_time.second - 1 + 60) % 60; } } // 更新RTC时间 if(key == KEY2_PRES || key == KEY3_PRES) { RTC_SetTime(current_time.hour, current_time.minute, current_time.second); } // 显示设置界面 OLED_Clear(); OLED_ShowString(0, 0, "Set Time", 16); OLED_ShowString(40, 2, "Hour", 12); OLED_ShowString(90, 2, "Minute", 12); OLED_ShowString(40, 4, "Second", 12); OLED_ShowNum(45, 3, current_time.hour, 2, 16); OLED_ShowNum(95, 3, current_time.minute, 2, 16); OLED_ShowNum(45, 5, current_time.second, 2, 16); // 显示光标 if(set_index == 0) OLED_ShowString(40, 6, "__", 12); else if(set_index == 1) OLED_ShowString(90, 6, "__", 12); else OLED_ShowString(40, 7, "__", 12); OLED_Refresh_Gram(); } // 更新时间 void TIME_Update(void) { // 从RTC获取时间 RTC_GetTime(&current_time.hour, &current_time.minute, &current_time.second); } // RTC初始化(简化版) void RTC_Init(void) { // 实际项目中应包含完整的RTC初始化代码 // 这里仅做简化处理 } // 设置RTC时间(简化版) void RTC_SetTime(u8 hour, u8 minute, u8 second) { // 实际项目中应包含完整的RTC时间设置代码 // 这里仅做简化处理 } // 获取RTC时间(简化版) void RTC_GetTime(u8 *hour, u8 *minute, u8 *second) { // 实际项目中应包含完整的RTC时间获取代码 // 这里仅做简化处理 static uint32_t counter = 0; counter++; if(counter >= 1000) // 模拟1秒 { counter = 0; current_time.second = (current_time.second + 1) % 60; if(current_time.second == 0) { current_time.minute = (current_time.minute + 1) % 60; if(current_time.minute == 0) { current_time.hour = (current_time.hour + 1) % 24; } } } *hour = current_time.hour; *minute = current_time.minute; *second = current_time.second; }这是我的alarm.c代码出现了以下报错Hardwore\alarm.c(70): error: #20: identifier "KEY3_PRES" is undefined else if(key == KEY3_PRES) Hardwore\alarm.c(120): error: #20: identifier "KEY3_PRES" is undefined else if(key == KEY3_PRES) Hardwore\alarm.c(308): error: #20: identifier "KEY3_PRES" is undefined else if(key == KEY3_PRES) Hardwore\alarm.c(326): error: #20: identifier "KEY3_PRES" is undefined if(key == EY2_PRES || key == KEY3_PRES) Hardwore\alarm.c: 0 warnings, 4 errors
最新发布
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值