thieumao

Tính số phần tử khác nhau

Apr 8th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. /*
  2. Nhập 1 dãy số gồm n số nguyên từ file number.inp
  3. Yêu cầu:Tính số phần tử khác nhau trong dãy rồi xuất ra file number.out
  4. VD:2 3 4 4 4 5
  5. Kết quả là: 4
  6. */
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string.h>
  11. #include <windows.h>
  12. #include <conio.h>
  13. #include <stdlib.h>
  14.  
  15. using namespace std;
  16.  
  17. int main()
  18. {
  19.     int a[100];
  20.     int k;
  21.     // Doc file number.inp
  22.     ifstream Doc ("number.inp");
  23.     if(! Doc.is_open()) {
  24.         cout<<"Khong the mo file.\n";
  25.         return 0;
  26.     }
  27.     else {
  28.         k=0;
  29.         while(Doc.good()) {
  30.               k=k+1;
  31.               Doc>>a[k];
  32.         }
  33.     }
  34.     Doc.close();
  35.     int tg;
  36.     for(int i=1; i < k; i++) {
  37.         for (int j=i+1; j<k; j++){
  38.             if (a[i]>a[j]) {
  39.                 tg=a[i];
  40.                 a[i]=a[j];
  41.                 a[j]=tg;
  42.             }
  43.         }
  44.     }
  45.     int j=1;
  46.     for(int i=1; i < k-1; i++) {
  47.         if (a[i+1]>a[i]) {
  48.             j++;
  49.         }
  50.     }
  51.     // Ghi file number.out
  52.     ofstream Ghi ("number.out");
  53.     Ghi<<j;
  54.     Ghi.close();
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment