#A1031 Hello World for U (20)(20 分)

Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, "helloworld" can be printed as:

h  d
e l
l r
lowo

That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n1 = n3 = max { k| k ⇐ n2 for all 3 ⇐ n2 ⇐ N } with n1

  • n2 + n3 - 2 = N.

Input Specification:

Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.

Output Specification:

For each test case, print the input string in the shape of U as specified in the description.

Sample Input:

helloworld!

Sample Output:

h   !
e d
l l
lowor

思路

第二种方法,获取上界,编程就是解放人脑啊。

##AC代码

#include <stdio.h>
#include <string.h>
int main() {
char str[100], ans[40][40];//这里二维数组开到30也可以的吧
gets(str);//读入一行字符串
int N = strlen(str);//获取字符串的长度
int n1 = (N + 2) / 3, n3 = n1, n2 = N + 2 - n1 - n3;//自动向下取整
for(int i = 1; i <= n1; i++) {
for(int j = 1; j <= n2; j++) {
ans[i][j] = ' ';//初始化,将ans数组全部赋值为空格
}//第i行第j列
}
int pos = 0;
for(int i = 1; i <= n1; i++) {
ans[i][1] = str[pos++];
}
for(int j = 2; j <= n2; j++) {
ans[n1][j] = str[pos++];
}
for(int i = n3 - 1; i >= 1; i--) {
ans[i][n2] = str[pos++];
}
for(int i = 1; i <= n1; i++) {
for(int j = 1; j <= n2; j++) {
printf("%c", ans[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
gets(str);
int N = strlen(str);
int n1 = (N + 2) / 3, n3 = n1, n2 = N + 2 - n1 - n3;
for(int i = 0; i < n1 - 1; i++) {//前n1-1行
printf("%c", str[i]);
for(int j = 0; j < n2 - 2; j++) {
printf(" ");//n2-2个空格
}
printf("%c\n", str[N - 1-i]);
}
for(int i = 0; i < n2; i++) {
printf("%c", str[n1 + i - 1]);
}
return 0;
}

A1031 Hello World for U (20)(20 分)的更多相关文章

  1. MVC4 学习笔记 之 URL中存在编译的空格 20%20%

    /Config/Edit/QQCC%20%20%20%20%20%20%20 原因是: 通过EF直接添加了空格? NO 是因为你的数据库字段设计问题,因为你当然设计如>:sID nchar(10 ...

  2. 2016年11月29日 星期二 --出埃及记 Exodus 20:20

    2016年11月29日 星期二 --出埃及记 Exodus 20:20 Moses said to the people, "Do not be afraid. God has come t ...

  3. 安装nginx环境(含lua)时遇到报错ngx_http_lua_common.h:20:20: error: luajit.h: No such file or directory的解决

    下面是安装nginx+lua环境时使用的相关模块及版本,ngx_devel_kit和lua-nginx-module模块用的都是github上最新的模块.并进行了LuaJIT的安装. #Install ...

  4. 做数据挖掘,就算发 20 几分的 CNS 子刊,也是垃圾!?--转载

    关于数据挖掘发表文章,我们知道很多人是看不上.瞧不起.嗤之以鼻的.大抵是因为这些人平时只发 CNS 主刊,所以才认为通过数据挖掘这种用「别人的数据」或者叫「干实验」来发文章是“「垃圾」,没有什么价值. ...

  5. PAT A1031 Hello World for U (20)

    思路: 读取数组 int i = 0; while(cin >> word) { c[i] = word; i++; } 计算边长 int n1 = (length + 2) / 3; i ...

  6. [C++]PAT乙级1012.数字分类 (20/20)

    /* 1012. 数字分类 (20) 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和, ...

  7. [C++]PAT乙级1008.数组元素循环右移问题 (20/20)

    /* 1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数, ...

  8. [C++]PAT乙级1007.素数对猜想 (20/20)

    /* 1007. 素数对猜想 (20) 让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数.“素数对猜想”认为“存在无穷 ...

  9. [C++]PAT乙级1004. 成绩排名 (20/20)

    /* 1004. 成绩排名 (20) 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生 ...

  10. [C++]PAT乙级1002.写出这个数(20/20)

    /* 1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10^100. ...

随机推荐

  1. jackson简单使用案例

    新建Maven项目导入依赖包:jakson <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&quo ...

  2. SharePoint 2010开发方面的课堂中整理有关问题

    SharePoint 2010开发方面的课堂中整理有关问题 这是我这几天在做一个SharePoint开发的课程的时候,大家提出的一些问题,及我的解答,分享给更多的朋友参考一下 这个文档,也可以在这里下 ...

  3. jquery.validate+jquery.form表单验证提交

    1.通过jquery.validate的submitHandler选项,即当表单通过验证时运行回调函数.在这个回调函数中通过jquery.form来提交表单: <script type=&quo ...

  4. webpack 3 优化

    编译时间太长 项目为多页面应用时,编译的时候每个入口都会读取依赖的路径,所以入口越多,会导致编译越慢 公用库提取 除了公用的框架(如 Vue.React)以外,不同页面所需要的第三方库可能不一样,而且 ...

  5. chroot 的应用

    http://www.williamlong.info/archives/3864.html http://my.oschina.net/u/1590519/blog/342576

  6. Android入门:封装一个HTTP请求的辅助类

    前面的文章中,我们曾经实现了一个HTTP的GET 和 POST 请求: 此处我封装了一个HTTP的get和post的辅助类,能够更好的使用: 类名:HttpRequestUtil 提供了如下功能: ( ...

  7. ABAP:从例子学习ABAP

    1.插入内表行: *插入内表行: DATA: BEGIN OF man, name(20) TYPE c, high TYPE p DECIMALS 2, weight TYPE p DECIMALS ...

  8. js或者jq判断一段文字中是否有自己想要的那几个字,如果有就把那几个字变成红色

    如何判断一段文字中是否有自己想要的那几个字,如果有就把那几个字变成红色(在html中)比如有body中有这么一串文字“驾驶的后视镜的华盛顿”,想要判断里面是否有“驾驶”这两个字,如果有就把这两个字变成 ...

  9. python3应用例子01(进度条)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import sysimport time def bar(num, total): rate = num / ...

  10. c++ 输入split

    日期格式为“yyyy/mm/dd”(即年/月/日)格式 scanf("%d/%d/%d", &year, &month, &day);