Skip to content

Instantly share code, notes, and snippets.

@trylovetom
Created October 13, 2015 08:17
Show Gist options
  • Save trylovetom/93127097b5b0a0756999 to your computer and use it in GitHub Desktop.
Save trylovetom/93127097b5b0a0756999 to your computer and use it in GitHub Desktop.
//
// main.c
// SkyLine
//
// Created by ChangTom on 2015/10/5.
// Copyright © 2015年 ChangTom. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#define MAX_X 30
#define MAX_Y 20
#define BUILDING_NUM 8
typedef struct Building
{
int start;
int height;
int weight;
int end;
} Building;
Building* bulidingMaker( int start, int height, int end )
{
Building *newBuildeing = (Building *)malloc( sizeof( Building ) );
newBuildeing->start = start;
newBuildeing->height = height;
newBuildeing->weight = end - start;
newBuildeing->end = end;
return newBuildeing;
}
void printMap( int map[ MAX_X ] )
{
// print
for ( int posY = MAX_Y - 1; posY >= 0; --posY )
{
printf( "%d\t", posY );
for ( int posX = 0; posX < MAX_X; ++posX )
{
if ( posY <= map[ posX ] )
{
printf( "*" );
}
else
{
printf( " " );
}
}
printf( "\n" );
}
}
int main( int argc, const char * argv[] )
{
// Initial the data.
// (1; 11; 5) (2; 6; 7) (3; 13; 9) (12; 7; 16) (14; 3; 25) (19; 18; 22) (23; 13; 29) (24; 4; 28)
Building *b0 = bulidingMaker( 1, 11, 5 );
Building *b1 = bulidingMaker( 2, 6, 7 );
Building *b2 = bulidingMaker( 3, 13, 9 );
Building *b3 = bulidingMaker( 12, 7, 16 );
Building *b4 = bulidingMaker( 14, 3, 25 );
Building *b5 = bulidingMaker( 19, 18, 22 );
Building *b6 = bulidingMaker( 23, 13, 29 );
Building *b7 = bulidingMaker( 24, 4, 28 );
Building *buildings[ BUILDING_NUM ] = { b0, b1, b2, b3, b4, b5, b6, b7 };
// 用一個一維陣列儲存高度資料,每個X位置對應一個高度值,初始值為0。
int map[ MAX_X ] = { 0 };
// 用X軸做索引,從0開始搜尋。
for ( int posX = 0; posX < MAX_X; ++posX )
{
// 從房子做索引,從0開始搜尋。
for ( int counter = 0; counter < BUILDING_NUM; ++counter )
{
// 判斷X座標是否介於當前房子的初始位置與結束位置的區間。
if ( posX >= buildings[ counter ]->start && posX <= buildings[ counter ]->end )
{
// 判斷當前房子高度是否大於陣列對應的高度值。
if ( map[ posX ] < buildings[ counter ]->height )
{
// 替換為當前房子高度。
map[ posX ] = buildings[ counter ]->height;
}
}
}
}
printMap( map );
// Free the memory.
for ( int counter = 0; counter < BUILDING_NUM; ++counter )
{
free( buildings[ counter ] );
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment