Skip to content

Instantly share code, notes, and snippets.

@Shun2439
Last active December 18, 2023 07:36
Show Gist options
  • Save Shun2439/90e8836d16e4b275e8c1c29daad2091e to your computer and use it in GitHub Desktop.
Save Shun2439/90e8836d16e4b275e8c1c29daad2091e to your computer and use it in GitHub Desktop.
garbages
#include <iostream>
#include <vector>
using namespace std;
vector<int> a = {1, 14, 32, 51, 51, 51, 243, 419, 750, 910};
// 目的の値 key の index を返すようにする (ない場合は -1)
int binary_search(int key)
{
int left = 0, right = (int)a.size() - 1; // 配列 a の左端と右端
while (right >= left)
{
int mid = left + (right - left) / 2; // 区間の真ん中
if (a[mid] == key) return mid;
else if (a[mid] > key) right = mid - 1;
else if (a[mid] < key) left = mid + 1;
}
return -1;
}
int main()
{
cout << binary_search(51) << endl; // a[4] = 51 (他に a[3], a[5] も)
cout << binary_search(1) << endl; // a[0] = 1
cout << binary_search(910) << endl; // a[9] = 910
cout << binary_search(52) << endl; // -1
cout << binary_search(0) << endl; // -1
cout << binary_search(911) << endl; // -1
}
#include <cmath>
#include <iostream>
#include <fstream>
#include <gmpxx.h>
using namespace std;
struct PQT
{
mpz_class P, Q, T;
};
class Chudnovsky
{
// Declaration
mpz_class A, B, C, D, E, C3_24; // GMP Integer
int DIGITS, PREC, N; // Integer
double DIGITS_PER_TERM; // Long
clock_t t0, t1, t2; // Time
PQT compPQT(int n1, int n2); // Computer PQT (by BSA)
public:
Chudnovsky(); // Constructor
void compPi(); // Compute PI
};
/*
* Constructor
*/
Chudnovsky::Chudnovsky()
{
// Constants
DIGITS = 1000000;
A = 13591409;
B = 545140134;
C = 640320;
D = 426880;
E = 10005;
DIGITS_PER_TERM = 14.1816474627254776555; // = log(53360^3) / log(10)
C3_24 = C * C * C / 24;
N = DIGITS / DIGITS_PER_TERM;
PREC = DIGITS * log2(10);
}
/*
* Compute PQT (by Binary Splitting Algorithm)
*/
PQT Chudnovsky::compPQT(int n1, int n2)
{
int m;
PQT res;
if (n1 + 1 == n2) {
res.P = (2 * n2 - 1);
res.P *= (6 * n2 - 1);
res.P *= (6 * n2 - 5);
res.Q = C3_24 * n2 * n2 * n2;
res.T = (A + B * n2) * res.P;
if ((n2 & 1) == 1) res.T = - res.T;
} else {
m = (n1 + n2) / 2;
PQT res1 = compPQT(n1, m);
PQT res2 = compPQT(m, n2);
res.P = res1.P * res2.P;
res.Q = res1.Q * res2.Q;
res.T = res1.T * res2.Q + res1.P * res2.T;
}
return res;
}
/*
* Compute PI
*/
void Chudnovsky::compPi()
{
cout << "**** PI Computation ( " << DIGITS << " digits )" << endl;
// Time (start)
t0 = clock();
// Compute Pi
PQT PQT = compPQT(0, N);
mpf_class pi(0, PREC);
pi = D * sqrt((mpf_class)E) * PQT.Q;
pi /= (A * PQT.Q + PQT.T);
// Time (end of computation)
t1 = clock();
cout << "TIME (COMPUTE): "
<< (double)(t1 - t0) / CLOCKS_PER_SEC
<< " seconds." << endl;
// Output
ofstream ofs ("pi.txt");
ofs.precision(DIGITS + 1);
ofs << pi << endl;
// Time (end of writing)
t2 = clock();
cout << "TIME (WRITE) : "
<< (double)(t2 - t1) / CLOCKS_PER_SEC
<< " seconds." << endl;
}
int main()
{
try
{
// Instantiation
Chudnovsky objMain;
// Compute PI
objMain.compPi();
}
catch (...) {
cout << "ERROR!" << endl;
return -1;
}
return 0;
}
S = str(input("hex:"))
i = int(s, 16)
print(str(i))
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game() {
int inputNumber = 0, answer = 0, isContinue = 0, counter = 0;
srand((unsigned int)time(NULL));
answer = rand() % 100;
printf("%d", answer);
while (isContinue == 0) {
counter++;
printf("数字を当ててください。:");
scanf("%d", &inputNumber);
if (inputNumber == answer) {
printf("正解!\nあなたが推測した回数は%d回です。\n", counter);
isContinue = 1;
} else if (answer < inputNumber) {
printf("もっと小さいです。\n");
} else if (inputNumber < answer) {
printf("もっと大きいです。\n");
}
}
}
int main(void) {
int select;
while (1) {
printf("ゲームをしますか?\nYes:1\nNo:2\n:");
scanf("%d", &select);
if (select == 1) {
game();
} else {
return (0);
}
}
}
・ソ#define circle_test
#ifdef Pythagorean_theorem
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int main()
{
/**
莠檎せ髢薙・霍晞屬縺ッ繝斐ち繧エ繝ゥ繧ケ縺ョ螳夂炊繧剃スソ縺・→邁。蜊倥↓豎ゅa繧峨l繧・
**/
vector<int> A(2, 0), B(2, 0);
/**
example:
A = (25, 80)
B = (55, 40)
answer = 50
**/
cout << "Input A_x:";
cin >> A[0];
cout << "Input A_y:";
cin >> A[1];
cout << "Input B_x:";
cin >> B[0];
cout << "Input B_y:";
cin >> B[1];
cout << "Distance between A, B is " << sqrt(pow(B[0] - A[0], 2) + pow(B[1] - A[1], 2)) << endl;
return (0);
}
#endif
#ifdef determine_rectangular_triangle
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int main()
{
/**
input:
20 50
100 90
70 150
**/
vector<int> A(2, 0), B(2, 0), C(2, 0);
cin >> A[0] >> A[1];
cin >> B[0] >> B[1];
cin >> C[0] >> C[1];
double AB = sqrt(pow(B[0] - A[0], 2) + pow(B[1] - A[1], 2)),
BC = sqrt(pow(C[0] - B[0], 2) + pow(C[1] - B[1], 2)),
AC = sqrt(pow(C[0] - A[0], 2) + pow(C[1] - A[1], 2));
if (pow(AB, 2) + pow(BC, 2) == pow(AC, 2))
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return (0);
}
#endif
#ifdef calc_3D_coordinate
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int main()
{
vector<int> A(3, 0), B(3, 0);
cin >> A[0] >> A[1] >> A[2];
cin >> B[0] >> B[1] >> B[2];
cout << "center: " << (A[0] + B[0]) / 2 << ", " << (A[1] + B[1]) / 2 << ", " << (A[2] + B[2]) / 2 << endl;
return (0);
}
#endif
#ifdef circle_test
#include <iostream>
#include <vector>
#include <math.h>
struct circle;
struct sphere;
using namespace std;
int main()
{
return (0);
}
struct circle
{
float center[2];
float radius;
};
struct sphere
{
float center[3];
float radius;
};
#endif
#!/bin/bash
#sleep $(($RANDOM % 10))
printf "Input seconds\n:"
read time
for ((i = 0; i < $time; i++)); do
sleep 1
echo $(($i + 1))
done
#include <stdio.h>
void sortNum(int *a, int *b, int *c);
int main(void)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
printf("a=%d, b=%d, c=%d\\n", a, b, c);
sortNum(&a, &b, &c);
printf("a=%d, b=%d, c=%d\\n", a, b, c);
return (0);
}
void sortNum(int *a, int *b, int *c)
{
int tmp;
if(*a < *b)
{
tmp = *a;
*a = *b;
*b = tmp;
}
if(*a < *c)
{
tmp = *a;
*a = *c;
*c = tmp;
}
if(*b < *c)
{
tmp = *b;
*b = *c;
*c = tmp;
}
}
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
int32_t main(void)
{
int32_t a = 0, b = 0, tmp;
bool get_ans = false;
printf("Input first number:");
scanf("%d", &a);
printf("Input second number:");
scanf("%d", &b);
while (!get_ans)
{
if (a % b != 0)
{
tmp = b;
b = a % b;
a = tmp;
}
else
{
get_ans = true;
}
}
printf("GCD:%d", b);
return (0);
}
def combination():
import math
n = int(input("n:"))
r = int(input("r:"))
n1 = math.factorial(n) #nの階乗を求める
r1 = math.factorial(r)
a = n -r
a1 = math.factorial(n-r)
print(n1/(r1 * a1))
combination()
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdint>
using namespace std;
unsigned long long factorial(uint32_t n);
int32_t main()
{
uint32_t n = 0;
cout << "階乗を求めます\n" << "Input:";
cin >> n;
cout << "Output:" << factorial(n) << endl;
return 0;
}
unsigned long long factorial(uint32_t n)
{
if (n > 0)
//recursive function call
return n * factorial(n - 1);
//else
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment