20241009日常训练

简单四则运算

#include<bits/stdc++.h>
using namespace std;
stack<int> num;
stack<char> op;
void eval()
{
    auto b = num.top(); num.pop();
    auto a = num.top(); num.pop();
    auto c = op.top(); op.pop();
    int x;
    if (c == '+') x = a + b;
    else if (c == '-') x = a - b;
    else if (c == '*') x = a * b;
    else x = a / b;
    num.push(x);
}

int solution(string str)
{
    unordered_map<char, int> pr{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};
    
    for (int i = 0; i < str.size(); i++)
    {
        auto c = str[i];
        if (isdigit(c))
        {
            int x = 0, j = i;
            while (j < str.size() && isdigit(str[j])) x = x * 10 + (str[j++] - '0');
            i = j - 1;
            num.push(x);
        }
        else if (c == '(') op.push(c);
        else if (c == ')')
        {
            while (op.top() != '(') eval();
            op.pop();
        }
        else
        {
            while (op.size() && pr[c] <= pr[op.top()]) eval();
            op.push(c);
        }
    }
    while (op.size()) eval();
    int ans = num.top();
    num.pop();
    return ans;
}
int main(){
    cout << solution("1+1") << endl;
}

计算x到y最短距离

代码:

#include<bits/stdc++.h>
using namespace std;
#define int  long long
int solution(int xx,int yy) {
	int d = yy - xx;
	int x = 1;
	while(x * (x + 1) < d)x ++;
	if(x * x >= d)x = 2 * x - 1;
	else x *= 2;
	return x;
}
signed main(){
	cout << solution() << endl;
}

在单片机上实现数字显示通常需要配合数码管或者LED阵列,这里假设我们使用的是8位LED数码管,例如常见的共阴极数码管。以下是使用C语言编写的一个基本示例,用于控制LED数码管显示数字"20241009": ```c #include <reg52.h> //假设这是单片机的头文件 // LED数码管的数据线配置 #define LCD_D4 P2^0 #define LCD_D5 P2^1 #define LCD_D6 P2^2 #define LCD_D7 P2^3 // 共阴数码管段选线 #define LCD_RS P2^4 #define LCD_EN P2^5 void init_lcd() { LCD_EN = 0; // 初始化时清空数码管 LCD_RS = 0; LCD_D4 = LCD_D5 = LCD_D6 = LCD_D7 = 0; } void display_digit(int digit) { switch (digit) { case '0': LCD_write(0x3F); break; // 数字0的码表 case '1': LCD_write(0x06); break; // 数字1 case '2': LCD_write(0x5B); break; // 数字2 case '3': LCD_write(0x4F); break; // 数字3 case '4': LCD_write(0x66); break; // 数字4 case '5': LCD_write(0x6D); break; // 数字5 case '6': LCD_write(0x7D); break; // 数字6 case '7': LCD_write(0x07); break; // 数字7 case '8': LCD_write(0x7F); break; // 数字8 case '9': LCD_write(0x6F); break; // 数字9 default: LCD_write(0x00); // 非法字符,清空数码管 } } void display_number(char* num_str) { for (int i = 0; num_str[i] != '\0'; ++i) { if (num_str[i] >= '0' && num_str[i] <= '9') { display_digit(num_str[i]); LCD_RS = 1; // 切换到下一个数字 delay_ms(100); LCD_RS = 0; // 返回正常模式 LCD_EN = 1; // 显示当前字符 delay_ms(100); LCD_EN = 0; // 清除显示 } } } void main(void) { init_lcd(); char num[] = "20241009"; display_number(num); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值