PAT1027 打印沙漏 (20 分)

题目描述
在这里插入图片描述
在这里插入图片描述

C++解法

  • 解法1
#include<cstdio>
#include<cmath>
int main(){
    int n;
    char c;
    scanf("%d %c",&n,&c);
    int bottom=(int)sqrt(2*(n+1))-1;
    if(bottom%2==0) bottom--;
    int used=(bottom+1)*(bottom+1)/2-1;
    for(int i=bottom;i>=0;i-=2){
        for(int j=0;j<(bottom-i)/2;j++){
            printf(" ");
        }
        for(int j=0;j<i;j++){
            printf("%c",c);
        }
        printf("\n");
    }
    for(int i=3;i<=bottom;i+=2){
        for(int j=0;j<(bottom-i)/2;j++){
            printf(" ");
        }
        for(int j=0;j<i;j++){
            printf("%c",c);
        }
        printf("\n");
    }
    printf("%d\n",n-used);
}

思路:先打印倒三角形的沙漏

 for(int i=bottom;i>=0;i-=2){
        for(int j=0;j<(bottom-i)/2;j++){
            printf(" ");
        }
        for(int j=0;j<i;j++){
            printf("%c",c);
        }
        printf("\n");

在打印正三角形的沙漏

    for(int i=3;i<=bottom;i+=2){
        for(int j=0;j<(bottom-i)/2;j++){
            printf(" ");
        }
        for(int j=0;j<i;j++){
            printf("%c",c);
        }
        printf("\n");

最后打印剩余个数

printf("%d\n",n-used);

  • 解法2
#include <iostream>
using namespace std;
int main() {
    int N, row = 0;
    char c;
    cin >> N >> c;
    for (int i = 0; i < N; i++) {
        if ((2 * i * (i + 2) + 1) > N) {
            row = i - 1;
            break;
        }
    }
    for (int i = row; i >= 1; i--) {
        for (int k = row - i; k >= 1; k--) cout << " ";
        for (int j = i * 2 + 1; j >= 1; j--) cout << c;
        cout << endl;
    }
    for (int i = 0; i < row; i++) cout << " ";
    cout << c << endl;
    for (int i = 1; i <= row; i++) {
        for (int k = row - i; k >= 1; k--) cout << " ";
        for (int j = i * 2 + 1; j >= 1; j--) cout << c;
        cout << endl;
    }
    cout << (N - (2 * row * (row + 2) + 1));
    return 0;
}

思路:每个沙漏都是从最中间一行行向上下分别扩展一行,每次扩展行都要比之前一层多2个符号,最中间一行只有 1 个符号,假设扩展的层数为 i,则扩展出去的上边需要的所有符号个数为3 + 5 + 7 + … + (2i+1) = (3 + 2i + 1) * i / 2 = i* (i + 2),扩展出去的下边与上边同样多所以乘以2,加上最重要那一行1个符号,所以 总共需要2 * i * (i + 2) + 1个符号,所以i从0到N,找满足(2 * i * (i + 2) + 1) > N的最小的 i,因为符号不能超过N,所以只能扩展出去 i-1行,用变量row表示从最中间一行需要扩展出去的行数,row = i – 1,接下来开始输出,上面的每一行,对于扩展出去的第 i层需要输出row – i个空格,接着输出i * 2 + 1个符号c和换行符;对于最中间一行,需要输出row –1个空格、符号c和换行符;对于下面的每一行,对于扩展出去的第i层,需要输出row-i个空格,接着输出i * 2 +1个符号c和换行符,因为用掉的符号数为2 * row * (row + 2) + 1,所以最后输出剩下没用掉的符号数为N – (2 *row * (row + 2) + 1)

Java解法

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        char c = in.next().charAt(0);
        in.close();
 
        int w = (int) Math.sqrt((n + 1) / 2);
        for (int i = 0; i < 2 * w - 1; i++) {
            for (int j = 0; j < 2 * w - 1; j++) {
                if ((i > j && i + j < 2 * w - 2) || (i < j && i + j > 2 * w - 2)) {
                    if (i > j && i + j < 2 * w - 2)
                        System.out.print(" ");
                } else {
                    System.out.print(c);
                }
            }
 
            System.out.println();
        }
 
        System.out.println(n - 2 * w * w + 1);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值