《算法笔记》3.3 codeup编号:100000577

  • Problem A
问题

输出梯形

描述输入一个高度h,输出一个高为h,上底边为h的梯形。
输入一个整数h(1<=h<=1000)。
输出h所对应的梯形。

#include<stdio.h>

int main(){
	int h;
	while(scanf("%d",&h)!=EOF){
		for(int i=0;i<h;i++){//高 
			for(int j=2;j<2*(h-i);j++){
				printf(" ");
			}
			for(int j=0;j<h+2*i;j++){
				printf("*"); 
			}
			printf("\n");
		}
	}
	return 0;
} 
  • Problem B
问题

Hello World for U

描述

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.

输入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.
输出For each test case, print the input string in the shape of U as specified in the description.

#include<stdio.h>
#include<string.h>

int main(){
	char words[81];
	int side;
	int mid;
	int i;
	while(scanf("%s",words)!=EOF){
		side=(strlen(words)+2)/3;
		mid=strlen(words)-2*side;
		for(i=0;i<side-1;i++){
			printf("%c",words[i]);
			for(int j=0;j<mid;j++){
				printf(" ");
			}
			printf("%c",words[strlen(words)-1-i]);
			printf("\n");
		}
		for(int j=0;j<mid+2;j++) {
			printf("%c",words[i]);
			i++;
		}
		printf("\n");
	}
	return 0;
}
  • Problem C
问题

等腰梯形

描述

请输入高度h,输入一个高为h,上底边长为h 的等腰梯形(例如h=4,图形如下)。

   ****

  ******

 ********

**********

输入输入第一行表示样例数m,接下来m行每行一个整数h,h不超过10。
输出对应于m个case输出要求的等腰梯形。

#include<stdio.h>

int main(){
	int T;
	int h;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&h);
		for(int i=0;i<h;i++){//高 
			for(int j=1;j<(h-i);j++){
				printf(" ");
			}
			for(int j=0;j<h+2*i;j++){
				printf("*"); 
			}
			printf("\n");
		}
	}
	return 0;
} 
  • Problem D

写的有点复杂了,感觉这个大佬写的比较简洁明了。

https://blog.csdn.net/privilage/article/details/79939943?ops_request_misc=&request_id=&biz_id=102&utm_term=%E9%97%AE%E9%A2%98%20D:%20%E6%B2%99%E6%BC%8F%E5%9B%BE%E5%BD%A2%20tri2str%20%5B1*+%5D&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-8-.pc_search_result_before_js&spm=1018.2226.3001.4187

问题

沙漏图形 tri2str [1*+]

描述输入n,输出正倒n层星号三角形。首行顶格,星号间有一空格,效果见样例
输入输入样例:
3
输出
* * *
 * * 
  *
 * * 
* * *
#include<stdio.h>

int main(){
	int h,i;
	int flag=0;
	while(scanf("%d",&h)!=EOF){
		i=0;
		for(;i<(2*h-1)/2;i++){
			for(int j=0;j<2*h-i-1;j++){
				if(flag==0&&j>=i){
					printf("*");
					flag++;
				}
				else{
					printf(" ");
					flag=0;
				}
			}
			printf("\n");
		}
		for(int j=0;j<i;j++)printf(" ");
		printf("*\n");
		i++;
		for(;i<2*h-1;i++){
			flag=0;
			for(int j=0;j<2*h-(2*((2*h-1)/2)-i)-1;j++){
				if(flag==0&&j>=(2*((2*h-1)/2)-i)){
					printf("*");
					flag++;
				}
				else{
					printf(" ");
					flag=0;
				}			
			}
			printf("\n");	
		}
		
		
	}
	return 0;
}

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值