การใช้ high Resolution Timer ด้วย c++ 


การใช้ high Resolution Timer ด้วย c++




#include <windows.h>
#include <stdio.h>
#include <conio.h>

typedef struct {
LARGE_INTEGER start;
LARGE_INTEGER stop;
} stopWatch;

class CStopWatch {

private:
stopWatch timer;
LARGE_INTEGER frequency;
double LIToSecs( LARGE_INTEGER & L) ;
public:
CStopWatch() ;
void startTimer( ) ;
void stopTimer( ) ;
double getElapsedTime() ;
};
double CStopWatch::LIToSecs( LARGE_INTEGER & L) {
return ((double)L.QuadPart /(double)frequency.QuadPart) ;
}

CStopWatch::CStopWatch(){
timer.start.QuadPart=0;
timer.stop.QuadPart=0;
QueryPerformanceFrequency( &frequency ) ;
}

void CStopWatch::startTimer( ) {
QueryPerformanceCounter(&timer.start) ;
}

void CStopWatch::stopTimer( ) {
QueryPerformanceCounter(&timer.stop) ;
}

double CStopWatch::getElapsedTime() {
LARGE_INTEGER time;
time.QuadPart = timer.stop.QuadPart - timer.start.QuadPart;
return LIToSecs( time) ;
}

#define n 200000

int main(int argc, char*argv[])
{
int x[n];
int j= 0;
int k = 1;
int l = 0;
int m=0;
CStopWatch timer;
for(int k=1;k<n/2;k=k*2)
{
timer.startTimer();
for(int l=0;l<20;l++)
{

for(int i=0;i< k;i++)
{
m=i;
while(m<n)
{
//printf("%d ",m);
x[m] = 7*x[m]*x[m];
m = (m + k ) ;
}
}
}
timer.stopTimer();
printf("\nk= %d --> \t%lf\n\n",k,timer.getElapsedTime());
}



}



http://expert-programming-tutor.com/

[ view entry ] ( 2020 views )   |  permalink  |  $star_image$star_image$star_image$star_image$star_image ( 3 / 1247 )
เฉลย Google Code jam 2009 : Welcome to google code jam (Dynamic Programming) 


เฉลย Google Code jam 2009 : Welcome to google code jam (Dynamic Programming)




จากโจทย์
https://code.google.com/codejam/contest/90101/dashboard#s=p2&a=2


import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;

public class WelcomToGoogleCodeJam {
public static void main(String[] args) {
try {
String welcom = "welcome to code jam";
PrintWriter pw = new PrintWriter(new File("out-large.out"));
Scanner sc = new Scanner(new File("C-large-practice.in"));

int T = Integer.parseInt(sc.nextLine());
for (int i = 1; i <= T; i++) {
String s = sc.nextLine();
BigInteger mem[] = new BigInteger[welcom.length()];
for(int j=0;j< welcom.length();j++)
{
mem[j] = new BigInteger("0");
}
for (int j = 0; j < s.length(); j++) {
char x = s.charAt(j);
if (welcom.contains("" + x)) {
if (x == welcom.charAt(0)) {
mem[0] = mem[0] .add(BigInteger.ONE);
} else {
for (int k = 1; k < welcom.length(); k++) {
if (x == welcom.charAt(k))
mem[k] = mem[k].add( mem[k - 1]);
}
}
}
}
String output ;
output = String.format("%04d", mem[ welcom.length()-1].mod(new BigInteger( "10000")));
System.out.println("Case #"+i+": "+output);
pw.println("Case #"+i+": "+output);
}
pw.close();
} catch (IOException ee) {
ee.printStackTrace();
}

}
}



[ view entry ] ( 11632 views )   |  permalink  |  $star_image$star_image$star_image$star_image$star_image ( 3 / 1078 )
Linked List ในภาษา C 

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

typedef struct Nodex
{
int data;
Nodex* next;
} Node ;
typedef struct List
{
int size;
Node * head;
} LinkedList;

void addFirst(LinkedList* ll , int data )
{
if(ll->head == NULL)
{
ll->head = (Node*)malloc(sizeof(Node));
ll->head->data = data;
ll->head->next = NULL;
}
else
{
Node * n= (Node*)malloc(sizeof(Node));
n->data = data;
n->next = ll->head;
ll->head = n;
}
ll->size ++;
}

void addLast(LinkedList* ll , int data )
{
if(ll->head == NULL)
{
ll->head = (Node*)malloc(sizeof(Node));
ll->head->data = data;
ll->head->next = NULL;
}
else
{
Node * n= (Node*)malloc(sizeof(Node));
n->data = data;
n->next = NULL;
Node * k = ll->head;

while(k->next != NULL)
{
k=k->next;
}

k->next = n;
}
ll->size ++;
}

void insert(LinkedList* ll , int data,int at)
{
if(ll->head == NULL)
{
if(at ==0 )
{
ll->head = (Node*)malloc(sizeof(Node));
ll->head->data = data;
ll->head->next = NULL;
}
}
else
{
Node * k = ll->head;
int i=0;
while(k->next != NULL&& i < at-1)
{
k=k->next;
i++;
}
Node * n= (Node*)malloc(sizeof(Node));
n->data = data;
n->next = k->next ;
k->next = n;
}
ll->size ++;
}

