วันพุธที่ 16 พฤศจิกายน พ.ศ. 2559

C++ leading in digit number


int leadDigit(int n)
{
int m[9];
int k = 100'000'000;
int i;
for (int i = 7; i >=0; i--)
{
m[i] = n % k / (k / 10);
k /= 10;
}
for ( i = 7; i>=0;i--)
{
if (m[i] == 0)
{
m[i] = m[i - 1];
}
else
{
return m[i];
}
}
}
int main()
{
system("cls");
int k;
cout << "Enter Number : ";
cin >> k;
cout<<"The leading digit of " <<k<<" is "<<leadDigit(k) << endl;
        return 0;
}


วันเสาร์ที่ 12 พฤศจิกายน พ.ศ. 2559

C++ LED NUMBER


#include <iostream>
#include <math.h>
#include <iomanip>
#include <time.h>
#include <thread>
#include <windows.h>
using namespace std;

int main(int argc, char** args)
{
char x[10][5][5] = { {
"xxxx",
"x  x",
"x  x",
"x  x",
"xxxx",
},

{
"   x",
"   x",
"   x",
"   x",
"   x",
},

{
 "xxxx",
 "   x",
 "xxxx",
 "x   ",
 "xxxx",
 },
 {
  "xxxx",
  "   x",
  "xxxx",
  "   x",
  "xxxx",
  },
  {
"x  x",
"x  x",
"xxxx",
"   x",
"   x",
},
{
"xxxx",
"x   ",
"xxxx",
"   x",
"xxxx",
},
{
"x   ",
"x   ",
"xxxx",
"x  x",
"xxxx",
},
{
"xxxx",
"   x",
"   x",
"   x",
"   x",
},
{
"xxxx",
"x  x",
"xxxx",
"x  x",
"xxxx",
},
{
"xxxx",
"x  x",
"xxxx",
"   x",
"   x",
} };
    int num, temp[12], i, count;
char c;
srand(time(NULL));
for (int k = 1; k < 1000; k++)
{
if (k == 999)
k = 1;
num = k;
count = 0;
i = 0;
while (num > 0)
{
temp[i] = num % 10;
num = num / 10;
i++;
}
count = i;
cout << endl;
for (int k = 0; k < 5; k++)
{
for (i = count - 1; i >= 0; i--)
{
cout << x[temp[i]][k] << "\t";
}
Sleep(10);
cout << endl;
}
cout << endl;
}
return 0;
}


C+ กระดานมหาสนุก


#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;


int main(int argc, char** args)
{
int i, j, temp,num;
char dir;
char x[4][4] = { {'a','b','c','d'},
{'e','f','g','h'},
{'i','j','k','l'},
{'m','n','o','p'} };
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << x[i][j] << "\t";
}
cout << endl;
}
while (1)
{
cout <<endl<< "Input number {0-3} and char {l,r,u,d} : ";
cin >> num >> dir;
if (num == -1)
{
break;
}
if (dir == 'l')
{
temp = x[num][0];
for (i = 0; i < 3; i++)
{
x[num][i] = x[num][i + 1];
}
x[num][3] = temp;
}
else if (dir == 'r')
{
temp = x[num][3];
for (i = 3; i>0; i--)
{
x[num][i] = x[num][i - 1];
}
x[num][0] = temp;
}
else if (dir == 'd')
{
temp = x[3][num];
for (i = 3; i>0; i--)
{
x[i][num] = x[i-1][num];
}
x[0][num] = temp;
}
else if (dir == 'u')
{
temp = x[0][num];
for (i = 0; i<3; i++)
{
x[i][num] = x[i + 1][num];
}
x[3][num] = temp;
}
cout << endl << endl;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << x[i][j] << "\t";
}
cout << endl;
}
}
return 0;
}

วันพฤหัสบดีที่ 10 พฤศจิกายน พ.ศ. 2559

C++ หาค่า PI ที่มีค่าเท่ากับ 3.141592xxx ว่ามีทั้งหมดกี่พจน์


int i = 2, j = 3, n = 1;
double target = 3.141592;
double result_neg = 1.0, result_pos = 1.0, result_all = 4.0;

while (n <= 3'000'000)
{
if (i - j == -1)
{
cout << fixed;
result_neg = (double)i / j;
result_all =  result_all * result_neg;
i += 2;
n++;
if (abs(target - result_all) <= 0.0000000001)
{
break;
}
}
if (i - j == 1)
{
cout << fixed;
result_pos = (double)i / j;
result_all =  result_all * result_pos;
j += 2;
n++;
if (abs(target - result_all) <= 0.0000000001)
{
break;
}
}
}
cout << setprecision(9);
cout<< result_all << endl;
cout <<endl<< n-1 << endl;




C++ หาจำนวนเฉพาะตัวที่ค่ามากที่สุดที่น้อยกว่า 100'000'000


void run(int upperBound)
{
int upperBoundSqureRoot = (int)sqrt((double)upperBound);
bool *isComposite = new bool[upperBound + 1];
memset(isComposite, 0, sizeof(bool)*(upperBound + 1));
for (int m = 2; m <= upperBoundSqureRoot; m++)
{
if (!isComposite[m])
{
cout << m << "\t";
for (int k = m*m; k <= upperBound; k += m)
{
isComposite[k] = true;
}
}
}
for (int m = upperBoundSqureRoot; m <= upperBound; m++)
{
if (!isComposite[m])
{
cout << m << "\t";
}
}
delete[] isComposite;
}
int main()
{
system("cls");
run(100'000'000);
         return 0;
}


วันพุธที่ 9 พฤศจิกายน พ.ศ. 2559

C++ เรียงตัวเลขจากน้อยไปหามาก


int a[10];
for (int i = 0; i < 10; i++)
{
cout << "Enter Input a#" << i + 1 << " : ";
cin >> a[i];
}
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 9; i++)
{
if (a[i] > a[i + 1])
{
int temp;
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
for (int i = 0; i < 10; i++)
{
cout << a[i] << endl;
}


C++ สร้าง array ขนาด 20 ช่อง มีกี่จำนวนที่ซ้ำกัน และจำนวนอะไรบ้างที่ซ้ำกัน


#define max 20
int a[max];
int b[max];
int count = 0;
//bubble sort
srand(time(NULL));
for (int i = 0; i < max; i++)
{
cout << "Enter a[" << i + 1 << "] : ";
cin >> a[i];
}
for (int i = 0; i < max; i++)
{
if (i % 4 == 0)
cout << endl<<endl;
cout << a[i] << "\t";
}
for (int j = 0; j < max; j++)
{
for (int i = 0; i < max - 1; i++)
{
if (a[i] > a[i + 1])
{
int temp;
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
for (int i = 0; i < max; i++)
{
if (a[i] < 999)
{
b[i] = 1;
for (int j = i + 1; j < max; j++)
{
if (a[i] < 999)
{
if (a[i] == a[j])
{
b[i] = b[i] + 1;
a[j] = 999;
}
}
}
}
}
cout <<endl<< "--------------------------";
cout << endl<<endl;
for (int i = 0; i < max; i++)
{
if (a[i] < 999 && b[i] != 1)
count++;
}
cout << count << endl;
cout << endl;
for (int i = 0; i < max; i++)
{
if (a[i] < 999 && b[i] != 1)
cout << a[i] << " = " << b[i] << endl;
}