22.01.09 总结

总的来说今天学到的东西不多

 

vector

又或者说动态数组更好?

头文件:#include<bits/stdc++.h>

头文件:#include<vector>

常用操作:

vector<int> num;//里面的int为容器数据类型
//感觉像是数组里的共用体
a.back();  
最后一个元素

a.front(); 
第一个(首个)元素

a.empty(); 
为空判断

a.pop_back();  
末尾出队

a.swap(b);   
交换a,b;

a.insert(a.begin()+1,5);  
在a[1]处插入一个5

a.size();
容器大小(数据数量),是一个数值

reverse(a.begin(),a.end());
将容器倒置

特性:

vector<int>num(100)分配100个int空间 像数组一样  也能通过下标访问

可以通过迭代器读取,不知道为什么要用迭代器,也可以通过下标访问

但是由于容器是动态的,不能这样写

vector <int>num;
num[0]=1;   (X)

因为此时num还没有空间  

如果这样就对了

vector <int> num(10);
num[0]=1;

迭代器输出

    vector <int> num;

    vector <int>::iterator pos;
    for(int i=0;i<999;i++)
    {
        num.push_back(i);
    }
    for(pos=num.begin();pos!=num.end();pos++)
    {
        cout<<*pos<<" ";
        //printf("%c ",*pos);
    }

下标遍历输出

    vector <int> num;
    for(int i=0;i<999;i++)
    {
        num.push_back(i);
    }
    
    for(int i=0;i<999;i++)
    {
        cout<<num[i]<<" ";
    }

截取a中指定区间元素入b(和sort一样左闭右开)

vector<int>b(num.begin(),num.begin()+3);

测试代码

可以通过格式控制符改变     输出值的类型  入printf %c

#include<iostream>
#include<string>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;
int main()
{
    vector <int> num;
    for(int i=0;i<999;i++)
    {
        num.push_back(i);
    }

    for(int i=0;i<999;i++)
    {
        cout<<num[i]<<" ";
    }
    vector <int>::iterator pos;
    for(pos=num.begin();pos!=num.end();pos++)
    {
        cout<<*pos<<" ";
        //printf("%c ",*pos);
    }
    vector<int>b(num.begin(),num.begin()+3);
    for(pos=b.begin();pos!=b.end();pos++)
    {
        cout<<*pos<<" ";
        //printf("%c ",*pos);
    }
   // cout<<num>=b;貌似不行

}

今天又去复盘了一下树的基本操作,感觉树的推理还不是很熟练,但是基本上都是一个模板

中序和其他任意一个遍历建树  另一种遍历输出

也看到了树的复制,和深度计算,这些之前都忽略掉了,明天再学习一下

蓝桥上做了些练习题

其中有两道比较有意思

贴一下,发一下思路

样例输入

10

样例输出

3628800

一开始我惯性思维准备直接大数相乘了;

但是发现那样非常麻烦,然后采取了模拟手算乘法的方法

加一个进位器就好了

查了一下1000!的位数,只有2568位

字符串完全够了

解释放代码里面

#include<iostream>
#include<string>
#include<algorithm>
#include<functional>

#define LEN 10000
//10000*1000貌似也不是很大,不会超时
using namespace std;

int a[10000];

int main()
{
    int i,j,k;
    int n;
    int jinwei;
    a[0]=1;
    cin>>n;
    for(i=2; i<=n; i++) //从2开始阶乘 ,每次循环计算i阶乘的结果
    {
        jinwei=0;
        for(j=0; j<LEN; j++)//与记录位数的另一种方法不同,这里不用管jinwei是否为0,因为LEN远大于上限2568位
        {
            a[j]=a[j]*i+jinwei;//相应阶乘中的一项与当前所得临时结果的某位相乘加上进位
            jinwei=a[j]/10;
            a[j]%=10;
        }
    }
    for(i=LEN-1; ; i--)//去掉末尾的0不输出
    {
        if(a[i]!=0)
        break;
    }
    for(j=i; j>=0; j--)//倒序输出
        printf("%d",a[j]);
    return  0;

}

还有这道题

中心有点离谱,在我的印象中中心应该是重心,而在这里却是外心 ??

这里补充一下概念就好了,没什么技术含量

重心:中线交点  (x1+x2+x3)/3 , (y1+y2+y3)/3

