#6628: 為何一直愈時


edwardlo12 (2)

學校 : 臺中市私立立人高級中學
編號 : 23438
來源 : [140.134.18.45]
最後登入時間 :
2015-05-08 11:26:51
d872. 過橋問題 -- UVa10037簡易版 | From: [210.60.247.254] | 發表日期 : 2012-05-14 12:38

#include <stdio.h>
#include "time.h" 
#include <stdlib.h>
#define SWAP(x,y,t)  (t=x,x=y,y=t)

int sort(int [],int);
int main(int argc, char *argv[])
{
  int a;
  while(scanf("%d",&a)!=EOF){
    int str1[a];
    int str2[a-2];
    int i,b,c,d,j,e;
    b=0;
    e=0;
    d=0;
    for(i=0;i<a;i++){
      scanf("%d",&str1[i]); 
    }
    sort(str1,a);
    for(i=a-2;i>1;i-=2)
    {
     e=str1[0]+2*str1[1]+str1[i+1];
     d=2*str1[0]+str1[i]+str1[i+1]; 
         
    if(d<e)
    {
      e=d;
      printf("%d\n",e);     
    }
    b=b+e;
    printf("%d\n",b);
    }
    c=(a-2)%2;
    if(c==1)
    {
      b=b+str1[0]+str1[1]+str1[2];       
    }
    else
    {
    b=b+str1[1];   
    }              
  printf("%d\n",b);   
}
 
  return 0;
}
int sort(int str1[],int n)
{
  int i,j,temp,min;
  for(i=0;i<n-1;i++)
  {
   
    min=i;
    for(j=i+1;j<n;j++)
     if(str1[j]<str1[min])
      min=j;
    SWAP(str1[i],str1[min],temp);                   
  }    
}

 
#6638: Re:為何一直愈時


kkoo7788 (白鹿洞-lufy)

學校 : 不指定學校
編號 : 19326
來源 : [211.72.124.239]
最後登入時間 :
2019-05-04 16:30:18
d872. 過橋問題 -- UVa10037簡易版 | From: [114.42.208.158] | 發表日期 : 2012-05-17 22:57

#include
#include "time.h" 
#include
#define SWAP(x,y,t)  (t=x,x=y,y=t)

int sort(int [],int);
int main(int argc, char *argv[])
{
  int a;
  while(scanf("%d",&a)!=EOF){
    int str1[a];
    int str2[a-2];
    int i,b,c,d,j,e;
    b=0;
    e=0;
    d=0;
    for(i=0;i      scanf("%d",&str1[i]); 
    }
    sort(str1,a);
    for(i=a-2;i>1;i-=2)
    {
     e=str1[0]+2*str1[1]+str1[i+1];
     d=2*str1[0]+str1[i]+str1[i+1]; 
         
    if(d    {
      e=d;
      printf("%d\n",e);     
    }
    b=b+e;
    printf("%d\n",b);
    }
    c=(a-2)%2;
    if(c==1)
    {
      b=b+str1[0]+str1[1]+str1[2];       
    }
    else
    {
    b=b+str1[1];   
    }              
  printf("%d\n",b);   
}
 
  return 0;
}
int sort(int str1[],int n)
{
  int i,j,temp,min;
  for(i=0;i  {
   
    min=i;
    for(j=i+1;j     if(str1[j]      min=j;
    SWAP(str1[i],str1[min],temp);                   
  }    
}

因為氣泡排序法太慢
 
ZeroJudge Forum