2008年12月29日 星期一
2008年12月27日 星期六
371 - Ackermann Functions ( Time limit exceeded )
Ackermann Functions
| Ackermann Functions |
An Ackermann function has the characteristic that the length of the sequence of numbers generated by the function cannot be computed directly from the input value. One particular integer Ackermann function is the following:
This Ackermann has the characteristic that it eventually converges on 1. A few examples follow in which the starting value is shown in square brackets followed by the sequence of values that are generated, followed by the length of the sequence in curly braces:
[10] 5 16 8 4 2 1 {6}
[13] 40 20 10 5 16 8 4 2 1 {9}
[14] 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 {17}
[19] 58 29 88 44 22 ... 2 1 {20}
[32] 16 8 4 2 1 {5}
[1] 4 2 1 {3}
Input and Output
Your program is to read in a series of pairs of values that represent the first and last numbers in a closed sequence. For each closed sequence pair determine which value generates the longest series of values before it converges to 1. The largest value in the sequence will not be larger than can be accomodated in a 32-bit Pascal LongInt or C long. The last pair of values will be 0, 0. The output from your program should be as follows:
Between L and H, V generates the longest sequence of S values.
Where:
L = the lower boundary value in the sequence
H = the upper boundary value in the sequence
V = the first value that generates the longest sequence, (if two or more values generate the longest sequence then only show the lower value) S = the length of the generated sequence.
In the event that two numbers in the interval should both produce equally long sequences, report the first.
Sample Input
1 20
35 55
0 0
Sample Output
Between 1 and 20, 18 generates the longest sequence of 20 values.=======================================================
Between 35 and 55, 54 generates the longest sequence of 112 values.
#include <stdio.h>
#include <stdlib.h>
/* function prototype */
void Ackermann(const int,const int);
int find_length(const int);
int main()
{
freopen("Input.txt","r",stdin);
freopen("Output.txt","w",stdout);
int L,H;
while(scanf("%d%d",&L,&H)==2)
if(L|H)
Ackermann(L,H);
return 0;
}
/* scan from lower bound to upper bound */
void Ackermann(const int L,const int H)
{
int i,max_len=L,max_num=find_length(L);
for(i=L+1;i<=H;i++)
{
int temp=find_length(i);
if(max_num<temp)
{
max_len=i;
max_num=temp;
}
}
printf("Between %d and %d, %d generates the longest sequence of %d values.\n",L,H,max_len,max_num);
}
/* return the sequence number of value "V" */
int find_length(const int V)
{
int count=0,x=V;
do
{
count++;
x=(!x%2)?(x/2):(3*x+1);
}while(x!=1);
return count;
}
靈犬尋寶
正方形(左下角座標為(0,0),右上角座標為(99,99))的格網上,有一隻靈犬要尋找一個寶物,格網上除了靈犬與寶物之外,還有一些障礙物。一般情況下,只要不超出格網的邊界,靈犬的每一步最多有8 個方向可供選擇,如圖一;

但是必須注意,只有在A 點沒有障礙物時才可以選擇方向1 或方向2,只有在B點沒有障礙物時才可以選擇方向3 或方向4,只有在C 點沒有障礙物時才可以選擇方向5 或方向6,只有在D 點沒有障礙物時才可以選擇方向7 或方向8。如果靈犬可以從出發的位置走到寶物的位置,其總共使用的步數,理論上應有一個最小值;但是靈犬也有可能無法走到寶物的位置。過程中,靈犬不可以
走到障礙物的位置上。

