Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // Billboard
- //
- // Created by Sidharth Juyal on 21/01/12.
- // Copyright (c) 2012 whackylabs. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define T_MAX 1000
- #define DEBUG_OUT 0
- void process(int caseno, int w, int h, size_t *wordlens,int wc);
- int getlines(size_t cpl, size_t *wordlens, int wc);
- int main (int argc, const char * argv[]){
- int T, W, H;
- int ch, wc;
- char word[T_MAX];
- char *wptr = word;
- size_t wordlens[T_MAX];
- freopen(argv[1], "r", stdin);
- T = -1;
- while((ch = getchar())){
- if(ch == '\n'){
- if(T < 0){
- T = 1;
- }else{
- *wptr = '\0';
- wordlens[wc++] = strlen(word);
- process(T, W, H, wordlens, wc);
- T++;
- }
- wc = 0;
- W = H = -1;
- wptr = word;
- }else if(ch == ' '){
- *wptr = '\0';
- if(W < 0)
- W = atoi(word);
- else if(H < 0)
- H = atoi(word);
- else
- wordlens[wc++] = strlen(word);
- wptr = word;
- }else if(ch == EOF){
- *wptr = '\0';
- wordlens[wc++] = strlen(word);
- process(T, W, H, wordlens,wc);
- break;
- }else{
- *wptr++ = ch;
- }
- }
- return 0;
- }
- void process(int caseno, int w, int h, size_t *wordlens,int wc){
- size_t font;
- size_t cpl; //chars per line
- int i;
- cpl = wordlens[0];
- for(i = 1; i < wc; i++){
- if(wordlens[i] > cpl)
- cpl = wordlens[i];
- }
- font = w/cpl;
- if(DEBUG_OUT) printf("font: %lu cpl: %lu\n",font,cpl);
- while(getlines(cpl, wordlens, wc)*font > h){
- font--;
- cpl = w/font;
- if(DEBUG_OUT) printf("font: %lu cpl: %lu\n",font,cpl);
- }
- printf("Case #%d: %lu\n",caseno, font);
- if(DEBUG_OUT) printf("{%d, %d} %d\n\n",w, h, wc);
- }
- int getlines(size_t cpl, size_t *wordlens, int wc){
- int i;
- int lines = 1;
- size_t used_chars = 0;
- for(i = 0; i < wc;){
- used_chars += wordlens[i];
- if(used_chars < cpl){
- if(DEBUG_OUT) printf("[%lu]_",wordlens[i]);
- i++;
- used_chars++;
- }else if(used_chars == cpl){
- if(DEBUG_OUT) printf("[%lu]\n",wordlens[i]);
- lines++;
- used_chars = 0;
- i++;
- }else{
- if(DEBUG_OUT) printf("\n");
- lines++;
- used_chars = 0;
- }
- }
- if(DEBUG_OUT)printf("\nlines: %d\n",lines);
- return lines;
- }
Add Comment
Please, Sign In to add comment