c++,直接输出可以,但我更喜欢for
查看原帖
c++,直接输出可以,但我更喜欢for
602687
fujunye楼主2024/4/4 19:12

这道题目是入门基础题 可以直接输出

#include<bits/stdc++.h>
using namespace std;

int main(){
	cout<<"  *"<<endl<<" ***"<<endl<<"*****"<<endl<<" ***"<<endl<<"  *"<<endl;
	return 0;
}
或者也可以设两个边界,在边界内输出“*”,否则输出“ ”。
就是这样:
if(j>=k1&&j<=k2)cout<<"*";//边界内输出“*”
else cout<<" ";
每过一轮边界变化
边界行数i<3时,边界扩大,否则边界缩小
就是这样:
	int k1=3,k2=3//边界
   	if(i<3)k1--,k2++;
	else k2--,k1++;
然后套两个for,表示行列(i和j),菱形是5*5的
最后是这样:
#include<bits/stdc++.h>
using namespace std;

int main(){
	int k1=3,k2=3;
	for(int i=1;i<=5;i++){
		for(int j=1;j<=5;j++){
			if(j>=k1&&j<=k2)cout<<"*";//边界内输出“*”
			else cout<<" ";
		}
		cout<<endl;
		if(i<3)k1--,k2++;
		else k2--,k1++;
	}
	return 0;	
}

好了,讲完了,感谢收看
2024/4/4 19:12
加载中...