void print(LinkedList* ll)
{
Node * k = ll->head;
int i=0;
while(k != NULL)
{
printf(" [%d]-> ",k->data, k->next );
k=k->next;
i++;
}
printf("\n");
}
void removeLast(LinkedList * ll)
{
if(ll->head == NULL) return;
if(ll->size == 1)
{
free (ll->head);
ll->head = NULL;
ll->size --;
return;
}
Node * k = ll->head;
int i=0;
while(k->next->next != NULL )
{
k=k->next;
i++;
}
free (k->next);
k->next = NULL;
ll->size --;

}


void sort(LinkedList * ll)
{
if(ll->head == NULL) return ;
int s = ll->size;
int i = 0 ;
int m;
for(;i < s ; i++)
{
Node * k = ll->head;
int j=0;
while(k->next != NULL && j < s- i )
{
if( k->data > k->next->data)
{
m = k->data;
k->data = k->next->data;
k->next->data = m;
}
k=k->next;
j++;
}
}
}
void removeFirst(LinkedList * ll)
{
if(ll->head == NULL) return;

Node * n= ll->head;
ll->head = ll->head->next;
free(n);

ll->size --;
}

void deleteListAll(LinkedList * ll)
{
while(ll->size > 0 )
{
removeFirst( ll);
}
}

int main(int argc,char* argv[])
{

LinkedList ll;
ll.head = NULL;
ll.size = 0;
addFirst (&ll,5);
addFirst (&ll,15);
addFirst (&ll,3);
addFirst (&ll,4);
addFirst (&ll,2);
addLast (&ll,21);
insert (&ll,40,1);

print (&ll);
sort(&ll);
print (&ll);


deleteListAll(&ll);

return 0;
}





[ view entry ] ( 1744 views )   |  permalink  |  $star_image$star_image$star_image$star_image$star_image ( 3 / 1129 )
Face Detection in C# 
มาเขียนโปรแกรม FACE DETECTION ด้วย C# กันเถอะ
จริงๆ ACCORD.VISION (http://accord-framework.net/) อันนี้มันก็คือ Image Processign โดย ใช้ Aforge.NET (http://www.aforgenet.com/) เป็นฐานนะครับ
1 Load Accord.Vision จาก NUGET ตามรูป







2 ใช้ CODE ดังนี้
HaarObjectDetector detector;
HaarCascade cascade = new FaceHaarCascade();
detector = new HaarObjectDetector(cascade, 120);

detector.SearchMode = (ObjectDetectorSearchMode.Default);
detector.ScalingMode = (ObjectDetectorScalingMode.SmallerToGreater);
detector.ScalingFactor = 1.5f;
detector.UseParallelProcessing = true;
detector.Suppression = 2;

Stopwatch sw = Stopwatch.StartNew();
// Process frame to detect objects
Rectangle[] objects = detector.ProcessFrame(bitmap);

sw.Stop();
if (objects.Length > 0)
{
RectanglesMarker marker = new RectanglesMarker(objects, Color.Fuchsia);
bitmap = marker.Apply(bitmap);
}


ผลที่ได้












[ view entry ] ( 2736 views )   |  permalink  |  $star_image$star_image$star_image$star_image$star_image ( 3 / 1230 )
Recursive ใน JAVA สร้าง Sierpinski triangle 

fractal Sierpinski triangle โดยใช้ภาษา JAVA




import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TriangleFractal extends JPanel
{
public TriangleFractal()
{
setBackground(Color.black);
}
public void paint(Graphics g)
{
super.paint(g);
for(int i=0;i< 1000000;i++)
{
draw( g, 50, getWidth() - 50, getHeight()-50, getWidth()/2, getHeight()-25);
}
}

public void draw(Graphics g, double n,double w,double h,double x,double y)
{
if(n ==0)
{
g.setColor(Color.green);
g.fillRect((int)(x+Math.random()*w-w/2),(int) (y+Math.random()*h-h/2), 1, 1);
return;
}

double r= Math.random();
if(r < 1.0/3.0)
{
y = y - h /2;

}
else if(r < 2.0/3.0)
{
x = x - w/4;
}
else
{
x = x + w/4;
}
draw( g, n-1, w/2, h/2, x, y);
}
public static void main(String[] args)
{
JFrame fnt = new JFrame();

fnt.add(new TriangleFractal());
fnt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
fnt.setSize((int)width, (int)height);
fnt.setVisible(true);

}

}



 

จำนวนครั้งของ recursive n=1



จำนวนครั้งของ recursive n=2



จำนวนครั้งของ recursive n=3



จำนวนครั้งของ recursive n=4



จำนวนครั้งของ recursive n=5



จำนวนครั้งของ recursive n=6



จำนวนครั้งของ recursive n=7



จำนวนครั้งของ recursive n=30





EPT








[ view entry ] ( 1234 views )   |  permalink  |  $star_image$star_image$star_image$star_image$star_image ( 3 / 1057 )

<<First <Back | 1 | 2 | 3 | 4 | 5 | Next> Last>>