CCF 201803-2 碰撞的小球

 1 #include <iostream>
 2 //#include <bits/stdc++.h>
 3 #include <string>
 4 
 5 using namespace std;
 6 
 7 typedef struct {
 8     int location;
 9     int foreOrback;
10 }table;
11 
12 bool isequal(table *tab,int i,int n,int L);
13 void change(table &tt);
14 
15 void move(table *tab,int n,int L)
16 {
17     for(int i=0;i<n;i++)
18     {
19         if(isequal(tab,i,n,L))
20             tab[i].location+=tab[i].foreOrback;
21         else
22         {
23             //change(tab[i]);
24             tab[i].location+=tab[i].foreOrback;
25         }
26     }
27 }
28 
29 bool isequal(table *tab,int i,int n,int L)
30 {
31     if(tab[i].location==L&&tab[i].foreOrback==1)
32     {
33         tab[i].foreOrback=-1;
34         return false;
35     }
36     else if(tab[i].location==0&&tab[i].foreOrback==-1)
37     {
38         tab[i].foreOrback=1;
39         return false;
40     }
41     for(int j=0;j<n;j++)
42     {
43         if(j!=i)
44         {
45             if(tab[j].location==tab[i].location)
46             {
47                 change(tab[j]);
48                 change(tab[i]);
49                 return false;
50             }
51         }
52     }
53     return true;
54 }
55 
56 void change(table &tt)
57 {
58     if(tt.foreOrback==-1)
59         tt.foreOrback=1;
60     else
61         tt.foreOrback=-1;
62 }
63 
64 int main()
65 {
66     int n,L,t;
67     cin>>n>>L>>t;
68     table *s;
69     s=new table[n];
70     for(int i=0;i<n;i++)
71     {
72         cin>>s[i].location;
73         s[i].foreOrback=1;
74     }
75     while(t--)
76     {
77         move(s,n,L);
78     }
79     for(int i=0;i<n;i++)
80     {
81         if(i==0)
82             cout<<s[i].location;
83         else
84             cout<<" "<<s[i].location;
85     }
86     return 0;
87 }
原文地址:https://www.cnblogs.com/-Asurada-/p/14236432.html