马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
小白一枚,想了半天才想出沙漏的上半部分,可还是进入了死循环,有大神能帮忙改下吗 #include "stdafx.h"
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int main(){
float n=1,N,e;string a;cin>>N>>a;
while(((pow((n+1),2)-4)/2)<=N){
n+=2;
e=n;
}
n-=2;e-=2;//找出第一行n最大值
for(int c=0;n!=-1;n-=2){
for(float d=e;(n+1)/2<(d+1)/2;n+=2){
cout<<" ";//每一行前半部分空格
}
for(int f=0;n!=0;--n){
cout<<a;//每一行符号
}
for(float d=e;(n+1)/2<(d+1)/2;++n){
cout<<" ";//每一行后半部分空
}
cout<<endl;}
cout<<N-(pow((n+1),2)-4)/2;
return 0;
}
N=15,a='a';
n=3,e=3;
第一次循环:
for(int c=0;n!=-1;n-=2) //n=3,e=3
for(float d=e;(n+1)/2<(d+1)/2;n+=2) //不执行,n=3,e=3;
for(int f=0;n!=0;--n) //打印‘aaa’,n=0,e=3;
for(float d=e;(n+1)/2<(d+1)/2;++n) //打印三个空格 ,n=3,e=3;
第二次循环:
for(int c=0;n!=-1;n-=2) //n=1,e=3;
for(float d=e;(n+1)/2<(d+1)/2;n+=2) //打印一个空格,n=3,e=3;
之后就是无限循环了。
|