以圖二為例,有多達4 個障礙物介於靈犬與寶物之間,但是靈犬最快只要2步就可以到達寶物的位置。圖三是另一個例子,靈犬的位置靠近角落,雖然只有2 個障礙物,靈犬卻無法到達寶物的位置。請撰寫一個程式,若靈犬可以從最初位置走到寶物的位置時,請列印出其使用之最少步數;若靈犬無法到達寶物的位置,請列印出單字『impossible』。
輸入說明:
第一行為一個整數n,代表障礙物的個數,0 ≦ n ≦ 1000。接下來的n 行,每行表示一個障礙物的座標,其橫座標值與縱座標值間以一個空白隔開。再下來的一行,表示靈犬的最初位置,其橫座標值與縱座標值間以一個空白隔開。最後一行,代表寶物的位置,其橫座標值與縱座標值間以一個空白隔開。注意:輸入之障礙物、靈犬、寶物皆在不同位置。所有橫、縱座標值均為介於0(含)至99(含)之間的整數。
輸出說明:
依行走之規則,若靈犬可以從其位置走到寶物的位置時,請列印出其使用之最少步數;若靈犬無法到達寶物的位置,請列印出單字『impossible』。
輸入範例1:
4
3 6
4 5
5 4
6 3
3 3
7 5
輸出範例1:
2
輸入範例2:
2
1 1
0 2
0 1
4 3
輸出範例2:
impossible
====================================================
Note. 所求為最少步數,故不必記錄路徑,使用BFS一步步逼近寶物,也
不會有走到重複點卻需要覆蓋的問題,且由於水平或垂直的移動皆
為偶數的組合步,故每一步的上下左右都無法走到,頂多只會有棋
盤的一半被走到。
====================================================
#include <stdio.h>
#include <stdlib.h>
#define BOARD_SIZE 100
#define MAX_QUEUE BOARD_SIZE*BOARD_SIZE/2
/* global variables */
int board[BOARD_SIZE+6][BOARD_SIZE+6];
/* the direction index of the dog's moving directions */
int dir_ver[8]={1,-1,-3,-3,-1,1,3,3};
int dir_hor[8]={3,3,1,-1,-3,-3,-1,1};
/* the direction index of the obstacles */
int dir_block_ver[4]={0,-1,0,1};
int dir_block_hor[4]={1,0,-1,0};
/* data structure of the queue */
typedef struct Position
{
/* location */
int x;
int y;
/* number of foot steps */
int count;
}Position;
Position pos[MAX_QUEUE];
/* function prototype */
void initial(const int,const int,const int,const int [],const int []);
int find(const int,const int,const int,const int);
int main()
{
/* read and open the input file named "H04dat.txt" */
FILE *fp;
fp=fopen("H04dat.txt","r");
int i,ans,obstacle;
/* set up the blocks */
fscanf(fp,"%d",&obstacle);
int x[obstacle],y[obstacle];
for(i=0;i<obstacle;i++)
fscanf(fp,"%d%d",&x[i],&y[i]);
/* set up the start and end points */
int start_x,start_y,end_x,end_y;
fscanf(fp,"%d%d",&start_x,&start_y);
fscanf(fp,"%d%d",&end_x,&end_y);
/* intialize the board */
initial(start_x+3,start_y+3,obstacle,x,y);
/* if the treasure and the dog is on the same location */
if(start_x==end_x&&start_y==end_y)
printf("0\n");
else
/* find the treasure */
if((ans=find(start_x+3,start_y+3,end_x+3,end_y+3))>0)
printf("%d\n",ans);
else
/* if the treasure and the obstacle is on the same location */
printf("impossible\n");
/* close the input file */
fclose(fp);
system("PAUSE");
return 0;
}
/* initialize the board */
void initial(const int sx,const int sy,const int obstacle,const int x[],const int y[])
{
int i,j;
/* set the outside boundary to -1, others to 0 */
for(i=0;i<BOARD_SIZE+6;i++)
for(j=0;j<BOARD_SIZE+6;j++)
board[i][j]=(i>2&&i<BOARD_SIZE+3&&j>2&&j<BOARD_SIZE+3)?0:-1;
/* put down the blocks */
for(i=0;i<obstacle;i++)
board[y[i]+3][x[i]+3]=-1;
/* because it's meaningless that move to the start point */
board[sy][sx]=-1;
/* initialize the queue */
pos[0].x=sx;
pos[0].y=sy;
pos[0].count=0;
}
/* return the minimum steps if the dog can find the treasure, */
/* return 0 if false */
int find(const int sx,const int sy,const int ex,const int ey)
{
int i,j,idx;
/* the four directions of each foot step are unreachable */
if((ex+sx+ey+sy)%2)
return 0;
/* scan the whole queue */
for(i=0,idx=1;i<idx;i++)
{
/* if the dog has already found the treasure then return the steps */
/* use this condition may run more times "if(pos[i].y==ey&&pos[i].x==ex)" */
if(board[ey][ex]!=0)
return board[ey][ex];
/* using BFS the find out the next eight directions */
for(j=0;j<8;j++)
/* if the obstacle is not near the dos's present location */
if(board[pos[i].y+dir_block_ver[j/2]][pos[i].x+dir_block_hor[j/2]]!=-1)
/* if any direction is reachable then PUSH into the queue */
if(!board[pos[i].y+dir_ver[j]][pos[i].x+dir_hor[j]])
{
pos[idx].x=pos[i].x+dir_hor[j];
pos[idx].y=pos[i].y+dir_ver[j];
pos[idx++].count=pos[i].count+1;
board[pos[i].y+dir_ver[j]][pos[i].x+dir_hor[j]]=pos[i].count+1;
}
}
}
2008年12月26日 星期五
369 - Combinations
Combinations
| Combinations |
Computing the exact number of ways that N things can be taken M at a time can be a great challenge when N and/or M become very large. Challenges are the stuff of contests. Therefore, you are to make just such a computation given the following:
GIVEN:
Compute the EXACT value of:
You may assume that the final value of C will fit in a 32-bit Pascal LongInt or a C long.
For the record, the exact value of 100! is:
93,326,215,443,944,152,681,699,238,856,266,700,490,715,968,264,381,621,
468,592,963,895,217,599,993,229,915,608,941,463,976,156,518,286,253,
697,920,827,223,758,251,185,210,916,864,000,000,000,000,000,000,000,000
Input and Output
The input to this program will be one or more lines each containing zero or more leading spaces, a value for N, one or more spaces, and a value for M. The last line of the input file will contain a dummy N, M pair with both values equal to zero. Your program should terminate when this line is read.
The output from this program should be in the form:
N things taken M at a time is C exactly.
Sample Input
100 6
20 5
18 6
0 0
Sample Output
100 things taken 6 at a time is 1192052400 exactly.===========================================
20 things taken 5 at a time is 15504 exactly.
18 things taken 6 at a time is 18564 exactly.
#include <stdio.h>
#include <stdlib.h>
int gcd(int,int);
void combi(const int,const int);
int main()
{
freopen("Input.txt","r",stdin);
freopen("Output.txt","w",stdout);
int N,M;
while(scanf("%d%d",&N,&M)==2)
if(N|M)
combi(N,M);
return 0;
}
/* calculate the combinition number */
void combi(const int N,const int M)
{
int A[M],B[M];
int i,j;
for(i=0;i<M;i++)
{
A[i]=N-i;
B[i]=i+1;
}
for(j=1;j<M;j++)
for(i=0;i<M;i++)
{
/* find the GCD of each pair when B[] is not 1 */
int GCD=gcd(A[i],B[j]);
if(GCD>1)
{
A[i]/=GCD;
B[j]/=GCD;
}
if(B[j]==1)
break;
}
/* when all elements in B[] are 1 then multiply every element in A[] */
long ans=1;
for(i=0;i<M;i++)
ans*=A[i];
printf("%d things taken %d at a time is %d exactly.\n",N,M,ans);
}
/* return (x,y) by recursive */
int gcd(int x,int y)
{
return (!y)?x:gcd(y,x%y);
}
2008年12月24日 星期三
2008年12月22日 星期一
350 - Pseudo-Random Numbers
Pseudo-Random Numbers
| Pseudo-Random Numbers |
Computers normally cannot generate really random numbers, but frequently are used to generate sequences of pseudo-random numbers. These are generated by some algorithm, but appear for all practical purposes to be really random. Random numbers are used in many applications, including simulation.
A common pseudo-random number generation technique is called the linear congruential method. If the last pseudo-random number generated was L, then the next number is generated by evaluating ( , where Z is a constant multiplier, I is a constant increment, and M is a constant modulus. For example, suppose Z is 7, I is 5, and M is 12. If the first random number (usually called the seed) is 4, then we can determine the next few pseudo-random numbers are follows:
As you can see, the sequence of pseudo-random numbers generated by this technique repeats after six numbers. It should be clear that the longest sequence that can be generated using this technique is limited by the modulus, M.
In this problem you will be given sets of values for Z, I, M, and the seed, L. Each of these will have no more than four digits. For each such set of values you are to determine the length of the cycle of pseudo-random numbers that will be generated. But be careful: the cycle might not begin with the seed!
Input
Each input line will contain four integer values, in order, for Z, I, M, and L. The last line will contain four zeroes, and marks the end of the input data. L will be less than M.
Output
For each input line, display the case number (they are sequentially numbered, starting with 1) and the length of the sequence of pseudo-random numbers before the sequence is repeated.
Sample Input
7 5 12 4
5173 3849 3279 1511
9111 5309 6000 1234
1079 2136 9999 1237
0 0 0 0
Sample Output
Case 1: 6============================================
Case 2: 546
Case 3: 500
Case 4: 220
#include <stdio.h>
#include <stdlib.h>
/* function prototype */
int random_length(const int,const int,const int,int);
int main()
{
freopen("Input.txt","r",stdin);
freopen("Output.txt","w",stdout);
int Z,I,M,L,data_num=0;
while(scanf("%d%d%d%d",&Z,&I,&M,&L)==4)
if(Z|I|M|L)
printf("Case %d: %d\n",++data_num,random_length(Z,I,M,L));
return 0;
}
/* return the length of the random number cycle */
int random_length(const int Z,const int I,const int M,int L)
{
/* record the random numbers */
/* module 4-digit number only get 0~9998 */
int array[9999];
int i,j,count,ap;
for(ap=0,count=1;;count++)
{
array[ap++]=L;
L=(Z*L+I)%M;
for(j=0;j<ap;j++)
if(L==array[j])
/* find the begin of the cycle */
return count-j;
}
}
344 - Roman Digititis
Roman Digititis
| Roman Digititis |
Many persons are familiar with the Roman numerals for relatively small numbers. The symbols ``i", ``v", ``x", ``l", and ``c" represent the decimal values 1, 5, 10, 50, and 100 respectively. To represent other values, these symbols, and multiples where necessary, are concatenated, with the smaller-valued symbols written further to the right. For example, the number 3 is represented as ``iii", and the value 73 is represented as ``lxxiii". The exceptions to this rule occur for numbers having units values of 4 or 9, and for tens values of 40 or 90. For these cases, the Roman numeral representations are ``iv" (4), ``ix" (9), ``xl" (40), and ``xc" (90). So the Roman numeral representations for 24, 39, 44, 49, and 94 are ``xxiv", ``xxxix", ``xliv", ``xlix", and ``xciv", respectively.
The preface of many books has pages numbered with Roman numerals, starting with ``i" for the first page of the preface, and continuing in sequence. Assume books with pages having 100 or fewer pages of preface. How many ``i", ``v", ``x", ``l", and ``c" characters are required to number the pages in the preface? For example, in a five page preface we歓l use the Roman numerals ``i", ``ii", ``iii", ``iv", and ``v", meaning we need 7 ``i" characters and 2 ``v" characters.
Input
The input will consist of a sequence of integers in the range 1 to 100, terminated by a zero. For each such integer, except the final zero, determine the number of different types of characters needed to number the prefix pages with Roman numerals.
Output
For each integer in the input, write one line containing the input integer and the number of characters of each type required. The examples shown below illustrate an acceptable format.
Sample Input
1
2
20
99
0
Sample Output
1: 1 i, 0 v, 0 x, 0 l, 0 c================================================
2: 3 i, 0 v, 0 x, 0 l, 0 c
20: 28 i, 10 v, 14 x, 0 l, 0 c
99: 140 i, 50 v, 150 x, 50 l, 10 c
#include <stdio.h>
#include <stdlib.h>
/* function prototype */
void digit_change(int,int [5]);
int main()
{
freopen("Input.txt","r",stdin);
freopen("Output.txt","w",stdout);
int num,i,count[5];
while(scanf("%d",&num)==1)
if(num!=0)
{
/* reset the number counting array */
for(i=0;i<5;i++)
count[i]=0;
for(i=1;i<=num;i++)
digit_change(i,count);
printf("%d: %d i, %d v, %d x",num,count[0],count[1],count[2]);
printf(", %d l, %d c\n",count[3],count[4]);
}
return 0;
}
/* return every elements of Roman digit from 1 to num */
void digit_change(int num,int count[5])
{
/* handle the exception "100" */
if(num==100)
{
count[4]++;
return;
}
/* the representations of units and tens are the same, */
/* just differenct at count[] index shift */
int i,n,k,times=1;
if(num>9)
times=2;
for(i=0,k=0,n=num%10;i<times;i++,k+=2,n=num/10)
if(n!=0)
/* separate one digit to three phases */
if(n>0&&n<4)
count[0+k]+=n;
else if(n>3&&n<9)
{
count[1+k]++;
count[0+k]+=abs(n-5);
}
else
{
count[0+k]++;
count[2+k]++;
}
}
2008年12月21日 星期日
352 - The Seasonal War
The Seasonal War
| The Seasonal War |
The inhabitants of Tigerville and Elephantville are engaged in a seasonal war. Last month, Elephantville successfully launched and orbited a spy telescope called the Bumble Scope. The purpose of the Bumble Scope was to count the number of War Eagles in Tigerville. The Bumble Scope, however, developed two problems because of poor quality control during its construction. Its primary lens was contaminated with bugs which block part of each image, and its focusing mechanism malfunctioned so that images vary in size and sharpness.
The computer programmers, who must rectify the Bumble Scope's problems are being held hostage in a Programming Contest Hotel in Alaland by elephants dressed like tigers. The Bumble Scope's flawed images are stored by pixel in a file called Bumble.in. Each image is square and each pixel or cell contains either a 0 or a 1. The unique Bumble Scope Camera (BSC) records at each pixel location a 1 if part or all of a war eagle is present and a 0 if any other object, including a bug, is visible. The programmers must assume the following:
- a)
- A war eagle is represented by at least a single binary one.
- b)
- Cells with adjacent sides on common vertices, which contain binary ones, comprise one war eagle. A very large image of one war eagle might contain all ones.
- c)
- Distinct war eagles do not touch one another. This assumption is probably flawed, but the programmers are desperate.
- d)
- There is no wrap-around. Pixels on the bottom are not adjacent to the top and the left is not adjacent to the right (unless, of course, there are only 2 rows or 2 columns)
Input and Output
Write a program that reads images of pixels from the input file (a text file), correctly counts the number of war eagles in the images and prints the image number and war eagle count for that image on a single line in the output file (also a text file).
Use the format in the sample output. Do this for each image in the input file. Each image will be preceded by a number indicating its square dimension. No dimension will exceed 25.
Sample input
6
100100
001010
000000
110000
111000
010100
8
01100101
01000001
00011000
00000010
11000011
10100010
10000001
01100000
Sample output
Image number 1 contains 3 war eagles.
Image number 2 contains 6 war eagles.
=========================================================
#include <stdio.h>
#include <stdlib.h>
#define MAX_DIM 25
/* functions prototype */
int find_eagle(int);
void DFS(int,int,int,int);
/* global variables */
int map[MAX_DIM+2][MAX_DIM+2];
int map2[MAX_DIM+2][MAX_DIM+2];
int main()
{
freopen("Input.txt","r",stdin);
freopen("Output.txt","w",stdout);
int dimention,data_num=0;
while(scanf("%d",&dimention)==1)
{
char row[dimention];
int i,j;
for(i=1;i<=dimention;i++)
{
scanf("%s",row);
for(j=1;j<=dimention;j++)
map[i][j]=row[j-1]-'0';
}
printf("Image number %d contains %d war eagles.\n",++data_num,find_eagle(dimention+2));
}
return 0;
}
/* return how many eagles */
int find_eagle(int dim)
{
int i,j,label=1;
/* map2 is the object label image map */
for(i=0;i<MAX_DIM+2;i++)
for(j=0;j<MAX_DIM+2;j++)
map2[i][j]=0;
/* if there's a point is 1 then find it's connected points */
for(i=1;i<dim-1;i++)
for(j=1;j<dim-1;j++)
if(map[i][j]==1&&map2[i][j]==0)
DFS(i,j,label++,dim);
return label-1;
}
/* mark the connected points with same label with eight directions */
void DFS(int x,int y,int lab,int dim)
{
/* if this point (x,y) hasn't been marked */
if(map2[x][y]==0)
{
map2[x][y]=lab;
if(map[x+1][y+1]==1)
DFS(x+1,y+1,lab,dim);
if(map[x+1][y]==1)
DFS(x+1,y,lab,dim);
if(map[x+1][y-1]==1)
DFS(x+1,y-1,lab,dim);
if(map[x][y-1]==1)
DFS(x,y-1,lab,dim);
if(map[x-1][y-1]==1)
DFS(x-1,y-1,lab,dim);
if(map[x-1][y]==1)
DFS(x-1,y,lab,dim);
if(map[x-1][y+1]==1)
DFS(x-1,y+1,lab,dim);
if(map[x][y+1]==1)
DFS(x,y+1,lab,dim);
}
}
A Question Of Honor - Sarah Brightman
Ebbene? ... N'andro lontana,
Come va l'eco della pia campana,
La, fra la neve bianca;
La, fra le nubi d'or;
La, dov'e la speranza, la speranza
Il rimpianto, il rimpianto, e il dolor!
Two men collide
When two men collide, when two men collide
It's a question of honour
Two men collide
When two men collide, when two men collide
It's a question of honour
Two men collide
When two men collide, when two men collide
If you win or you lose,
It's a question of honour
And the way that you choose,
It's a question of honour
I can't tell what's wrong or right
If black is white or day is night
But I know when two men collide
It's a question of honour
If you win or you lose,
It's a question of honour
And the way that you choose,
It's a question of honour
If you win or you lose,
It's a question of honour
And the way that you choose,
It's a question of honour
I can't tell what's wrong or right
If black is white or day is night
I know when two men collide
It's a question of honour
Ebbene? ... N'andro lontana,
Come l'eco della pia campana,
La, fra la neve bianca;
La, fra le nubi d'or;
N'andro, n'andro sola e lontana!
E fra le nubi d'or!
2008年12月20日 星期六
343 - What Base Is This? (Wrong Answer)
What Base Is This?
| What Base Is This? |
In positional notation we know the position of a digit indicates the weight of that digit toward the value of a number. For example, in the base 10 number 362 we know that 2 has the weight , 6 has the weight
, and 3 has the weight
, yielding the value
, or just 300 + 60 + 2. The same mechanism is used for numbers expressed in other bases. While most people assume the numbers they encounter everyday are expressed using base 10, we know that other bases are possible. In particular, the number 362 in base 9 or base 14 represents a totally different value than 362 in base 10.
For this problem your program will presented with a sequence of pairs of integers. Let�'s call the members of a pair X and Y. What your program is to do is determine the smallest base for X and the smallest base for Y (likely different from that for X) so that X and Y represent the same value.
Consider, for example, the integers 12 and 5. Certainly these are not equal if base 10 is used for each. But suppose 12 was a base 3 number and 5 was a base 6 number? 12 base 3 = , or 5 base 10, and certainly 5 in any base is equal to 5 base 10. So 12 and 5 can be equal, if you select the right bases for each of them!
Input
On each line of the input data there will be a pair of integers, X and Y, separated by one or more blanks; leading and trailing blanks may also appear on each line, are are to be ignored. The bases associated with X and Y will be between 1 and 36 (inclusive), and as noted above, need not be the same for X and Y. In representing these numbers the digits 0 through 9 have their usual decimal interpretations. The uppercase alphabetic characters A through Z represent digits with values 10 through 35, respectively.
Output
For each pair of integers in the input display a message similar to those shown in the examples shown below. Of course if the two integers cannot be equal regardless of the assumed base for each, then print an appropriate message; a suitable illustration is given in the examples.
Sample Input
12 5
10 A
12 34
123 456
1 2
10 2
Sample Output
12 (base 3) = 5 (base 6)
10 (base 10) = A (base 11)
12 (base 17) = 34 (base 5)
123 is not equal to 456 in any base 2..36
1 is not equal to 2 in any base 2..36
10 (base 2) = 2 (base 3)
=====================================================================
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int length(char *,int);
int base_test(char *,int,int);
int find(char *,char *);
/* flag of least bases */
int max[2];
int main()
{
freopen("Input.txt","r",stdin);
freopen("Output.txt","w",stdout);
char s1[100],s2[100];
while(scanf("%s%s",s1,s2)==2)
if(find(s1,s2)==0)
printf("%s is not equal to %s in any base 2..36\n",s1,s2);
return 0;
}
/* return string length and set the minimum base */
int length(char *str,int key)
{
int i,base=str[0];
for(i=1;str[i]!='\0';i++)
if(base<str[i])
base=str[i];
base=(base-'0'>9)?(base-'A'+10+1):(base-'0'+1);
max[key]=base;
return i;
}
/* count the sumation in base "base" to decimal number */
int base_test(char *str,int len,int base)
{
int i,sum=0,weight;
for(i=0;i<len;i++)
{
weight=(str[len-i-1]-'0'>9)?(str[len-i-1]-'A'+10):(str[len-i-1]-'0');
sum+=weight*(int)pow(base,i);
}
return sum;
}
/* return 0 if they are not equal at any base, 1 otherwise */
int find(char *s1,char *s2)
{
int i,j;
int len1=length(s1,0);
int len2=length(s2,1);
for(i=max[0];i<=36;i++)
for(j=max[1];j<=36;j++)
if((base_test(s1,len1,i))==(base_test(s2,len2,j)))
{
printf("%s (base %d) = %s (base %d)\n",s1,i,s2,j);
return 1;
}
return 0;
}
2008年12月19日 星期五
Display a Picture
需填入 cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib 這些會用到的否則無法正常編譯!
#include "highgui.h"
int main(int argc, char** argv)
{
IplImage* img=cvLoadImage(argv[1]);
cvNamedWindow("Example1",CV_WINDOW_AUTOSIZE);
cvShowImage("Example1",img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
return 0;
}
/* IplImage 為函式庫中的圖檔結構 */
/* cvLoadImage() 可讀取大部分的圖片檔案,有路徑的地方如C:\需用C:\\代替 */
/* cvLoadImage()回傳一個pointer */
/* 可輸入第二個參數 */
/* -1:預設讀取圖像的原通道數,0:強制轉化讀取圖像為灰階,1:讀取彩色影像 */
IplImage* img=cvLoadImage("file path",int);
/* console之外再創一個視窗且命名為"title" */
/* 第二個參數會將圖片縮放為符合開啟的視窗大小,預設是1 */
cvNamedWindow("title",CV_WINDOW_AUTOSIZE);
/* 在名為"title"的window中顯示讀取到的結構資料 */
cvShowImage("title",IplImage *);
/* 若參數為0或負數,則等鍵盤輸入一個按鍵才結束 */
/* 若參數為正數N,則會暫停N毫秒 */
cvWaitKey(int);
/* 釋放所配給的記憶體,指令完成後會將poniter指向NULL */
cvReleaseImage(&img);
/* 關閉視窗且會釋放跟此有關且有使用的記憶體,包含image buffer */
/* 雖然程式結束後都會自動的釋放記憶體,但卻不是個好習慣 */
cvDestroyWindow("Example1");
2008年12月18日 星期四
暮光之城

有三件事我很確定:
第一、愛德華是吸血鬼
第二、出於天性,他渴望喝我的血
第三、我無可救藥地愛上他了……
貝拉從繁華的鳳凰城搬到偏僻且陰雨不斷的福克斯,她原本認為往後的日子會很無聊,但當她遇上神祕又迷人的愛德華之後,生活開始變得刺激有趣,心也深深地被吸引。到目前為止,愛德華一家人身為吸血鬼的秘密,在福克斯是不為人知的,而如今,所有人都陷入險境,特別是貝拉──愛德華最摯愛的人。
他們之間濃烈的愛意,讓兩人就像在刀尖上行走,在慾望與危險間掙扎著求取平衡。
Preface
我將如何死亡,我並沒怎麼多想──雖然最後這幾個月,我的確有足夠的理由來思考這個問題──但就算我真的想過,也想像不到會是這般的情景。
我屏住呼吸、走過長廊,凝望獵人漆黑的雙眼,他愉悅的看著我。
這的確是個挺好的死亡方法:在一個只有我愛的人與我同在的地方。可以算是壯麗的,應該是值得的……
我知道,如果我沒來福克斯,現在的我就無須面對死亡,但是,無論我有多害怕,我對這個決定永不後悔。當生命給你一個超乎預期的夢想時,就算即將死亡,也不應悲傷。
獵人給我一個友善的微笑,從容的向前──殺死我。
2008年12月17日 星期三
暮光之城:無懼的愛 ( twilight )
預告片中文版
劇情簡介:
如果可以永遠不死,那你要為誰而活?
貝拉史旺(克莉絲汀史都華飾演)一直有點特立獨行,在她唸的鳳凰中學裡,從來就不在乎和愛時髦的女同學處不處得來。母親改嫁之後,把貝拉送去華盛頓州佛斯 小鎮,和她的父親同住,可是她萬萬沒想到會發生重大的改變,直到她遇見了神秘又俊美的艾德華卡倫(羅伯派亭森飾演),她沒見過像他這樣的男孩。他聰明又機 智,一眼就看穿了她的靈魂。過不了多久,貝拉和艾德華就展開了一場激烈又違反傳統的愛情。艾德華跑起來比美洲獅還要快,可以赤手空拳攔下一輛行駛中的汽 車,而且他從1918年就到現在,一點也沒有變老。他和所有的吸血鬼一樣,都是永生不死的,可是他沒有尖牙,也不吸人血,因為艾德華和他的家人有別於其他 的吸血鬼,他們選擇了不同的生活方式。
對艾德華而言,貝拉是他等待了90年才得到的靈魂伴侶,不過他們愈是親近,艾德華就愈要拼命抵抗對 她身上氣味的本能吸引力,否則他有可能會抓狂到難以控制的地步。後來,與卡倫家族對立的羅倫(艾迪蓋瑟吉飾演)和詹姆斯(凱姆吉甘特飾演)來到這個小鎮尋 找貝拉,他該怎麼辦才好呢?
關於小說
史蒂芬妮梅爾的暢銷小說《暮光之城》,在國內熱賣550萬本,連續32週登上《紐約 時報》暢銷書排行榜,這是同系列小說的第一部。《暮光之城》是一個文化現象,有基本的忠實書迷,迫不及待想看改編拍成的電影。《暮光之城》有一百多個書迷 網站,也被幾大出版商選為年度風雲小說,已經被翻譯成20種語言了。這是一部現代版的羅密歐與茱麗葉,敘述吸血鬼和人類之間禁忌的愛。
=======================================================
雖然背景像是決戰異世界般有吸血鬼及萊肯,卻不是動作片,而是偏向愛情方面,
不像一般的吸血鬼電影主打驚悚、血腥、特效,想帶給人的是另外一種感受,
如同劇情簡介第一句話,如此簡單的問題,卻又難以回答,某幾幕中其實有被感動到,
不論是親情或是愛情成分,極端的情緒更能讓觀眾有所感受,男主角演的不錯,
不過可能礙於片長問題,感覺很難感受到一些細節,好想買小說來看!
我覺得是部不錯又值得看的電影,續集應該會更吸引人。