We have a group of N items (represented by integers from 1 to N), and we know that there is some total order defined for these items. You may assume that no two elements will be equal (for all a, b: a<b or b<a). However, it is expensive to compare two items. Your task is to make a number of comparisons, and then output the sorted order. The cost of determining if a < b is given by the bth integer of element a of costs (space delimited), which is the same as the ath integer of element b. Naturally, you will be judged on the total cost of the comparisons you make before outputting the sorted order. If your order is incorrect, you will receive a 0. Otherwise, your score will be opt/cost, where opt is the best cost anyone has achieved and cost is the total cost of the comparisons you make (so your score for a test case will be between 0 and 1). Your score for the problem will simply be the sum of your scores for the individual test cases.
標簽:
represented
integers
group
items
上傳時間:
2016-01-17
上傳用戶:jeffery
#include <stdlib.h>
#include<stdio.h>
#include <malloc.h>
#define stack_init_size 100
#define stackincrement 10
typedef struct sqstack
{
int *base;
int *top;
int stacksize;
} sqstack;
int StackInit(sqstack *s)
{
s->base=(int *)malloc(stack_init_size *sizeof(int));
if(!s->base)
return 0;
s->top=s->base;
s->stacksize=stack_init_size;
return 1;
}
int Push(sqstack *s,int e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(int *)realloc(s->base,(s->stacksize+stackincrement)*sizeof(int)); if(!s->base)
return 0;
s->top=s->base+s->stacksize;
s->stacksize+=stackincrement;
}
*(s->top++)=e;
return e;
}
int Pop(sqstack *s,int e)
{
if(s->top==s->base)
return 0;
e=*--s->top;
return e;
}
int stackempty(sqstack *s)
{
if(s->top==s->base)
{
return 1;
}
else
{
return 0;
}
}
int conversion(sqstack *s)
{
int n,e=0,flag=0;
printf("輸入要轉(zhuǎn)化的十進制數(shù):\n");
scanf("%d",&n);
printf("要轉(zhuǎn)化為多少進制:\n"); scanf("%d",&flag);
printf("將十進制數(shù)%d 轉(zhuǎn)化為%d 進制是:\n",n,flag);
while(n)
{
Push(s,n%flag);
n=n/flag;
}
while(!stackempty(s))
{
e=Pop(s,e);
switch(e)
{
case 10: printf("A");
break;
case 11: printf("B");
break;
case 12: printf("C"); break;
case 13: printf("D"); break;
case 14: printf("E"); break;
case 15: printf("F"); break;
default: printf("%d",e); }
}
printf("\n");
return 0;
}
int main()
{
sqstack s;
StackInit(&s);
conversion(&s);
return 0;
}
標簽:
整數(shù)
棧
基本操作
十進制
轉(zhuǎn)化
進制
上傳時間:
2016-12-08
上傳用戶:愛你198