Advertisement
rotti321

Sortare dupa prima cifra

Feb 24th, 2022
51
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ///Avem un sir cu n nr naturale. Sortati nr din sir cresc dupa prima cifra:
  5. /*
  6. IN:
  7. 6
  8. 89 22 91 4005 51 721
  9. OUT:
  10. 22 4005 51 721 89 91
  11. */
  12. int main(){
  13. ifstream cin("test.in");
  14. ofstream cout("test.out");
  15. //Andreea -declaratii
  16. int n,a[501],i,x,y,sumx,sumy,c;
  17. //Georgiana -citire sir
  18. cin>>n;
  19. for (int i=1;i<=n;i++){
  20. cin >> a[i];
  21. }
  22. for(i=1; i<=n; i++){
  23. for(int j=i+1 ; j<=n ;j++){
  24. x=a[i];
  25. y=a[j];
  26. sumx=0;
  27. sumy=0;
  28. ///G
  29. while(x>=10){
  30. x=x/10;
  31. }
  32. while(y>=10){
  33. y=y/10;
  34. }
  35.  
  36.  
  37. if(x>y){
  38. swap(a[i],a[j]);
  39. }
  40. }
  41. }
  42.  
  43.  
  44. //Bianca -afisare sir
  45. for(i=1; i<=n; i++){
  46. cout<<a[i] << " ";
  47. }
  48.  
  49. }
  50.  
  51.  
  52.  
Advertisement
RAW Paste Data Copied
Advertisement