寻找最长合法括号序列九度0J

#include<stdlib.h>
#include<stdio.h>
#include<string.h> 


main()
{
char str[1024] = {0};
int  left = 0, right = 0, count = 0, maxn = 0, count1 = 0;
int  i = 0, j= 0;
printf("请输入括号字符串:");
    
scanf("%s", str);
    for(i = 0; i < strlen(str); i++)            //i < strlen(str) 
{
   
   if(str[0] == ')' && i == 0)             //此句可能多余 
{

continue;
}
        
if(str[i] == '(')                      
{
     
 left++;                  //出现左括号加1 
     }  
 
if(str[i] == ')')
{
right++;                   
if(left >= right)          //left计数做括号个数, right计数右括号数 
{

left--;                
right--;
count++;               //count计数成功配对的对数, 对数加1, left和right都减1 
    }
   else
   {
    left = 0;             // 如果右括号数大于左括号数, 全部置0, 
    right = 0;
    count = 0;                  //count置0, count为成功配对括号的对数 
}
 }
         
         if(count > maxn)                  //不断刷新最大成功配对括号的对数 
         {
          maxn = count;                  //只要刷新最大对数即赋值给maxn, count1为出现最大对数count的次数, count1重置1 
          count1 = 1;
}
else if(count == maxn && maxn != 0)     //maxn != 0 避免出现没有任何成功配对时, 计数count1自加 
{
maxn = count;                        //最大成功配对对数相同时, count1加1 
count1++;
 }
}

printf("%d , %d", maxn * 2, count1);         // maxn * 2计算最长成功配对的括号个数, count1最长配对括号个数出现的次数 
return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将代码修改为温度每三秒上升一度,持续9秒时进行温升告警,可以按照以下步骤进行修改: 1. 将 tick2 的时间判断条件改为温度上升一度的时间判断条件,即:bms_get_time_interval(tick2, OSTimeGet()) > 3000/1。 2. 将 old_temp 的初始值改为当前温度,即:old_temp = now_temp; 3. 将温度上升的判断条件改为温度上升了三度,即:now_temp - old_temp >= 3。 4. 将触发告警的判断条件改为温度上升了九度,即:now_temp - old_temp >= 9。 下面是修改后的代码: ``` void bms_temp_rise_diag(void) { static INT8U temp_flag = FALSE; static INT16U old_temp = 0; static INT32U tick = 0, tick2 = 0; INT8U flag = 0; INT16U now_temp = 0; if (temp_flag == FALSE) { tick = OSTimeGet(); tick2 = OSTimeGet(); old_temp = now_temp = bms_get_max_temp(); } if (bms_get_time_interval(tick2, OSTimeGet()) > 3000/1 || temp_flag == FALSE) { if (bms_get_time_interval(tick, OSTimeGet()) > 3000) { now_temp += 3; tick = OSTimeGet(); } tick2 = OSTimeGet(); if (now_temp - old_temp >= 3) { flag = TRUE; } old_temp = now_temp; temp_flag = TRUE; } if (flag == TRUE && now_temp - old_temp >= 9) { if (AlarmLevel2 != bms_get_tr()) { save_event_log(TR_ALARM_TYPE, 0xFF); } bms_set_tr(AlarmLevel2); } else { bms_set_tr(AlarmNone); } if (bms_get_rel_flag() == TRUE) { bms_set_tr(AlarmNone); temp_flag = FALSE; } } ``` 注意,这只是一种修改方法,具体的实现方式可能还需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值