我得到了下面的代碼來列印一個模式(附在下面)。但是我想只使用一個回圈
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i ){
for(int j=1;j<=i;j ){
cout<<"*";
}
for(int j=1;j<=n-i;j ){
if(j%2!=0){
cout<<"_";
}else{
cout<<".";
}
}
cout<<endl;
}
for(int i=1;i<n;i ){
for(int j=1;j<=n-i;j ){
cout<<"*";
}
for(int j=1;j<=i;j ){
if(j%2==0){
cout<<".";
}else{
cout<<"_";
}
}
cout<<endl;
}
}
當 n = 5 時,輸出如下。
*_._.
**_._
***_.
****_
*****
****_
***_.
**_._
*_._.
我怎么把它變成一個回圈
uj5u.com熱心網友回復:
試試這個,看看它是如何完成你想要理解的你自己沒有找到的步驟的:
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n*2-1; i ) {
if (i <= n)
{
for (int j = 1; j <= i; j ) {
cout << "*";
}
for (int j = 1; j <= n - i; j ) {
if (j % 2 != 0) {
cout << "_";
}
else {
cout << ".";
}
}
cout << endl;
}
else
{
for (int j = 1; j <= n*2 - i; j ) {
cout << "*";
}
for (int j = 1; j <= i-n; j ) {
if (j % 2 == 0) {
cout << ".";
}
else {
cout << "_";
}
}
cout << endl;
}
}
}
uj5u.com熱心網友回復:
我只想使用一個回圈。
我會從字面上理解并展示一個可能的解決方案的起點。
// Let's start by figuring out some dimensions.
int n;
std::cin >> n;
int height = 2 * n - 1;
int area = n * height;
// Now we'll print the "rectangle", one piece at a time.
for (int i = 0; i < area; i)
{ // ^^^^^^^^
// Extract the coordinates of the char to be printed.
int x = i % n;
int y = i / n;
// Assign a symbol, based on such coordinates.
if ( x <= y and x <= height - y - 1 )
{ // ^^^^^^ ^^^^^^^^^^^^^^^^^^^ Those are the diagonals.
std::cout << '*'; // This prints correctly the triangle on the left...
}
else
{
std::cout << '_'; // <--- But of course, something else should done here.
}
// End of row.
if ( x == n - 1 )
std::cout << '\n';
}
uj5u.com熱心網友回復:
如果你看一下圖案,那么你會看到一種“三角形”。這已經為解決方案提供了提示。使用三角函式。
請在此處閱讀。
然后你會注意到總是std::abs涉及到 C 中的“絕對”函式。
但首先,很容易看出要列印的行數始終是三角形的寬度 * 2。
并且模式中的字符數可以通過應用三角形函式來計算。寬度 5 的示例:
Number of stars number of dashdot
Row width-abs(row-width) abs(row-width)
1 1 4
2 2 3
3 3 2
4 4 1
5 5 0
6 4 1
7 3 2
8 2 3
9 1 4
現在可以很容易地實作這一點。
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std::string_literals;
int main() {
// Get the max width of the pattern and perform a short input validation
int maxWidth{};
if ((std::cin >> maxWidth) and (maxWidth > 0)) {
// The number of rows for the pattern is dependent on the width. It is a simple relation
const int numberOfRows = 2 * maxWidth;
// Show all rows
for (int row = 1; row < numberOfRows; row) {
// Use triangle formular to create star pattern
std::string starPattern(maxWidth - std::abs(row - maxWidth), '*');
// Create dashDot pattern
std::string ddp(std::abs(row - maxWidth), '\0');
std::generate(ddp.begin(), ddp.end(), [i = 0]() mutable { return i % 2 ? '.' : '_'; });
// Show output
std::cout << (starPattern ddp) << '\n';
}
}
else std::cout << "\n*** Error: Invalid input\n\n";
}
當然你也可以機智地創建整個模式std::generate。
也許現在太復雜了。
而且更難理解。
看:
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
int main() {
// Get the max width of the pattern and perform a short input validation
if (int width{}; (std::cin >> width) and (width > 0)) {
std::vector<std::string> row(2 * width - 1);
std::for_each(row.begin(), row.end(), [&, r = 1](std::string& s) mutable {
s = std::string(width - std::abs(r - width), '*');
std::string ddp(std::abs(r - width),'\0');
std::generate(ddp.begin(), ddp.end(), [&, i = 0]() mutable{ return i % 2 ? '.' : '_'; });
s = ddp; std::cout << s << '\n'; });
}
else std::cout << "\n*** Error: Invalid input\n\n";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/528602.html
標籤:C 循环
