fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. unsigned int multiply (unsigned short a, unsigned short b)
  6. {
  7. unsigned int result = 0;
  8. char negflag = 0;
  9.  
  10. if ( (a < 0) != (b < 0) )
  11. {
  12. negflag++;
  13. a &= 0b0111111111111111;
  14. b &= 0b0111111111111111;
  15. }
  16. char n = 0;
  17. while ( b >= (1 << (n)) )
  18. {
  19. if ( b & (1 << n) )
  20. result += a << n;
  21. n++;
  22. }
  23. return result;
  24. }
  25.  
  26. int main(void)
  27. {
  28.  
  29. unsigned short a, b;
  30. //scanf("%hi%hi", &a, &b);
  31. a = 1;
  32. b = 1;
  33.  
  34. //это типа такое юнит-тестирование. Если оно зависает, значит все норм
  35. while (multiply(a, b) == a * b)
  36. {
  37. if (SHRT_MIN == a)
  38. {
  39. b++;
  40. }
  41. a++;
  42. // fprintf(stderr," %i : %i", a,b);
  43. }
  44. fprintf(stderr,"my: %i ; norm: %i\n", multiply(a, b), a * b);
  45.  
  46. return 0;
  47. }
  48.  
Time limit exceeded #stdin #stdout 5s 1784KB
stdin
1 1
stdout
Standard output is empty