Editorial for C++ Bài 3.C13 [Vòng lặp For, While, Do-while]: Tổng chuỗi
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lời giải chi tiết:
Đầu tiên, các bạn cần khai báo kiểu dữ liệu của số nguyên n dựa vào khoảng giá trị đề bài đưa \(n\) \((n \leq 10^6)\).
Bước 2, các bạn dùng lệnh cin để nhập số vừa khai báo ở trên.
Bước 3, duyệt các số từ 1 đến n, với mỗi lần duyệt:
Đầu tiên ta cộng giá trị của
ivào biếnsum.Tiếp theo ta cộng giá trị của
sumvào biếnans. Điều này tính tổng của các số từ 1 đếnitại mỗi bước của vòng lặp.
Bước 4, In ra tổng cuối cùng của các tổng.
Comments