Advertisement
theo830

stacking boxes

Apr 28th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <stack>
  7. using namespace std;
  8. int isPrime(int n) {
  9. int i;
  10. if(n == 2) return 1;
  11. if(n%2 == 0) return 0;
  12. for(i = 3; i*i<=n; i+=2) {
  13. if(n%i == 0) return 0;
  14. }
  15. return 1;
  16. }
  17. int main() {
  18. int a;
  19. stack<int>s1;
  20. stack<int>s2;
  21. while(cin>>a){
  22. if(isPrime(a)){
  23. s1.push(a);
  24.  
  25. }
  26. else{
  27. s2.push(a);
  28. }
  29. if(a == 2 || a == 4){
  30. break;
  31. }
  32. }
  33. if(s1.top()== 2){
  34. while(!s2.empty()){
  35. cout<<s2.top()<<endl;
  36. s2.pop();
  37. }
  38. while(!s1.empty()){
  39. cout<<s1.top()<<endl;
  40. s1.pop();
  41. }
  42. }
  43. else if(s2.top()==4){
  44. while(!s1.empty()){
  45. cout<<s1.top()<<endl;
  46. s1.pop();
  47. }
  48. while(!s2.empty()){
  49. cout<<s2.top()<<endl;
  50. s2.pop();
  51. }
  52. }
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement