PAT 00-自测1. 打印沙漏(20)

今天第一次在线(浙大PAT)刷算法题,折腾好几个小时后,终于成功了:)

c用的少,写起来好费力,将多个*拼接,就百度好久。下面再用其他语言试试。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(){
    /*PAT 00-自测1. 打印沙漏(20)
	*2015.4.13
	*shaonian.ding
	*PATLink:http://www.patest.cn/contests/mooc-ds2015spring/00-%E8%87%AA%E6%B5%8B1
	*/	 

	int test = 1000;
	char cStar = '*';
	//scanf_s("%c", &cStar,1);
	scanf("%d %c", &test, &cStar);

	int temp = (test + 1) / 2;
	int n = (int)sqrt((double)temp);
	int i = n;
	
	for (i; i > 0; i--)
	{
		int numStar = 2 * (i - 1) + 1;
		char *star = (char *)calloc(numStar + 1, 1);
		char *space = (char *)calloc(n - i + 1, 1);
		char *lineStar = (char *)memset(star, cStar, numStar);
		char *lineSpace = (char *)memset(space, ' ', n - i);
		printf("%s%s\n", space, lineStar);
		free(space);
		free(star);
	}
	int j = 2;
	for (j; j <= n; j++)
	{
		int numStar = 2 * (j - 1) + 1;
		char *star = (char *)calloc(numStar + 1, 1);
		char *space = (char *)calloc(n-j + 1, 1);
		char *lineStar = (char *)memset(star, cStar, numStar);
		char *lineSpace = (char *)memset(space, ' ', n - j);
		printf("%s%s\n", space, lineStar);
		free(space);
		free(star);
	}
	printf("%d\n", test - (2 * n * n - 1));
	getchar();
	return 0;
}

  

 

转载于:https://www.cnblogs.com/dingblog/p/4422369.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值