PAT B1027 打印沙漏(20)
思路:
- 使用数组保存每一行沙漏的最大符号数
- 输入一个正整数和一个符号
- 遍历数组,找到大于正整数的数组下标 j。
- 三角形底边的字符数为 (j - 1) * 2 - 1
- 打印沙漏
- 打印剩余字符:x - n[j - 1]
AC代码
#include <cstdio>
const int max_n = 200;
int n[max_n] = {0};
int i = 3;
void init() {
n[1] = 1;
n[2] = 7;
int sum = 0;
while(n[i-1] < 1000) {
int j = (i * 2 - 1) * 2;
n[i] = n[i - 1] + j;
i++;
}
}
int main() {
init();
int x;
char y;
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
scanf("%d %c", &x, &y); //x:正整数 y:符号
// int i = 1;
// while(n[i] != 0) {
// printf("%d:---%d---\n", i,n[i]);
// ++i;
// }
// printf("*********%d*************\n", i);
int j = 1;
while(n[j] <= x) {
j++;
}
int bottom = (j - 1)*2-1; //三角形底边的字符数
//int space = (bottom+1)*(bottom-1)/2-1;
// printf("%d\n", small);
//打印沙漏
//倒三角
// printf("bottom: %d\n", bottom);
for(int i = bottom; i >= 1; i-=2) {
for(int j = 0; j < (bottom - i) / 2; j++) {
printf(" ");
}
for(int j = 0; j < i; j++) {
printf("%c", y);
}
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", y);
}
printf("\n");
}
printf("%d\n", x - n[j-1]);
return 0;
}
PAT B1027 打印沙漏(20)的更多相关文章
- PAT 1027 打印沙漏(20)(思路)
1027 打印沙漏(20)(20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** ***** 所谓& ...
- PAT B1027 打印沙漏 (20 分)
本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号:各行符号中心对齐:相邻两 ...
- PAT 1027. 打印沙漏(20)
本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** ***** 所谓"沙漏形状",是指每行 ...
- PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)
PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20) http://www.patest.cn/contests/pat-b-practise/1027 本题 ...
- PAT乙级 1027. 打印沙漏(20)
1027. 打印沙漏(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打印成 ...
- PAT (Basic Level) Practise (中文)1027. 打印沙漏(20)
1027. 打印沙漏(20) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打印成 ...
- PAT 乙级 1027 打印沙漏(20) C++版
1027. 打印沙漏(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打印成 ...
- PAT-乙级-1027. 打印沙漏(20)
1027. 打印沙漏(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打印成 ...
- pat00-自测1. 打印沙漏(20)
00-自测1. 打印沙漏(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打 ...
随机推荐
- 第02组团队Git现场编程实战
GitHub仓库地址 click here 1.组员职责分工 组员 职责分工 黄智.赵镇 百度地图API使用 潘松波.颜志鹏 写分别测评福州人均消费50以下,50-100.100-200.200以上最 ...
- JavaWeb_(Spring框架)Spring整合Hibernate
Dao层类要继承HibernateDaoSupport.java父类 原先使用Hibernate框架hibernate.cfg.xml配置数据库 <hibernate-configuration ...
- Django环境搭建win(二)
Django 1.11.x 支持 Python 2.7, 3.4, 3.5 和 3.6,17年4月4号 已经发布(长期支持版本 LTS) win7下: 一.使用pip安装 1.首先要安装pip 2.p ...
- python性能测试值timeit的使用示例
from timeit import Timer def t1(): li = [] for i in range(10000): li.append(i) def t2(): li = [] for ...
- 解决虚拟机上的tomcat无法被主机访问的问题
在wmware中安装linux后安装好数据库,JDK及tomcat后启动服务,虚拟机中可以访问,但是主机却无法访问,但是同时主机和虚拟机之间可以ping的通. 网上查阅资料后 第一种解决方法是 ...
- swoole入门简介
原文:https://www.cnblogs.com/dormscript/p/4811921.html 本文主要记录一下学习swoole的过程.填过的坑以及swoole究竟有多么强大! 首先说一下对 ...
- ansible的剧本
ansible的playbook的介绍-yaml ansible的playbook是使用yaml语言写的 YAML标记语言介绍YAML是一个可读性高的用来表达资料序列的格式.YAML参考了其他多种语言 ...
- POJ 1837 -- Balance(DP)
POJ 1837 -- Balance 转载:優YoU http://user.qzone.qq.com/289065406/blog/1299341345 提示:动态规划,01背包 初看此题第 ...
- SpringCloud(三)之Feign实现负载均衡的使用
一 点睛 Feign是Netflix开发的声明式.模板化的HTTP客户端, Feign可以帮助我们更快捷.优雅地调用HTTP API. 在Spring Cloud中,使用Feign非常简单——创建一个 ...
- .net 分布式锁实现
摘要: .net分布式锁,包括redis分布式锁和zookeeper分布式锁的.net实现. 分布式锁在解决分布式环境下的业务一致性是非常有用的. 分布式锁 经常用于在解决分布式环境下的业务一致性和协 ...