威尼斯演展官網
並比較下圖
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg04C_8crgxYtP4aSlgfBYgp-f5m8l-xSj9cL609H5I1pyAstc7NcGbWNqukbK34XK8dpGi_8ZzPNMOlUj-gnKed3Pu99PhnNTrj7arbr6HY54OBU4khvqJn2XzbFNW7zydJaQAVlNlkyI/s320/2011-07-31_200843.jpg)
package acm;
import java.io.*;
import java.util.*;
public class ThreeNPlusOne {
public static void main(String[] args) throws IOException {
ArrayList<String> userInput = new ArrayList<String>();
int intCases = 0;
System.out.println("How many test cases to run?");
BufferedReader bs = new BufferedReader(new InputStreamReader(System.in));
String cases = bs.readLine();
intCases = Integer.parseInt(cases);
while (intCases != userInput.size()) {
System.out
.println("Please enter 2 numbers m and n,separate them by dot.");
userInput.add(bs.readLine());
}
Iterator it = userInput.iterator();
while (it.hasNext()) {
String numbers = (String) (it.next());
int[] ns = getInts(numbers);
int first = ns[0];
int second = ns[1];
System.out.println(first + " " + second + " "+ threeN(first, second));
}
}
public static int threeN(int a, int b) {
int max= 0;
for (int i = a; i <= b; ) {
int tmp=i;
int count = 0;
while (true) {
count++;
if (i == 1) {
break;
} else {
if (i % 2 != 0) {
i = 3 * i + 1;
} else {
i /= 2;
}
}
}
if (count > max) {
max = count;
}
i=++tmp;
}
return max;
}
public static int[] getInts(String str) {
int totalNumbers = str.split(",").length;
int[] ints = new int[totalNumbers];
for (int i = 0; i < totalNumbers; i++) {
ints[i] = Integer.parseInt(str.split(",")[i].trim());
}
return ints;
}
}
package acm;
import java.io.*;
import java.util.*;
public class GCD {
public static void main(String[] args) throws IOException {
ArrayList<String> userInput = new ArrayList<String>();
while (true) {
int intCases = 0;
System.out.println("How many test cases to run?(from 1 to 1000)?");
BufferedReader bs = new BufferedReader(new InputStreamReader(
System.in));
String cases = bs.readLine();
intCases = Integer.parseInt(cases);
if (intCases < 1 || intCases > 1000) {
System.out.println("Please enter a valid number!!");
} else {
System.out.println("Please enter 2 numbers m and n,separate them by dot.");
System.out.println("ex: 12,60");
while (intCases != userInput.size()) {
userInput.add(bs.readLine());
}
break;
}
}
Iterator it=userInput.iterator();
while(it.hasNext()){
String numbers=(String)(it.next());
int[] ns=getInts(numbers);
System.out.println(gcd(ns[0],ns[1]));
}
}
public static int gcd(int a, int b) {
while (a != 0 && b != 0) {
if (a > b)
a %= b;
else
b %= a;
}
if (a == 0)
return b;
else
return a;
}
public static int[] getInts(String str){
int totalNumbers=str.split(",").length;
int[] ints=new int[totalNumbers];
for(int i=0;i<totalNumbers;i++){
ints[i]=Integer.parseInt(str.split(",")[i]);
}
return ints;
}
}
#include <omp.h>
#include <iostream>
using namespace std;
int main() {
int n=10;
#pragma omp parallel shared(n)
{
#pragma omp for
for (int i=0; i<n; i++)
cout <<"Thread "<<omp_get_thread_num()<<" executes loop iteration "<< i<<endl;
}
return 0;
}
#include <omp.h>
#include <iostream>
using namespace std;
int main() {
#pragma omp parallel
{
cout <<"The parallel region is executed by thread "<<omp_get_thread_num();
if (omp_get_thread_num() == 1) {
cout <<" Thread "<<omp_get_thread_num()<<" does things differently\n";
}
}
return 0;
}
#ifdef _OPENMP
#include <omp.h>
#else
#define omp_get_thread_num() 0
#endif
//some code here
int TID = omp_get_thread_num();
#include <iostream>
#define capacity 5
using namespace std;
class ArrayQueue {
public:
int size;
int myFrontPos, myBackPos;
int array[capacity];
ArrayQueue(){
size=0;
myFrontPos=0;
myBackPos=0;
}
void enqueue(int number) {
int newBack=(myBackPos+1)%capacity;
if (size==capacity) {
cout<<"queue is full!!"<<endl;
} else {
array[myBackPos]=number;
myBackPos=newBack;
size++;
}
}
void dequeue() {
if (size==0) {
cout<<"queue is empty!!"<<endl;
} else {
myFrontPos=(myFrontPos+1)%capacity;
size--;;
}
}
void display() {
for(int i=0;i<size;i++){
cout <<array[i]<<endl;
}
}
};
int main() {
ArrayQueue aq;
aq.enqueue(1);
aq.enqueue(2);
aq.enqueue(3);
aq.enqueue(4);
aq.enqueue(5);
aq.dequeue();
aq.enqueue(6);
aq.display();
return 0;
}
若沒有塞值進入陣列的話,印出來的陣列值是殘存在那個記憶體位置裡的值,如下列例子:
我們也可用pointer(指標)的方式來對陣列存取,
陣列的第一個元素的位址(address)就是陣列的名稱,
因此我們可以把此名稱指定給poiner然後循序遞增或遞減pointer來存取陣列,
如下範例:
Subsequently, let's introduce the Dynamic Array.
The differences between static and dynamic are the declaration and memory handling.
For declaration, a "new" reserved word needs to be along with the variable.And we also need to delete the pointer variable to release the reserved memory block. It is a very important thing to prevent from a memory leak.
Following is the example code, you can focus on the colored words.
#include <omp.h>
#include <iostream>
using namespace std;
int main() {
#pragma omp parallel
cout<<"Hello from thread "<<omp_get_thread_num()<<", nthreads"
<< omp_get_num_threads()<<endl;
return 0;
}
#include <omp.h>
#include <iostream>
using namespace std;
int main() {
#pragma omp single
cout<<"Hello from thread "<<omp_get_thread_num()<<", nthreads"
<< omp_get_num_threads()<<endl;
return 0;
}
Queue:是一種先進先出的資料結構(First In First Out).
下面的範例程式使用C++ 的Linked List來實現Queue.
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* nxtNode;
Node(int _data) {
data=_data;//Data
nxtNode=NULL;//pointer to next one
}
};
class LinkedList {
Node* previous;
Node* next;
public:
LinkedList() {
previous=NULL;
next=NULL;
}
void insert(Node * _node) {
if (previous==NULL) {
previous= next= _node;//first node
} else {
(*next).nxtNode=_node;
next=_node;
}
}
void displayAll() {
Node* tmp=previous;
while(tmp!=NULL){
cout<<(*tmp).data <<endl;
tmp=(*tmp).nxtNode;
}
}
};
int main() {
Node* firstNode=new Node(1);
Node* secondNode=new Node(2);
Node* thirdNode=new Node(3);
LinkedList ll;
ll.insert(firstNode);
ll.insert(secondNode);
ll.insert(thirdNode);
ll.displayAll();
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a=1;
int & a1=a;//a1 is a reference variable
cout << a1 <<endl;
return 0;
}
#include <iostream>
using namespace std;
void sum4(int &);
int main() {
int a=1;
sum4(a);
cout << a <<endl;
return 0;
}
void sum4(int & a) {
a=2;
}
#include <iostream>
using namespace std;
int & sum5(int &);
int main() {
int a=1;
sum5(a)=3;
cout << a << endl;
return 0;
}
int & sum5(int &a){
return a;
}
#include <iostream>
using namespace std;
const int & sum5(int &);
int main() {
int a=1;
sum5(a)=3;
cout << a << endl;
return 0;
}
const int & sum5(int &a){
return a;
}
"那些年,我們一起追的女孩,觀後感"
#include <iostream>
using namespace std;
void sum3(int);
int main() {
int a=1;//Static Variable
sum3(a);
int * ptr=new int;//Dynamic Variable
*ptr=2;
cout << "before delete: "<<endl;
cout << *ptr<<endl;
delete ptr;
cout << "after delete:" <<endl;
cout << *ptr<<endl;
return 0;
}
void sum3(int a) {
int b=a;//Local Variable
cout << b<<"\n";
}
#include <iostream>
using namespace std;
namespace MyException {
class Exception {
public:
void mesg() {
cout << "Exception Occurred !!"<<endl;
}
};
}
void sum2(int, int) throw (MyException::Exception);
int main() {
int a=1;
int b=1;
try {
sum2(a, b);
} catch (MyException::Exception & ex) {// start of exception handler
ex.mesg();
}
return 0;
}
void sum2(int a, int b) throw (MyException::Exception) {
if (a==b) {
throw MyException::Exception();
}
cout << a+b<<"\n";
}
解說:
先命名一個namespace為MyException的block,然後在block內定義一個Exception類別,接下來使用"命名空間::類別名稱"來使用此類別
namespace MyException {
namespace SecondLevel {
class Exception {
public:
void mesg() {
cout << "Exception Occurred !!"<<endl;
}
};
}
}
#include <iostream>
using namespace std;
void sum(int, int);
int main() {
int a=1;
int b=1;
try {
sum(a, b);
} catch (const char * s) {// start of exception handler
cout << s <<endl;
}
return 0;
}
void sum(int a, int b) {
if (a==b) {
throw "This Combination is unacceptable!!";
}
cout << a+b<<"\n";
}
紅色字的部分是表示當function sum(int,int)在try block拋出string的錯誤訊息時,catch block(區塊)要把此錯誤訊息印出來.
前一個範例是拋出字串,這一個範例是拋出例外類別.使用這一個方法的好處是我們可以為exception分類出不同類別,以方便辨識.另外一方面,也提升了重用性(re-usability).
#include <iostream>
using namespace std;
class Exception {
public:
void mesg() {
cout << "Exception Occurred !!"<<endl;
}
};
void sum1(int, int) throw (Exception);
int main() {
int a=1;
int b=1;
try {
sum1(a, b);
} catch (Exception & ex) {// start of exception handler
ex.mesg();
}
return 0;
}
void sum1(int a, int b) throw (Exception) {
if (a==b) {
throw Exception();
}
cout << a+b<<"\n";
}
解說:
第一部分是宣告一個Exception class(例外類別)
第二部分部分只是把拋出字串,改為拋出此例外類別.
第三部分部分是函式的實做.要記得在定義define(定義)以及declaration(宣告)的時候加上throw關鍵字指定要丟出何種類外類別.
void sum(int , int);
template <class T> void sum(T , T);
void sum(T a, T b) {
cout << a+b;
}
#include <iostream>
using namespace std;
template <class T> void sum(T, T);
int main() {
int a=1, b=2;
double c=1.1, d=2.2;
sum(a, b) ;
sum(c, d) ;
return 0;
}
template <class T> void sum(T a, T b) {
cout << a+b<<"\n";
}
如果sum()這個函式的名字不變,但傳入的參數改為三個,
那麼這種情形便稱作多載,多載化的版本也是要先宣告template,然後在實做出來.
請注意紅色字的部分是新增出來的多載化的程式碼.
#include <iostream>
using namespace std;
template <class T> void sum(T, T);
template <class T> void sum(T, T, T);
int main() {
int a=1, b=2;
double c=1.1, d=2.2 ,e=3.3;
sum(a, b) ;//3
sum(c, d) ;//3.3
sum(c, d, e)//6.6 ;
return 0;
}
template <class T> void sum(T a, T b) {
cout << a+b<<"\n";
}
template <class T> void sum(T a, T b, T c){
cout << a+b+c<<"\n";
}
如何判別一個程式寫得好不好,可以用這個程式需要花費的記憶體,或是說要花多少時間才能解出答案,來判斷.因為現在硬體的價格很大眾化.所以我們可以專注在,時間上的討論. 有一個叫做Big-O的概念: 因為會影響時間的因素很多,因此這個概念主要是以討論程式執行的次數多寡來決定要花多少時間.一般我們都是採取比較嚴格的標準檢視程式,所以主要都是討論,在最差的情形下,這程式要花多少時間找出答案.
for (int i=0; i<n; i++) {
}
answer=n*2;
#include <iostream>
using namespace std;
int recursive(int, int);
int main() {
cout << recursive(2,3);
return 0;
}
int recursive(int x, int n){
if(n==0){
return 1;
}else{
return x*recursive(x, n-1);
}
}
#include <iostream>
using namespace std;
int recursive(int, int);
int main() {
cout << recursive(2, 3);
return 0;
}
int recursive(int x, int n) {
int answer=1;
if (n==0) {
return answer;
} else {
for (int i=0; i<n; i++) {
answer *=x;
}
}
return answer;
}
typedef可以用來為常用的變數型態設定方便自己識別的名稱.
#include <iostream>
using namespace std;
typedef int Integer;
int main() {
Integer a=1;
Integer * number= &a;
Integer * number2= number;
(*number2)=3;
cout << *(number) << "\n";
return 0;
}
1. 指標可以用來指定一個記憶體位址儲存變數資料,用"&"符號可以取出變數的記憶體位址,"*"符號則是取出實際所儲存的值
#include <iostream>
using namespace std;
int main() {
int a=1;
int * number= &a;
cout << *(number) << "\n";
return 0;
}
2. 指標也可以互相指定到同一個記憶體位址,但要很小心,萬一任何一個指標更動到實際所儲存值,那麼另一個也會受影響到
#include <iostream>
using namespace std;
int main() {
int a=1;
int * number= &a;
int * number2= number;
(*number2)=3;
cout << *(number) << "\n";
return 0;
}
The complexity of selection sort is O(n^2)
selection sort(選擇排序)的基本概念是,找出最大或最小的數字,放在最左邊,然後再從剩下的數字中,找出最大或最小的值,依次由左到右放,直到所有的數字都檢查過為止.
other sort algorithms:
Quick Sort