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;
}