垂心:高线交点

内心:角平分线交点(内切圆)

外心:中垂线交点  (外切圆)

 一般来说这四心合一才是中心,即正三角形,挺奇怪的

代码

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

int main()
{
    double x1,x2,x3,y1,y2,y3;
    cin>>x1>>y1;
    cin>>x2>>y2;
    cin>>x3>>y3;

    double a,b,c;//三边
    a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
    c=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));

    double C;
    C=a+b+c;//周长

    double cosxita;//θ
    double sinxita;
    cosxita=(a*a+b*b-c*c)/(2*a*b);

    sinxita=sqrt(1-pow(cosxita,2));
    double s=a*b*sinxita/2;//面积

    double A1,A2,B1,B2,C1,C2;
    A1=2*(x2-x1);
    B1=2*(y2-y1);
    C1=x2*x2-x1*x1+y2*y2-y1*y1;

    A2=2*(x3-x2);
    B2=2*(y3-y2);
    C2=x3*x3-x2*x2+y3*y3-y2*y2;
    double xw,yw;//外心  中心
    xw=(C1*B2-C2*B1)/(A1*B2-A2*B1);
    yw=(A1*C2-A2*C1)/(A1*B2-A2*B1);

    double x,y;//重心
    x=(x1+x2+x3)/3;
    y=(y1+y2+y3)/3;
    printf("%.2lf\n%.2lf\n%.2lf %.2lf\n%.2lf %.2lf\n",C,s,xw,yw,x,y);

}

补充一下,上面用到了面积公式  s=1.0/2*AB*sinc; 

这里是公式出处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来为您解答这个问题。 首先,我们需要将数据整理成R可处理的格式。这里,我将数据手动输入到R中,并将其保存为data.frame对象: ```R transportation <- data.frame( year = 2002:2021, railway = c(366.34, 375.14, 401.82, 419.16, 436.38, 447.02, 471.85, 487.8, 503.12, 518.25, 516.16, 522.77, 516.81, 532.08, 531.9, 527, 524.47, 511.09, 492, 476), highway = c(71.31, 67.32, 68.07, 67.88, 67.03, 66.3, 66.35, 63.34, 60.7, 51.92, 51, 49.2, 48.62, 46.52, 56.11, 54.45, 54.74, 54, 53, 45), water = c(20.27, 22.01, 29.42, 28.44, 27.44, 26.56, 27, 28.27, 29.03, 30.09, 30.35, 32.27, 31.09, 29.1, 34.06, 33.38, 33.5, 34.8, 37, 39), air = c(1482.14, 1510.68, 1773.71, 1751.13, 1724.75, 1716.97, 1669.62, 1616.08, 1598.11, 1573.69, 1547.57, 1508.83, 1464.21, 1497.47, 1502.85, 1484.65, 1479, 1470, 1442, 1432) ) ``` 接下来,我们可以使用ggplot2库绘制各种图形来更好地理解数据。 首先,我们可以使用ggplot2库绘制各种运输方式的时间序列折线图,代码如下: ```R library(ggplot2) ggplot(melt(transportation, id.vars = "year"), aes(x = year, y = value, color = variable)) + geom_line() + xlab("Year") + ylab("Passenger Distance") + ggtitle("Passenger Distance by Transportation Type") + theme(legend.position = "bottom") ``` 运行上面的代码,可以得到一个绘制了四种运输方式的时间序列折线图。 接着,我们可以使用ggplot2库绘制各种运输方式的密度图,代码如下: ```R ggplot(melt(transportation, id.vars = "year"), aes(x = value, fill = variable)) + geom_density(alpha = 0.5) + xlab("Passenger Distance") + ylab("Density") + ggtitle("Passenger Distance Density by Transportation Type") ``` 运行上面的代码,可以得到一个绘制了四种运输方式的密度图。 最后,我们可以使用ggplot2库绘制各种运输方式的箱线图,代码如下: ```R ggplot(melt(transportation, id.vars = "year"), aes(x = variable, y = value, fill = variable)) + geom_boxplot() + xlab("Transportation Type") + ylab("Passenger Distance") + ggtitle("Passenger Distance by Transportation Type") ``` 运行上面的代码,可以得到一个绘制了四种运输方式的箱线图。 以上是本次数据分析的R代码和图形,希望能够帮到您。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值