技巧:
先计算有多少行,再计算每个列的长度(可变的变量),在定义两个变量控制每一列的开始和结束,s,t 。
模板
int n = ini();
int m,s,t;
for(int i = 0; i < n; ++i) {
for(int j = s; j <= t; ++j) {
...
}
//或者
for(int j = 0; j < m; ++j) {
...
}
}
1 Simple Accepted / Used
题链接:https://ac.nowcoder.com/acm/contest/19305/1003
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int ini()
{
char c=getchar();while(c<'A' || c>'Z') c=getchar();
return c;
}
int main() {
char ch = ini();
int n = (ch-'A')+1;
int s = n, t = n, mid = n;
char ch1 = 'A'-1;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= t; ++j) {
if(j >= s) {
if(j > mid) {
cout<< (char)(--ch1);
}else {
cout<< (char)(++ch1);
}
}else {
cout<< ' ';
}
}
cout<<endl;
++t;
--s;
ch1 = 'A'-1;
}
return 0;
}
2 Simple Accepted / Used
题链接:https://ac.nowcoder.com/acm/contest/19305/1004
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int ini()
{
int x=0,f=1;char c=getchar();while(c<48||c>57){if(c=='-') f=-1; c=getchar();}
while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48);c=getchar();} return x*f;
}
int main() {
int T = ini();
while(T-->0) {
int n = ini();
if(n==1) {
cout<< '*' <<endl;
continue;
}
int s = n/2+1,len = n/2+1;
for(int i = 0; i < n/2+1; ++i) {
for(int j = 1; j <= len; ++j) {
if(j >= s) cout<< '*';
else cout<< ' ';
}
cout<<endl;
s--;
len++;
}
s = 2; len = n-1;
for(int i = 0; i < n/2; ++i) {
for(int j = 1; j <= len; ++j) {
if(j >= s) cout<< '*';
else cout<< ' ';
}
cout<<endl;
s++;
len--;
}
}
return 0;
}
3 Simple Accepted / Used
题链接:https://ac.nowcoder.com/acm/contest/19305/1005
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int ini()
{
int x=0,f=1;char c=getchar();while(c<48||c>57){if(c==-1) return -1;c=getchar();}
while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48);c=getchar();}return x*f;
}
int main() {
while(true) {
int n = ini();
if(n < 0) break;
int s = 2*n+1, t = s;
for(int i = 0; i < n+1; ++i) {
for(int j = 1; j <= t; ++j) {
if(j >= s) cout<< '*';
else cout<< ' ';
}
cout<<endl;
s -= 2;
t -= 1;
}
s = 3, t = n+2;
for(int i = 0; i < n; ++i) {
for(int j = 1; j <= t; ++j) {
if(j >= s) cout<< '*';
else cout<< ' ';
}
cout<<endl;
s += 2;
t += 1;
}
}
return 0;
}