tungggg

boi so using queue

Mar 18th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. /*Bài 2. Bội số chỉ chứa 0 và 9
  2. Cho số tự nhiên N. Hãy tìm số nguyên dương X nhỏ nhất được tạo bởi số 9 và số 0 chia hết cho N. Ví dụ với N = 5 ta sẽ tìm ra  X = 90.
  3. Input:
  4. Dòng đầu tiên ghi lại số lượng test T (T≤100).
  5. Những dòng kế tiếp mỗi dòng ghi lại một test. Mỗi test là một số tự nhiên N được ghi trên một dòng (N≤100).
  6. Output:
  7. Đưa ra theo từng dòng số X nhỏ nhất chia hết cho N tìm được .
  8. Ví dụ:
  9. Input
  10. Output
  11. 2
  12. 5
  13. 7
  14. 90
  15. 9009 */
  16.  
  17. #include<bits/stdc++.h>
  18. using namespace std;
  19. #define pub push_back
  20. #define ll long long
  21. string s[10001];
  22.  
  23. int  findThisNumber (int n){
  24.     queue < string > q;
  25.     q.push ("9");
  26.     while (1){
  27.         string getFront = q.front ();
  28.         q.pop();
  29.         int current = stoi (getFront);
  30.         if ( current % n == 0 ){
  31.             return current ;
  32.         }
  33.         string first = getFront + "0";
  34.         string second = getFront + "9";
  35.         q.push(first );
  36.         q.push(second);  
  37.     }
  38. }
  39.  
  40. int main(){
  41.     int t;
  42.     cin >> t;
  43.     while ( t-- ){
  44.         int n;
  45.         cin >> n;
  46.         cout << findThisNumber(n) << endl;
  47.     }
  48.     return 0;
  49. }
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment