这道题的思路是,将题目中所描述的齿轮看成是圆,其中两个齿轮相互咬合则是两个圆相切,那么根据判断两个圆相切的方法来判断两个齿轮间是否咬合。其中还会用到齿轮传动的规律,即传动轮的速度由主动轮来决定,根据该题的描述可知所有齿轮中只有一个是主动轮,那么其他齿轮的速度都都它决定,随度的比值就是R1/Ri。

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int x[1003];
int y[1003];
int r[1003];
int flag[1003];
int queue[1003];
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    //freopen("all.in","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&x[i],&y[i],&r[i]);
            flag[i]=0;
        }
        flag[1]=1;
        queue[0]=1;
        int head=0,tail=1;
        while(head!=tail)
        {
            for(int i=2;i<=n;i++)
            {
                if(queue[head]==i)
                    continue;
                if(flag[i]!=0)
                    continue;
                int t1=(x[queue[head]]-x[i])*(x[queue[head]]-x[i])+(y[queue[head]]-y[i])*(y[queue[head]]-y[i]);
                int t2=(r[queue[head]]+r[i])*(r[queue[head]]+r[i]);
                if(t1==t2)
                {
                    flag[i]=-flag[queue[head]];
                    queue[tail]=i;
                    tail++;
                }
            }
            head++;
        }
        for(int i=1;i<=n;i++)
        {
          if(flag[i]==0)
            printf("not moving\n");
          else if(flag[i]==1)
          {
            int t=gcd(r[1],r[i]);
            if(r[i]/t==1)
                printf("%d ",r[1]/t);
            else
            {
                printf("%d/",r[1]/t);
                printf("%d ",r[i]/t);
            }
            printf("clockwise\n");
          }
          else
          {
            int t=gcd(r[1],r[i]);
            if(r[i]/t==1)
                printf("%d ",r[1]/t);
            else
            {
                printf("%d/",r[1]/t);
                printf("%d ",r[i]/t);
            }
            printf("counterclockwise\n");
          }
        }
    }
    return 0;
}