4.6 高精度

ZOJ 2001

Adding Reversed Numbers

The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.

Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.

ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).


Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.


Output

For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.


Sample Input

3
24 1
4358 754
305 794


Sample Output

34
1998

1

package data20180311;

import java.util.Scanner;

public class AddReversedNum {
     static int[] num1 = new int[100];
     static int[] num2 = new int[100];
    static String c1,c2;
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int num = cin.nextInt();
        while(num-->0){
             c1 = cin.next();
             c2 = cin.next();
             for(int i=0;i<100;i++){
                 num1[i]=0;
                 num2[i]=0;
             }
    
             c1=getReversedNum(c1);
             c2=getReversedNum(c2);
            
             String rs = Add(c1,c2);
            
             rs = getReversedNum(rs);
            
            
            System.out.println(rs);
            
            
            
        }
        
    }
    //24 1000            0001  24
    public static String getReversedNum(String s){//得到相反数
        int nZero=0;
        for(int i=s.length()-1;i>=0;i--){
            if(s.charAt(i)=='0'){
                nZero++;
            }else{
                break;
            }
        }
        StringBuffer sb = new StringBuffer();
        if(nZero==0){
            for(int i=0;i<s.length();i++){
                sb.append(s.charAt(s.length()-1-i));
            }
        }else{
            for(int i=0;i<s.length()-nZero;i++){
                sb.append(s.charAt(s.length()-1-nZero-i));
            }
        }
        return sb.toString();
        
        
        
    }
    //高精度加法
    public static String Add(String s1,String s2){
    
        
        for(int i=0;i<s1.length();i++){
            num1[s1.length()-i-1]=s1.charAt(i)-'0';//必须减‘0’ 否则为ASSII码值
        }
        for(int i=0;i<s2.length();i++){
            num2[s2.length()-i-1]=s2.charAt(i)-'0';
        }
        int max = s1.length()>s2.length()?s1.length():s2.length();
        for(int i=0;i<max;i++){
            num1[i]=num1[i]+num2[i];
            if(num1[i]>9){
                num1[i]-=10;
                num1[i+1]++;
            }
        }
        int nZero=0;
        for(int i=num1.length-1;i>=0;i--){
            if(num1[i]==0){
                nZero++;
            }else{
                break;
            }
        }
    
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<num1.length-nZero;i++){
            sb.append(num1[num1.length-nZero-1-i]);
        }
        return sb.toString();
    }
}


昨天在先用c写的时候一直出错,原因就是c中字符串指针使用的不好。。

char * c1,*c2;

此时并不能直接进行输入操作 scanf(“%s %s”,c1,c2);

因为c1 c2并指向对应的内存,并不知道存储的位置。

可以使用:char str1[100],str2[100];

c1 =str1 ,c2=str2;

这样可以进行输入操作 scanf(“%s %s”,c1,c2);

然后输出的时候也直接printf(“%s %s”,c1,c2)即可。而不是printf("%s %s",*c1,*c2);


