30天编程练习(二)

1004 公交车路线

问题理解:本题写的是公交车路线问题,本质就是并查集问题,将各个线路合并,求各个构造树是否有相同的根节点

#include <iostream>
#include <cstdio>


using namespace std;
int a[100];


int find(int x){
    int n = x;
    while(a[x]!=x){
        x = a[x];
    }
    a[n] = x;             
    return x;
}


int Union(int x,int y){
    int m = find(x);
    int n = find(y);


    if(m!=n)
a[m] = n;
}


int main()
{
    int t,cars;
    scanf("%d",&t);
    while(t--){
    int start,end,m,tian,xia;
    scanf("%d%d",&start,&end);
    scanf("%d",&cars);


    for(int i = 1;i <=100;i++){
        a[i] = i;
    }
    for(int j = 0;j < cars;j++){
    cin>>m>>tian;
     for(int i=1;i<m;i++)
            {
                cin>>xia;
                Union(tian,xia);
            }
    }
        if(find(start)!=find(end))
            cout<<"No"<<endl;
        else
            cout<<"Yes"<<endl;


    }


    return 0;

}

1005 打印二叉树

思路:首先构造一颗二叉排序树,然后在对其进行前序中序后序遍历,依次输出。

#include <iostream>
using namespace std;



struct Node
{
    int value;
    struct Node *leftNode = NULL;
    struct Node *rightNode = NULL;
};


void insert(Node *node, Node *p)
{
    if (node == NULL)
        return;
    if (p->value < node->value)
    {
        if (node->leftNode == NULL)
            node->leftNode = p;
        else
            insert(node->leftNode, p);
    }
    else
    {
        if (node->rightNode == NULL)
            node->rightNode = p;
        else insert(node->rightNode, p);
    }
}
void preorder(Node *node)
{
    if (node == NULL)
        return;
    cout << node->value << " ";
    preorder(node->leftNode);
    preorder(node->rightNode);
}
void inorder(Node *node)
{
    if (node == NULL)
        return;
    inorder(node->leftNode);
    cout << node->value << " ";
    inorder(node->rightNode);
}
void postorder(Node *node)
{
    if (node == NULL)
        return;
    postorder(node->leftNode);
    postorder(node->rightNode);
    cout << node->value << " ";
}


int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int a;
        cin >> a;
        Node *root = new Node;
        Node *p;
        root->value = a;
        for (int i = 0;i < n - 1;i++)
        {
            cin >> a;
            p = new Node;
            p->value = a;
            insert(root, p);
        }
        preorder(root);
        cout << endl;
        inorder(root);
        cout << endl;
        postorder(root);
        cout << endl;
        cout << endl;
    }
    return 0;

}


1006 函数题

解析:写一个函数循环即可

#include<stdio.h>  
#include<string.h>  
int f[1000000];  
using namespace std;  
int main()  
{  
    int a,b,n;  
    while(~scanf("%d%d%d",&a,&b,&n))  
    {  
        if(a==0&&b==0&&n==0)break;  
        f[1]=1;  
        f[2]=1;  
        int i;  
        for(i=3; i<=1000; i++)  
        {  
            f[i]=(a*f[i-1]+b*f[i-2])%7;  
            if(f[i]==1&&f[i-1]==1)  
                break;  
        }  
        n=n%(i-2);  
        f[0]=f[i-2];  
        printf("%d\n",f[n]);  
    }  

}  


1007 时钟问题

题目理解:输入一个角度,计算一天内三个指针相差大于这个角度的时间占整个时间的比值,并输出。

解析:本题可以采取枚举法,先计算一秒内各个时钟移动的角度,再一秒一秒的往上加,计算每一秒的各个指针之间的距离,统计满足条件的时间占总时间比。

#include <iostream>  
#include <cstdio>  
using namespace std;  
double max1(double a,double b,double c)  
{  
     if(a<b) a=b;  
     if(a<c) a=c;  
     return a;  
}  
double min1(double a,double b,double c)  
{  
     if(a>b) a=b;  
     if(a>c) a=c;  
     return a;  
 }  
 
int main(){  
    double sum=0.0,d,newx,newy;  
    double mh=43200.0000/11;  
    double sm=3600.0000/59;  
    double sh=43200.0000/719;  
    double dsm,dsh,dmh,tdsm,tdsh,tdmh;  
    cin>>d;  
    while(d!=-1){  
        sum=0.0;  
        dsm=10.0*d/59.0;  tdsm=sm-dsm;  
        dsh=120.0*d/719.0;   tdsh=sh-dsh;  
        dmh=120.0*d/11.0;    tdmh=mh-dmh;  
        newx=max1(dsm,dsh,dmh); newy=min1(tdsm,tdsh,tdmh);  
        while(newx<=43200&&newy<=43200)  
        {  
            newx=max1(dsm,dsh,dmh);           
            newy=min1(tdsm,tdsh,tdmh);        
            if(newx<newy)   
                sum+=newy-newx;   
            if(newy==tdsm)   
            {  
                dsm+=sm;  
                tdsm+=sm;  
            }  
            else if(newy==tdsh)   
            {  
                dsh+=sh;  
                tdsh+=sh;  
            }  
            else if(newy==tdmh)  
            {  
                dmh+=mh;  
                tdmh+=mh;  
            }  
        }  
          
        printf("%.3lf\n",sum/432);  
        cin>>d;  
    }  
    return 0;  

}  


1008 电梯问题

解析:只需要每次判断电梯运行的方向即可

#include <iostream>  
using namespace std;  
int main()  
{  
    int x[10000]={0},t,m;  
    while(cin>>m)  
    {  
        if(m!=0)  
        {  
            t=0;  
            for(int i=1;i<=m;i++)  
            {  
                cin>>x[i];  
                if(x[i-1]<x[i])  
                {  
                    t+=(x[i]-x[i-1])*6;  
                }  
                if(x[i-1]>x[i])  
                {  
                    t+=(x[i-1]-x[i])*4;  
                }  
                t+=5;  
            }  
            cout<<t<<endl;  
        }  
        else  
            break;  
    }  
    return 0;  

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值