#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main(){
int cakes[50];
memset(cakes,0,sizeof(cakes));
char input[1000];
memset(input,'\0',sizeof(input));
while(fgets(input,sizeof(input),stdin)!=NULL){ //讀取一整行
int cake_num = 0; //蛋糕數 初始化
for(int i=0;i<sizeof(input);i++){ //將input列中的個別數字轉換為整數存入cakes
if(input[i]>='0' && input[i]<='9'){
int tmp = input[i]-'0';
cakes[cake_num] = cakes[cake_num]*10;
cakes[cake_num] += tmp;
}
if(input[i]==' ')cake_num++; //遇到空格,蛋糕數++
}
for(int i=0;i<=cake_num;i++)printf("%d ",cakes[i]);
printf("\n");
int n = cake_num; // n為計算還沒處理的cakes,在n以下的cakes都已經完成排序
while(n){
int max = 0;
int max_flag=0; //最大值旗標
for(int i=0;i<=n;i++){
if(cakes[i] >= max){ //找出最大值其位置(flag)
max = cakes[i];
max_flag = i;
}
}
if(max_flag==n){ //如果最底下的就是最大值,則查找範圍往上縮減
n--; //縮減
max = 0;
max_flag = 0;
}else if(max_flag==0 || cakes[0]==cakes[max_flag]){ //如果最大值在最上面或最大值和最上面相等,則直接全部翻轉
int top=0, buttom=n;
while(1){
if(top >= buttom)break;
int tmp = cakes[top]; //交換Pie
cakes[top] = cakes[buttom];
cakes[buttom] = tmp;
top++; buttom--;
}
printf("%d ",cake_num-n+1);
n--; //最大值已翻轉到最下面,往上縮減範圍
}else{
int top=0, buttom=max_flag; //將最大值以上的翻轉
while(1){
if(top >= buttom)break;
int tmp = cakes[top]; //交換Pie
cakes[top] = cakes[buttom];
cakes[buttom] = tmp;
top++; buttom--;
}
printf("%d ",cake_num-max_flag+1);
//n--; //縮減
}
}
printf("0\n");
// for(int i=0;i<=cake_num;i++)printf("%d ",cakes[i]);
// printf("\n");
memset(cakes,0,sizeof(cakes));
memset(input,'\0',sizeof(input));
}
return 0;
}
2014年11月24日星期一
[UVa] 120 - Stacks of Flapjacks
注意輸出的方式,因為是由底下輸出,要注意變數的換算。
2014年11月20日星期四
[UVa] 299 - Train Swapping
只能左右交換的排序
以陣列來看,在第 i 個位置,向右找到和自己位置相同的數字
一直向左交換回來即可
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int train[70]; //train carriages
int main(){
memset(train,0,sizeof(train));
int n; //cases
scanf("%d",&n);
while(n--){
int L; //length of traiin
int count=0; //swap count
scanf("%d",&L);
for(int i=0;i<L;i++)scanf("%d",&train[i]);
for(int i=0;i<L;i++){
for(int j=i;j<L;j++){
if(train[i] == i+1)break;
if(train[j] == i+1){ //左右交換
int tmp = train[j];
train[j] = train[j-1];
train[j-1] = tmp;
j=i;
count++;
}
}
}
//for(int i=0;i<L;i++)printf("%d ",train[i]);
//printf("\n");
printf("Optimal train swapping takes %d swaps.\n",count);
}
return 0;
}
[Note] Ubuntu 磁碟清理
這邊引用 Tsung's Blog 的貼文
http://blog.longwin.com.tw/2011/02/linux-free-hd-clean-2011/
另外還有 Rex's blah blah blah 關於清理儲存空間的貼文
內有詳盡的介紹
http://blog.longwin.com.tw/2011/02/linux-free-hd-clean-2011/
- 清空 Trash bin
- apt-get clean # 清除 local repository 淘汰得 Package (deb)
- apt-get autoclean # 清除舊版本的 暫存 Package (deb)
- apt-get autoremove # 刪除系統不再使用的 Package
- /var/cache/apt/archives # Package (deb) cache
- /var/cache/apt/archives/partial # 沒有下載完成的 Package 放在這邊
- ~/.mozilla/firefox/*.default/Cache # 若已經指定進 Ram Disk, 就不用管這個.
- /var/log/*
- /tmp/*
另外還有 Rex's blah blah blah 關於清理儲存空間的貼文
內有詳盡的介紹
2014年11月19日星期三
[UVA] 152 - Tree's a Crowd
運用一個新的函數 pow ,可取回傳入數值的某次方
輸入輸出皆為double
輸入輸出皆為double
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
//int trees[6000][3]; //6000 trees, 3-dimensional
int x[6000],y[6000],z[6000];
int hist[10];
int main(){
freopen("input.txt", "r", stdin);
memset(hist,0,sizeof(hist));
double input_x, input_y, input_z;
int t=0;
while(scanf("%d %d %d", &x[t], &y[t], &z[t])!=EOF){
if(x[t]==0 && y[t]==0 && z[t]==0)break;
t++; // next tree
}
for(int i=0;i<t;i++){
double min_d=10000;
for(int j=0;j<t;j++){
if(i==j)continue;
double d=sqrt(pow(x[i]-x[j],2.0)+pow(y[i]-y[j],2.0)+pow(z[i]-z[j],2.0));
//sqrt(dx^2+dy^2+dz^2)
//printf("%.2f\n",d);
if(d<=min_d){
min_d = d;
}
}
if(min_d==10000)break;
//printf("%.2f\n",min_d);
if(min_d<1 && min_d>=0)hist[0]++;
else if(min_d>=1 && min_d<2)hist[1]++;
else if(min_d>=2 && min_d<3)hist[2]++;
else if(min_d>=3 && min_d<4)hist[3]++;
else if(min_d>=4 && min_d<5)hist[4]++;
else if(min_d>=5 && min_d<6)hist[5]++;
else if(min_d>=6 && min_d<7)hist[6]++;
else if(min_d>=7 && min_d<8)hist[7]++;
else if(min_d>=8 && min_d<9)hist[8]++;
else if(min_d>=9 && min_d<10)hist[9]++;
else;
}
for(int i=0;i<10;i++) printf("%4d",hist[i]);
printf("\n");
return 0;
}
訂閱:
文章 (Atom)
-
因為先前寫UVa時,檔案名稱有時會花心思改,有時就直接把題目名稱加上.cpp就貼上了 導致現在有不同的格式出現 現在要處理的事情很簡單 1. 去除空白 2. 將底線 ( _ ) 換成dash ( - ) 經過一番查詢,終於發現最簡單的方法 - re...
-
文章出處: http://infbugs.blogspot.tw/2011/12/c_20.html 謝謝沙耶,解答了我長久以來對於 input/output 的疑惑。 C 語言入門 - 在線上批改系統練功 如何練習使用基本語法 自己出個練習題試著寫...
-
一開始用數學方法推斷得出,設輸入為n k為在n的前一斜線列數,故只要找到 (1+k)*k/2 < n 的最大k值,即可判定 k%2 == 1 => ((2*n)-(k*k)-k)/2 / ((k*k)+(3*k)-(2*n)+4)/2 k%2 ==...