<think>好的,我现在需要帮用户介绍STM32的HRTIM(高分辨率定器)实现高精度PWM输出的相关内容。首先,用户提到的“stimerpwm”可能是指STM32的定器PWM功能,但结合“高精度输出”,更可能是指HRTIM。我需要确认这一点,然后整理相关知识。 首先,我应该回顾一下STM32中的HRTIM的基本信息。HRTIM是STM32系列中某些高端型号(如STM32F334、G4系列等)特有的外设,主要用于高精度需求,比如数字电源、照明控制等。它的分辨率比普通定器高很多,通常可以达到ps级别,而普通定器可能在ns级别。 接下来,用户可能关心如何配置HRTIM来实现高精度PWM输出。我需要分步骤说明配置流程,包括钟设置、定器模式选择、比较寄存器配置、死区间设置以及输出极性控制等。同,需要注意HRTIM的各个主定器(如Master Timer和多个从定器Timer A到Timer E)的不同用途,以及如何协同工作。 然后,需要提到HRTIM的高精度特性,比如其钟源可能来自高频率的内部或外部钟,例如STM32F334的HRTIM钟可达4.6GHz,通过分频后得到高分辨率。此外,HRTIM的同步机制和Burst模式也是关键点,这些功能可以实现复杂的PWM波形生成和精确的事件触发。 用户可能还会关心应用场景,比如在电机控制、LLC谐振转换器、数字电源中的应用,这些都需要高精度的PWM控制。同,需要注意在使用HRTIM可能遇到的挑战,比如钟噪声、PCB布局对信号完整性的影响,以及中断和DMA配置的优化。 最后,需要总结一下HRTIM的优势,并提醒用户查阅相关参考手册和官方例程,以确保正确配置。同,确认提到的型号是否支持HRTIM,因为不是所有STM32都有这个功能。 现在需要将这些点组织成结构清晰的回答,分步骤说明,并确保使用正确的中文术语,避免技术错误。同,检查是否有遗漏的关键点,比如钟配置的具体数值示例,或者如何计算PWM频率和占空比,是否需要给出公式。例如,PWM频率的计算公式可能涉及定钟频率、预分频器和自动重载值,这些可以使用数学公式以$$...$$格式呈现,如频率公式为$$f_{PWM} = \frac{f_{clk}}{(ARR + 1) \times (Prescaler + 1)}$$,占空比为$$DutyCycle = \frac{CCR}{ARR + 1} \times 100\%$$。 需要确保所有数学表达式符合用户给定的格式要求,行内公式用$...$,独立公式用$$...$$。同,注意LaTeX语法的正确性,比如积分符号、上下标等是否正确转义。 最后,检查语言是否通顺,结构是否符合用户要求,分点说明,逐步引导用户理解HRTIM的高精度PWM输出实现方法,并确保信息的准确性和实用性。</think>以下是对STM32 HRTIM(高分辨率定器)实现高精度PWM输出的分步说明: --- ### 一、HRTIM简介 HRTIM(High-Resolution Timer)是STM32系列中**高端型号(如F3/G4系列)**特有的外设,专为高精度控制设计,特点包括: - **分辨率高达~217ps**(例如STM32F3344.6GHz钟下) - **多路独立PWM通道**(主定器+从定器Timer A~E) - **支持Burst模式、事件联动、死区间插入**等复杂功能 --- ### 二、实现高精度PWM的步骤 #### 1. 钟配置 - **选择高频钟源** 使用内部RC(HSI)或外部晶振(HSE),例如: $$f_{clk} = 4.6\text{GHz} \ (\text{STM32F334})$$ 通过分频系数调节实际工作频率: $$f_{HRTIM} = \frac{f_{clk}}{Prescaler}$$ #### 2.器模式设置 - **选择PWM模式** 配置为**中心对齐模式**(适合电机控制)或**边沿对齐模式**(通用场景) - **配置自动重载寄存器(ARR)** 决定PWM周期,公式: $$T_{PWM} = \frac{(ARR + 1)}{f_{HRTIM}}$$ #### 3. 比较寄存器(CCR)配置 - **设置占空比** 通过比较值CCR确定高电平间: $$DutyCycle = \frac{CCR}{ARR + 1} \times 100\%$$ 例如:ARR=999且CCR=300,占空比为30% #### 4. 死区间插入(关键!) - 防止上下管直通,需配置**Dead Time Register (DTR)**: $$DeadTime = DTR \times \frac{1}{f_{HRTIM}}$$ 通常设置数百ps到几ns #### 5. 输出极性控制 - 通过`TIMx_CCER`寄存器设置**OCx极性**(高电平有效/低电平有效) --- ### 三、关键代码示例(基于STM32CubeMX) ```c // 启动HRTIM钟 __HAL_RCC_HRTIM1_CLK_ENABLE(); // 配置主定器 hrtim1.Instance->sMasterRegs.MPER = 999; // ARR=999 hrtim1.Instance->sTimerxRegs[0].CMP1xR = 300; // CCR=300(占空比30%) // 使能输出并启动定器 HAL_HRTIM_WaveformOutputStart(&hrtim1, HRTIM_OUTPUT_TA1); HAL_HRTIM_WaveformCounterStart(&hrtim1, HRTIM_TIMERID_MASTER); ``` --- ### 四、提升精度的技巧 1. **钟优化** - 使用**锁相环(PLL)**倍频提高钟质量 - 避免高频噪声(如远离开关电源走线) 2. **PCB设计** - PWM输出线尽量短,阻抗匹配 - 使用差分信号(如互补PWM对) 3. **同步机制** - 通过`HRTIM_SYNC`事件同步多路PWM - 结合ADC触发实现闭环控制 --- ### 五、典型应用场景 - **数字电源**(LLC谐振转换器、移相全桥) - **电机控制**(无刷电机FOC算法) - **精密仪器**(超声换能器驱动) --- ### 六、注意事项 - **型号兼容性**:确认MCU支持HRTIM(如STM32F334/G474) - **热管理**:高频钟可能增加功耗 - **中断优化**:使用DMA传输减少CPU干预 建议参考**《STM32 HRTIM应用笔记》AN4539**和官方示例代码进一步学习。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值