Blue Bridge - Isosceles Triangle Water Problem

Idea: Preprocess all the numbers to be used and put them into a large string,
and then deal with the coordinate changes according to the situation.
code show as below:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define sd(a) scanf("%d",&a)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define cl(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define dbg() printf("aaa\n")
using namespace std;
const int maxn=1010;
char mp[maxn][maxn];
char c[maxn*10];
int main() {
    
    
    //初始化
    int p=0;
    for(int i=1;i<=999;i++){
    
    
        if(i<=9){
    
    
            c[++p]='0'+i;
        }else if(i<=99){
    
    
            c[++p]='0'+i/10;
            c[++p]='0'+i%10;
        }else if(i<=999){
    
    
            c[++p]='0'+i/100;
            c[++p]='0'+i/10%10;
            c[++p]='0'+i%10;
        }
    }
	int n;
    sd(n);
    rep(i,1,n){
    
    
        rep(j,1,n+i-1){
    
    
            mp[i][j]='.';
        }
    }
    int k=0;
    int i=1,j=n;
    while(1){
    
    
        mp[i][j]=c[++k];
        if(i<n&&j<=n){
    
    
            i++,j--;
        }else if(i==n&&j!=2*n-1){
    
    
            j++;
        }else{
    
    
            i--,j--;
        }
        if(i==1&&j==n) break;
    }
    rep(i,1,n){
    
    
        rep(j,1,n+i-1){
    
    
            putchar(mp[i][j]);
        }putchar('\n');
    }
	return 0;
}

This problem requires you to output an isosceles triangle composed of numbers to the console.
The specific steps are:

  1. First use the natural numbers of 1, 2, 3, ... to make a long enough string

  2. Fill the three sides of the triangle with this string. Start at the top vertex and fill in counter-clockwise.
    For example, when the triangle height is 8:

    1
    

    2 1
    3 8
    4 1
    5 7
    6 1
    7 6
    891011121314151

Input
A positive integer n (3<n<300) representing the height of the triangle
Output
An isosceles triangle filled with numbers.

For the convenience of evaluation, we require spaces to be replaced with ".".
sample input

5

Sample output

…1
…2.1
…3…2
.4…1
567891011

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326042093&siteId=291194637