生活随笔
收集整理的這篇文章主要介紹了
【PAT乙级】1027 打印沙漏 (20 分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目地址
#include<cstdio>
#include<iostream>
using namespace std
;
int main(void)
{int n
; char c
; cin
>>n
>>c
;int sum
,s
;for(int i
=1;i
<=100;i
++){sum
=2*i
*i
-1;if(sum
>=n
){if(sum
==n
) s
=i
;else s
=i
-1,sum
=2*(i
-1)*(i
-1)-1;break;}}for(int i
=1;i
<=s
;i
++){for(int j
=1;j
<=i
-1;j
++) cout
<<" ";for(int j
=1;j
<=(s
-i
)*2+1;j
++) cout
<<c
;cout
<<endl
;}for(int i
=1;i
<s
;i
++){for(int j
=1;j
<s
-i
;j
++) cout
<<" ";for(int j
=1;j
<=2*i
+1;j
++) cout
<<c
;cout
<<endl
;}cout
<<n
-sum
<<endl
;return 0;
}
#include<bits/stdc++.h>
using namespace std
;
int main(void)
{int n
,m
,ans
;char c
; cin
>>n
>>c
;for(int i
=0;i
<100;i
++){int temp1
=i
+i
*(i
-1);temp1
=temp1
*2-1;int temp2
=(i
+1)+(i
+1)*i
;temp2
=temp2
*2-1; if(n
>temp1
&&n
<=temp2
){if(n
==temp2
) m
=i
+1,ans
=0;else m
=i
,ans
=n
-temp1
;}}for(int i
=1;i
<=m
;i
++) {for(int j
=1;j
<=i
-1;j
++) cout
<<" ";for(int j
=1;j
<=1+(m
-i
)*2;j
++) cout
<<c
;cout
<<endl
;}for(int i
=1;i
<=m
;i
++){if(i
!=1){for(int j
=1;j
<=m
-i
+1-1;j
++) cout
<<" ";for(int j
=1;j
<=1+(i
-1)*2;j
++) cout
<<c
;cout
<<endl
;}}cout
<<ans
;return 0;
}
總結
以上是生活随笔為你收集整理的【PAT乙级】1027 打印沙漏 (20 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。