BlockStructure

/*Problem Statement

A group of vertical blocks are placed densely one after another on the ground. The blocks each have a width of 1, but their heights may vary.

For example, if the heights of the vertical blocks are given as {1,5,5,1,6,1}, the configuration would look like the following picture:


    ×
 ×× ×
 ×× ×
 ×× ×
 ×× ×
××××××
 
Your task is to reproduce the exact shape of this structure using some number of non-intersecting rectangles.

You will be given a int[] heights representing the heights of the vertical blocks from left to right.

Return the minimal number of rectangles necessary to perform this task with the given configuration of blocks.

Definition
Class:            BlockStructure
Method:            cover
Parameters:        int[]
Returns:        int
Method signature:    int cover(int[] heights)
(be sure your method is public)

Constraints
-    heights will have between 1 and 50 elements, inclusive.
-    Each element of heights will be between 1 and 1000, inclusive.
Examples
0)    {1,5,5,1,6,1}    Returns: 3
 We can use rectangles with sizes 6x1, 2x4 and 1x5.

    ×
 ×× ×
 ×× ×
 ×× ×
 ×× ×
××××××

1)    {2,2,2,4,4}    Returns: 2
 We can use a 3x2 rectangle and a 2x4 rectangle.

   ××
   ××
×××××
×××××

2)    {6,6,6,6,6,6}    Returns: 1
The structure is a rectangle.

××××××
××××××
××××××
××××××
××××××
××××××

3){71,44,95,16,10,80,12,17,98,61}    Returns: 10
It's impossible to use less than 10 rectangles.

4){100,100,97,100,100,100,97,98,99,99}
Returns: 5

This problem statement is the exclusive and proprietary property of TopCoder, Inc.
Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited.
(c)2003, TopCoder, Inc. All rights reserved.*/

package blockStructure;

import java.util.ArrayList;

public class BlockStructure {
   
    public static int cover(int[] heights) {
       
        int[][] records = markRecords(heights);
       
        int result = 0;
       
        int i = 0;
        int j = 0;
            while(i < records.length && j < records[i].length) {
                if(records[i][j] == 1) {
                    int start_i = i;
                    int start_j = j;
                    while(j < records[i].length && records[i][j] == 1) j++;       
                    j--;
                   
                    while(i < records.length && rangeExist(records, i, start_j, j)) i++;
                    i--;
                   
                    result++;
                    records = removeRecords(records, start_i, start_j, i, j);
                    i = j = 0;
                } else if(records[i][j] == 0) {
                    if(j < records[i].length - 1) j++;
                    else {
                        j = 0;
                        i++;
                    }
                }
        }
       
        return result;
    }
   
    //private static int result = 0;
   
    public static int cover_2(int[] heights) {
        int result = 0;
       
        if(heights.length == 1) return 1;
       
        deduct(heights);
       
        result++;
       
        if(isEmpty(heights)) return result;
       
        ArrayList zeroList = zeroPos(heights);
       
        int i = 0;
        int temp = -1;
        while(i < zeroList.size()) {
            int position = ((Integer)zeroList.get(i)).intValue();
            if(position == 0 || position - temp == 1) {
                temp = position;
            }
            else {
                int[] subtree = new int[position - temp - 1];
                for(int j = temp + 1; j < position; j++){
                    subtree[j - temp - 1] = heights[j];
                }
                result += cover_2(subtree);
               
                temp = position;
               
            }
            i++;
        }
       
        int position = ((Integer)zeroList.get(zeroList.size() - 1)).intValue();
        if(position != heights.length - 1) {
            int[] subtree = new int[heights.length - position - 1];
            for(int j = position + 1; j < heights.length; j++){
                subtree[j - position - 1] = heights[j];
            }
            result += cover_2(subtree);
        }
       
        return result;
    }
   
    private static boolean isEmpty(int[] array) {
        for(int k = 0; k < array.length; k++) {
            if(array[k] != 0) return false;
        }
       
        return true;
    }
   
