Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- unsigned long long pow(int x, int y)
- {
- unsigned long long res;
- res = x;
- if(y<1)
- {
- return 1;
- }
- else
- {
- for(int i=1;i<y;i++)
- {
- res=res*x;
- }
- return res;
- }
- }
- int main(void)
- {
- unsigned long long main_mass=10;
- int ratio, secondary_mass=1;
- double main_vel=1, secondary_vel=0, vcm;
- unsigned long long collisions=0;
- do
- {
- printf("input the weight of the main mass(positive) A, so that weight=100^A, the secondary mass is 1kg");
- scanf("%d",&ratio);
- main_mass=pow(100,ratio);
- }
- while(ratio<0);
- do
- {
- //cubes collide
- vcm = (main_mass*main_vel+secondary_mass*secondary_vel)/(main_mass+secondary_mass);
- // printf("2*%f-%f=",vcm,main_vel);
- main_vel = (2*vcm-main_vel);
- // printf("%f\n",main_vel);
- secondary_vel = 2*vcm-secondary_vel;
- collisions++;
- if(secondary_vel>0){
- //wall and cube collide
- secondary_vel = secondary_vel*(-1);
- collisions ++;}
- }
- while( secondary_vel < main_vel ||( secondary_vel>0 && main_vel > 0));
- printf("%llu collisions\n",collisions);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement