Posts tagged ‘code’

February 1, 2011

With or without button. Javascript Programming code


<html>
<body>

<input type=’button’ disabled=’disabled’ id=’javascr’ onclick=’alert(“Javascript enabled”)’ value=’Javascript’ />

<script type=”text/javascript”><!–

document.getElementById(“javascr”).disabled=false;

//–></script>

</body>
</html>

Advertisements
February 1, 2011

Finding the least common digit. C++ Programming code


#include<iostream.h>
int gcd(int a,int b)
{
int swap;
while(b>0)
{
swap=b;
b=a%b;
a=swap;
}
return a;
}
int nok(int *b,int n)
{
int result;
if(n==2)
{
return (*(b+0))*(*(b+1))/gcd(*(b+0),*(b+1));
}
else
result=nok(b,n-1);
return (*(b+n-1)*result)/gcd(*(b+n-1),result);
}
void enter(int m,int *b)
{

for(int i=0;i<m;i++)
{
cout<<“input element number:  “<<i+1<<”   “;
cin>>*(b+i);
cout<<“n”;
}
}
void print(int m ,int *b)
{
for (int i=0;i<m;i++)
{
cout<<“element number  “<<i+1<<”   “<<*(b+i)<<“n”;
}
}

February 1, 2011

Queen’s puzzle. C++ Programming code


#include <iostream.h>
const short int maxqn=20;
int solnum=0,x[maxqn+1];
bool raw[maxqn+1],md[maxqn*2],sd[maxqn*2+1],found=1;
//short int j=0;

void printsolution(int qn)
{
for(int l=1;l<=qn;l++)
{
cout<<x[l]<<“t”;
}
}

void try_first(short i,int qn)
{
short j=0;
do
{
j++;
if(raw[j]==true&&md[i-j+qn]==true&&sd[i+j]==true)
{
x[i]=j;
raw[j]=md[i-j+qn]=sd[i+j]=false;
if(i<qn) try_first(i+1,qn);
else {printsolution(qn);return;}
raw[j]=md[i-j+qn]=sd[i+j]=true;
}
}while(j<qn);

}
void try_all(int i,int qn)
{
short int j=0;
for(j=1;j<=qn;j++)
{
if(raw[j]&&md[i-j+qn]&&sd[i+j])
{
x[i]=j;
raw[j]=md[i-j+qn]=sd[i+j]=false;
if(i<qn) try_all(i+1,qn);
else {printsolution(qn);cout<<“n”;}
raw[j]=md[i-j+qn]=sd[i+j]=true;}
}

}
void main()
{
int qn,a=1;
do
{ cout<<“Number of Queens: “;
cin>>qn;
}while(qn<3||qn>maxqn);
for(int i=0;i<=maxqn;i++)
raw[i]=true;
for(int f=0;f<2*maxqn;f++)
md[f]=true;
for(int s=0;s<=2*maxqn;s++)
sd[s]=true;
try_first(a,qn);
cout<<“nn”;
try_all(a,qn);
}

February 1, 2011

Heap sorting.C++ Programming code


void Sift(int b[],int l,int r)
{
int i=l;
int x=b[i];
int j=2*i+1;
if((j<r)&&(b[j]<b[j+1])) j++;
while((j<=r)&&(x<b[j]))
{
b[i]=b[j];
i=j;
j=2*i+1;
if((j<r)&&(b[j]<b[j+1])) j++;
}
if(i!=l) b[i]=x;
}

void HeapSort(int m,int b[])
{
for(int i=m/2-1;i>=0;i–)
Sift(b,i,m-1);
for(i=m-1;i>0;)
{
int x=b[i];
b[i]=b[0];
b[0]=x;
Sift(b,0,–i);
}
}