    private static ArrayList zeroPos(int[] array) {
        ArrayList list = new ArrayList();
       
        for(int i = 0; i < array.length; i++) {
            if(array[i] == 0) list.add(i);
        }
       
        return list;
    }
   
    private static int maxium(int[] array) {
        int maxium = 0;
        for(int i : array) {
            if(i > maxium) maxium = i;
        }
       
        return maxium;
    }
   
    private static void deduct(int[] array) {
        int min = minum(array);
       
        for(int i = 0; i < array.length; i++) {
            array[i] = array[i] - min;
        }   
    }
   
    private static int minum(int[] array) {
        int min = Integer.MAX_VALUE;
        for(int i : array) {
            if(i < min) min = i;
        }
       
        return min;
    }
   
    private static int[][] markRecords(int[] array) {
        int largestNum = maxium(array);
       
        int[][] result = new int[largestNum][array.length];
       
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[i].length; j++) {
                try{
                if(array[j] > i ) result[i][j] = 1;
                else result[i][j] = 0;
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
       
        return result;
    }
   
    private static int[][] removeRecords(int[][] records, int i, int j, int x, int y) {
        for(int k = i; k <= x; k++) {
            for(int m = j; m <= y; m++) {
                records[k][m] = 0;
            }
        }
        return records;
    }
   
    private static boolean rangeExist(int[][] records, int i, int start_j, int j) {   
        for(int k = start_j; k <= j; k++) {
            if(records[i][k] == 0) return false;
        }
       
        return true;
    }

}
 
PRIVATE_FCT T_UWORD bbeep_IsRamImageValid( T_UBYTE *pb_RamStart , T_UBYTE eeprom_module_id) { /* ANALYSIS_REPORT_START_JUSTIFICATION (2017/07/26 : uid41645) !--> TOOL_NUMBER(PCLint:960) MISRA(M12.5) code optimisation : Validated for several years TOOL_NUMBER(PCLint:904) MISRA(M14.7) code optimisation : Validated for several years TOOL_NUMBER(PCLint:923) MISRA(11.1) cast is needed to obtain the correct address TOOL_NUMBER(PCLint:927) MISRA(11.4) cast is ok, variable to be accessed is of type T_ULONG TOOL_NUMBER(PCLint:826) MISRA(N/A) cast is ok, validated <--! */ const T_EEPROM_BLOCK *ps_RefEepBlock = NULL; //Get pointer to the corresponding EEPROM Block structure ps_RefEepBlock = bbeep_GetEepromModuleById(eeprom_module_id); #ifdef USE_FAFEE if(((*((T_UBYTE *)pb_RamStart)) && 0x01u) == FALSE) //t_EepromRamImage.HeaderData.Inconsistent { if(((*((T_UBYTE *)pb_RamStart)) && 0x03u) == FALSE) //t_EepromRamImage.HeaderData.Invalid { if((*((T_UBYTE *)pb_RamStart + 0x02u)) == Checksum_header((T_UBYTE *)(void*)pb_RamStart,FAFEE_HEADER_SIZE_WITHOUT_CHECKSUM )) //t_EepromRamImage.Checksum { if((*((T_CRC *)((T_ULONG)pb_RamStart + (ps_RefEepBlock->uw_EepAreaSize) - sizeof(T_CRC)))) == bbeep_crc16(eeprom_module_id)) //t_EepromRamImage.uw_DataCRC { return TRUE; } else { (*((T_UBYTE *)pb_RamStart)) = 0x01u; //t_EepromRamImage.HeaderData.Inconsistent return FALSE; } } else { return FALSE; } } else { (*((T_UBYTE *)pb_RamStart)) = 0x03u; //t_EepromRamImage.HeaderData.Invalid return FALSE; } } else { (*((T_UBYTE *)pb_RamStart)) = 0x01u; //t_EepromRamImage.HeaderData.Inconsistent return FALSE; } #elif defined USE_START_AND_END_MARKERS /* ANALYSIS_REPORT_START_JUSTIFICATION (2015/08/05 : uid25397) !--> TOOL_NUMBER(PCLint:960) MISRA(M12.5) code optimisation : Validated for several years TOOL_NUMBER(PCLint:904) MI
03-29
<think>好的,我需要帮助用户理解C语言中EEPROM校验逻辑的功能以及MISRA合规性注释的作用。首先,我应该先解释EEPROM校验逻辑的基本概念和常见实现方法。然后,再结合MISRA的规则和指令,分析合规性注解的作用,确保代码符合安全标准。 首先,关于EEPROM的校验逻辑,通常包括数据校验和、CRC校验或冗余存储等方法。这些方法用于确保存储在EEPROM中的数据在读写过程中没有发生错误。例如,校验和是将数据求和后存储,读取时再次计算校验和以验证数据完整性。我需要用例子来说明这些逻辑,比如代码中的校验和计算和验证过程。 接下来是MISRA合规性部分。用户提到MISRA C 2012中的规则和指令的区别。根据引用[1],规则是完整的要求,可以独立检查,而指令需要额外信息。我需要说明在代码中如何应用这些规则,比如避免使用未定义行为,确保类型转换安全,使用显式声明等。例如,使用显式类型转换而不是隐式的,或者避免使用动态内存分配,这些都属于MISRA的规则。 然后,合规性注释的作用是解释代码如何符合MISRA标准,特别是当某些规则需要例外处理时。例如,如果必须使用某个不符合规则的结构,开发者需要添加注释说明原因,并确认没有安全隐患。这有助于代码审查和维护。 还需要结合用户提供的引用,比如引用[2]中的宏定义是否符合常量表达式的要求,或者引用[3]中提到的编译器扩展问题。可能EEPROM代码中使用了某些编译器特定功能,这时需要注释说明是否符合MISRA,或者是否有变通方法。 最后,生成相关问题,帮助用户深入理解。例如,如何设计EEPROM校验逻辑,MISRA规则如何提高代码可靠性,合规性注释的具体应用场景等。 需要确保回答结构清晰,分点说明,使用正确的中文和LaTeX格式,同时引用用户提供的参考资料。注意不要遗漏任何关键点,并保持语言自然流畅。</think>### C语言 EEPROM校验逻辑与MISRA合规性分析 #### 1. EEPROM校验逻辑的功能 EEPROM(电可擦可编程只读存储器)常用于存储关键配置或状态数据,其校验逻辑旨在保障数据的完整性和可靠性。常见的校验方法包括: - **校验和(Checksum)**:通过计算数据块的累加和或异或值,并与存储的校验值对比,例如: ```c uint8_t calculate_checksum(const uint8_t *data, size_t len) { uint8_t sum = 0; for (size_t i = 0; i < len; i++) { sum ^= data[i]; // 异或校验 } return sum; } ``` - **CRC校验**:使用多项式算法(如CRC-16)生成更复杂的校验码,提高错误检测能力[^1]。 - **冗余存储**:将同一数据多次存储,通过多数表决机制验证一致性。 #### 2. MISRA合规性注释的作用 MISRA C规范通过**规则**和**指令**确保代码的安全性和可维护性: - **规则(Rules)**:可直接通过静态分析工具检查,例如: - 规则11.3:禁止从`void*`到其他类型的隐式转换,需显式类型声明。 - 规则15.5:避免使用`goto`语句以防止流程混乱。 - **指令(Directives)**:需要开发者结合设计文档解释合规性,例如: - 指令4.1:要求代码中避免使用动态内存分配,需通过注释说明静态分配策略。 **合规性注释示例**: ```c /* MISRA-C:2012 Rule 11.4 compliant - 显式类型转换确保安全性 */ uint16_t eeprom_read(uint8_t address) { return (uint16_t)(*(volatile uint8_t*)(EEPROM_BASE + address)); // [^2] } ``` #### 3. 校验逻辑的MISRA合规性关键点 - **避免未定义行为**:例如校验计算需确保无符号整数溢出合法(规则12.2)。 - **显式类型转换**:强制转换时需注释说明安全性(规则11.3)。 - **禁用危险函数**:如禁用非安全字符串函数(规则21.7)。 - **代码可读性**:通过注释解释复杂校验算法与MISRA规则的兼容性[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值