[Làm Quen OJ]. Bài 20. Sử dụng cout 2

View as PDF

Submit solution

Points: 1.00
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem source:
28Tech
Problem type
Allowed languages
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho số N, bạn hãy in ra số N với 6 chữ số, nếu N không đủ 6 chữ số thì tiến hành chèn 0 và dấu # vào đầu cho đủ 6 chữ số.

Cú pháp để in ra 1 số nguyên n với độ rộng K, điền đầy ký tự C vào đầu nếu không đủ K chữ số

#include <iomanip>
cout << setw(K) << setfill(C) << n << endl;

Đầu vào

Dòng duy nhất chứa số N


Ràng buộc

0<=N<10^6


Đầu ra

Dòng 1 in ra N với số 0 được chèn vào trước

Dòng 2 in ra N với dấu # được chèn vào trước


Ví dụ

Input 01
850
Output 01
000850
###850

Comments

Please read the guidelines before commenting.



  • -1
    KnMinh  commented on Nov. 26, 2025, 10:45 a.m.

    include <stdio.h>

    int main() { long long n; scanf("%lld", &n);

    long long temp = n;
    int set = 0;
    if (temp == 0) set = 1;
    else {
        while (temp > 0) {
            set++;
            temp /= 10;
        }
    }
    if(set >= 6) {
        printf("%lld\n", n % 1000000);
        printf("%lld", n % 1000000);
        return 0;
    }
    int need = 6 - set;
    
    // Dòng 1: thêm 0 vào đầu
    for (int i = 0; i < need; i++) {
        printf("0");
    }
    printf("%lld\n", n);
    
    // Dòng 2: thêm # vào đầu
    for (int i = 0; i < need; i++) {
        printf("#");
    }
    printf("%lld", n);
    
    return 0;
    

    }


  • 0
    L8_DuongThanhKhiem  commented on Nov. 22, 2025, 4:16 a.m.

    Code cho các bạn nek:

    #include <bits/stdc++.h>
    using namespace std;
    int a;
    int main(){
        cin >> a;
        cout << setw(6) << setfill('0') << a << endl;
        cout << setw(6) << setfill('#') << a;
        return 0;
    }
    

  • 0
    dung_482  commented on April 17, 2025, 4:23 p.m.

    n = input().strip() length = len(n) if length < 6: khoangtrong = 6 - length khoangtrongthem = '0' * khoangtrong + n else: khoangtrongthem = n if length < 6: hashtag = 6 - length hashtagthem = '#' * hashtag + n else: hashtagthem = n print(khoangtrongthem) print(hashtagthem)


  • -1
    bengokyeuanh99  commented on April 16, 2025, 11:21 a.m.

    def formatnumber(n: int, width: int = 6) -> tuple[str, str]: paddedzero = f"{n:0>{width}}" paddedhash = f"{n:#>{width}}" return paddedzero, padded_hash

    def main(): try: n = int(input().strip()) if not (0 <= n < 10**6): raise ValueError("N must be in range 0 <= N < 10^6") zerofilled, hashfilled = formatnumber(n) print(zerofilled) print(hash_filled) except Exception as e: print(f"Input Error: {e}")

    if name == "main": main()


  • 0
    KingOfHust  commented on April 16, 2025, 7:41 a.m.

    For Python

    N = int(input())

    print(f'{N:0>6}')

    print(f'{N:#>6}')


  • -3
    Try_hardxx  commented on Oct. 27, 2024, 4:20 a.m.

    include<bits/stdc++.h>

    using namespace std;

    int main(){ long long a,b,c,d; cin >> a; cout << setw(6) << setfill('0') << a <<'\n'; cout << setw(6) << setfill('#') << a;

    }


  • 5
    trong_nghia_245  commented on Sept. 4, 2024, 3:27 p.m.

    Lời giải cho python

    enter code herex = int(input())
    enter code here = print('{:06d}'.format(x))
    enter code here = print('{:>6d}'.format(x).replace(' ', '#'))