【硬核】如何使用oi解物理(电学)题

· · 科技·工程

我宣布!!!物理是全宇宙最【数据删除】的学科!!!

一、制作起源

当你有一本物理讲义时,当你做一道物理大题时 (电学) 时,最头疼的往往不是想到思路,而是算出数值来。通常你如果做错了那肯定是算错了。这是的你肯定想要一个东西,能记住你的电路,把答案算的一点不差。诶,巧了,电脑就是这么一个好东西,他绝不会骗你,只要你告诉他怎么算。

于是。。。。。。

:::info[《小作品》]

#include <bits/stdc++.h>
#define int long long
using namespace std;

struct ww{
    int type = 3;
    double R_resistor , I_current , V_voltage;
    //0 -> 电源 1 -> 串联组 2 -> 并联组 3 -> 单个用电器
    vector<int> v;
}v[1000005];

int n,root;
double Power_supply;

int cant[1000005];

void dfs_A(int x){
    if(v[x].type == 3)return;
    // cout<<x<<' '<<v[x].v.size()<<endl;
    v[x].R_resistor = 0;
    for(int i = 0 ;i < v[x].v.size(); i++){
        int xx = v[x].v[i];
        dfs_A(xx);
        if(v[x].type == 1) v[x].R_resistor += v[xx].R_resistor ;
        else if(v[x].type == 2) 
            if(v[xx].R_resistor == 0 ) {
                v[x].R_resistor = -1; break;
            } else v[x].R_resistor += 1.0 / v[xx].R_resistor ;
    }
    if(v[x].type == 2) {
        if(v[x].R_resistor == -1) v[x].R_resistor = 0;
        else v[x].R_resistor = 1.0 / v[x].R_resistor ;
    }
    return;
}
void dfs_B(int x){//串联分压

    if(v[x].R_resistor != 0 )v[x].I_current = v[x].V_voltage / v[x].R_resistor ;
    if(v[x].type == 3)return;

    for(int i = 0 ;i < v[x].v.size(); i++){
        int xx = v[x].v[i];

        if(v[x].type == 1) {
            v[xx].V_voltage = v[x].V_voltage * v[xx].R_resistor / v[x].R_resistor;
            v[xx].I_current = v[x].I_current;
        }else if(v[x].type == 2){
            v[xx].V_voltage = v[x].V_voltage;
            // v[xx].I_current
        } 

        dfs_B(xx);
    }
    return;
}

void solve(){
    dfs_A(root);//统计电阻

    dfs_B(root);//计算电流和电压
    // for(int i = 1;i <= n ;i ++) {
    //     printf("%lld | R = %.2lf , I = %.2lf , V = %.2lf \n",i,v[i].R_resistor , v[i].I_current , v[i].V_voltage);
    // }

    return;
}

double SSolve(double &question, double question_l,double question_r,double &Constant,double Constant_value) {
    //求一个在question_l至question_r之间的值,使它带入question处后使得Constant处值为Constant_value。
    question = 1;solve();double y1 = Constant;
    question = 2;solve();double y2 = Constant;

    double l = question_l,r = question_r,mid;
    while(l+0.0001<=r){
        mid = (l + r) / 2;
        question = mid;
        solve();
        cout<<mid << ' ' << Constant <<endl;
        if((!(y1 < y2)) ^ (Constant < Constant_value)){
            l = mid;
        }else{
            r = mid;
        }
    }


    return question;
}

char put[1005][1005];

int x_l[100005],y_l[100005];

void dfs_C(int x){//计算长度和宽度

    if(v[x].type == 3){
        x_l[x] = 11;
        y_l[x] = 2;
        return ;
    }

    for(int i = 0 ;i < v[x].v.size(); i++){
        int xx = v[x].v[i];

        dfs_C(xx);

        if(v[x].type == 1){
            x_l[x] += x_l[xx] ;
            y_l[x] = max(y_l[x],y_l[xx]);
        }else{
            x_l[x] = max(x_l[x],x_l[xx]);
            y_l[x] += y_l[xx];
        }
    }
    if(v[x].type == 2){
        x_l[x] += 4;
    }
    return;
}

string qiu(double xx){
    string ans = "R      ";
    int i = 3;
    // x *= 100;
    int x = xx * 100;
    ans[4] = '.';
    ans[5] = x % 10 + 48;
    ans[6] = x/10 % 10 + 48;
    x /= 100;
    while(x&&i>=0){
        ans[i] = x%10+48;
        x/=10;
        i--;
    }
    cout<<ans<<endl;
    return ans;
}

string print(){
    string ans = "";
    for(int i=y_l[root];i>=0;i--){
        for(int j = 0 ;j<=x_l[root];j++){
            ans += put[i][j];
        }
        ans += '\n';
    }
    return ans;
}

void dfs_D(int X,int Y,int x){
    // print();
    if(v[x].type == 1){
        int now_X = X;
        for(int i=0;i<v[x].v.size();i++) {
            int xx = v[x].v[i];
            dfs_D(now_X, Y, xx);
            now_X += x_l[xx];
        }
    }
    if(v[x].type == 2){
        put[Y][X] = put[Y][X+x_l[x]-1] = '-';
        for(int i=Y;i<=Y+y_l[x]-2;i++){
            put[i][X+1] = put[i][X+x_l[x]-2] = '|';
        }
        int now_Y = Y;
        for(int i=0;i<v[x].v.size();i++) {
            int xx = v[x].v[i];
            dfs_D(X + 2, now_Y, xx);
            now_Y += y_l[xx];
        }
    }
    if(v[x].type == 3){
        put[Y][X] = '-';
        put[Y][X+1] = '[';
        string can = qiu(v[x].R_resistor);
        for(int i=1;i<=7;i++){
            put[Y][X+i+1] = can[i-1];
        }
        put[Y][X+9] = ']';
        put[Y][X+10] = '-';
    }
    return;
}

string dayin(){
    dfs_C(root);
    dfs_D(0,0,root);
    return print();
}
/*
 |-[  200]-             |
 | |-[  200]-         | |
 | |                  | |
-|-|-[  200]--[  200]-|-|-

*/

void print_word(){

    for(int i = 1;i <= n ;i ++) {
        printf("%lld | R = %.2lf , I = %.2lf , V = %.2lf \n",i,v[i].R_resistor , v[i].I_current , v[i].V_voltage);
    }
    return;
}

signed main(){

    /*

    现已更新操作:

    - solve() 运行电路
    - print_word() 打印电路中数值
    - SSolve(int&,int,int,int&,int) 调整某个电阻的阻值使另一个数值(电压或电流)为某个值
    //示例: SSolve(v[1].R_resistor,0.05,200,v[2].V_voltage,1.5);
    - dayin() 打印电路图像


    如电路:
      ----[30 Ω]-----[20 Ω] --
    ---                      -----
    | ----[10 Ω]--------------   |
    |                            |
    ----|6 V|------------------------

    称为:

    5 6
    3 30
    3 20
    3 10
    1 2 1 2
    2 2 3 4

    */
    cout<<"The bug`s power way --- 1.0 \n------------------------------" << endl;

    cin >> n >> Power_supply;

    for(int i=1;i<=n;i++){
        cin>>v[i].type;
        if(v[i].type == 1) {
            int m;
            cin >> m;
            for(int j = 1;j <= m; j++) {
                int in;cin >> in ;
                cant[in] = 1;
                v[i].v.push_back(in);
            }
        } else if(v[i].type == 2) {
            int m;
            cin >> m;
            for(int j = 1;j <= m; j++) {
                int in;cin >> in ;
                cant[in] = 1;
                v[i].v.push_back(in);
                // cout<<i<<' '
            }
        } else if(v[i].type == 3) {
            cin >> v[i].R_resistor ;
        }
    }

    for(int i = 1;i <= n;i ++) {
        if(cant[i] == 0){
            root = i ;
            break;
        }
    }
    v[root].V_voltage = Power_supply;

    /*
        现已更新操作:

        - solve() 运行电路
        - print_word() 打印电路中数值
        - SSolve(int&,int,int,int&,int) 调整某个电阻的阻值使另一个数值(电压或电流)为某个值
        //示例: SSolve(v[1].R_resistor,0.05,200,v[2].V_voltage,1.5);
        - cout<<dayin(); 打印电路图像
    */

     /* 操作区 */

    solve();

    print_word();

    // SSolve(int&,int,int,int&,int)

    cout<<dayin();


    return 0;
}

:::

下面讲一下实现原理:

二、实现原理

对于一个电阻,ta 由很多的电阻丝串联而成。而他的电阻,就是这些电阻丝的电阻相加。类似的,我们可以把任何一个电路组当作一个电阻来看待。

那么我们怎么计算一个电路组的电阻呢?

根据初中物理知识,我们知道:

R_串 = R_1 + R_2 + ... + R_n

和:

R_并 = \frac{1}{\frac{1}{R_1}+\frac{1}{R_2}+...+\frac{1}{R_n}}

则我们可以得出电路每一个部分的电阻。然后我们根据电源电压,再根据串联分压和并联电路中电压的规律可以得到每一个部分的电压,每一个部分的电流也就得出来了。

三、使用方式

 5 6
 3 30    → 元件1:30Ω电阻
 3 20    → 元件2:20Ω电阻
 3 10    → 元件3:10Ω电阻
 1 2 1 2 → 元件4:串联组,包含元件1、2
 2 2 3 4 → 元件5:并联组,包含元件3、4(根元件)

如果要使用电流表和电压表,只需创建电阻为 0.005 和 100000 的电阻即可。

同时,可以在代码最尾部添加指令,如下:

SSolve(v[1].R_resistor,0.05,200,v[2].V_voltage,1.5);

代表求一个在 0.05 至 200 之间的值,使它带入元件一的电阻处后使得元件二的电压为 1.5V。

使用示例:

控制台输入:

5 6
3 30
3 20
3 10
1 2 1 2
2 2 3 4

代码最后添加指令:

    solve();

    print_word();

    // SSolve(int&,int,int,int&,int)

    cout<<dayin();

输出结果:

The bug`s power way --- 1.0 
------------------------------
1 | R = 30.00 , I = 0.12 , V = 3.60 
2 | R = 20.00 , I = 0.12 , V = 2.40 
3 | R = 10.00 , I = 0.60 , V = 6.00 
4 | R = 50.00 , I = 0.12 , V = 6.00 
5 | R = 8.33 , I = 0.72 , V = 6.00 
R 10.00
R 30.00
R 20.00


 |-[R 30.00]--[R 20.00]-|
 |                      |
-|-[R 10.00]-           |-

四、后话

求一些关注、收藏和点赞,其中: - 点赞过 $300$,下期添加求滑动变阻器可变化范围函数。 - 文章收藏过 $300$,下期添加手绘电路输入方式并完善指令,让所有操作在控制台中完成! - 个人关注过 $150$, 下期直接添加解答过程函数!!!(直接提供完整过程) 最后……**求过审**!!!