关于X/N=int(H/N)*65536+[rem(H/N)*65536+L]/N公式的例子

本文介绍了一个16位除法的汇编语言实现案例。通过具体的代码示例展示了如何利用寄存器进行除法运算,并详细解释了除法子程序的工作原理。对于学习汇编语言和底层运算具有较高的参考价值。

assume  cs:codeseg

 

stackseg segment

 

  dw 8 dup (0)

 

stackseg ends

 

codeseg  segment

 

start:  mov ax,stack

           mov ss,ax

           mov sp,10H

           mov ax,4240h;被除数为:000F4240H

           mov dx,0FH

           mov cx,0EH;除数为0EH

           call divdw;关于X/N=int(H/N)*65536+[rem(H/N)*65536+L]/N的实现

 

           mov ax,4c00h

           int 21h

 

divdw:push bx;bx中可能含有重要的数据

           push ax

           push dx

           mov dx,0

           pop ax;因为此时要做的是16位的除法,所以要设置AX,DX。根据公式这里做的是被除数的高位除以除数,被除数的高位为0FH,放到在16位除法中就应该是dx=0000h,ax=000fh

           div cx;(ax)=商,(dx)=余数

           mov bx,ax;将商送入bx中存贮

           pop ax;此时有dx(此时已经存放了高位除法的余数了)和ax构成了新的被除数了,即:rem(H/N)*65536+L

           div cx ;(ax)=最终的商低位

           mov cx,dx;(cx)=最终结果的余数

           mov dx,bx;(dx)=最终结果的商高位位

           ;商由dx和ax组成,余数为:cx

           pop bx

           ret

codeseg ends

end start

#include <stdio.h> #include <string.h> #include <stdbool.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <limits.h> #define eps 1e-9 //C3F //unsigned int F[14000010]; //E3D //unsigned long long arr[7500005]; //E3E //double arr1[1000000]; //double arr2[1000000]; //double Arr[1000000]; //E3F //int arr[1000010] = { 0 }; //int arrn[10010] = { 0 }; //E3G? //int arr[100][100]; //E3H! //int is_ry(int y) //{ // int result; // result = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0); // return result; //} //int D(int y, int m) //{ // int D[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; // int ds = D[m]; // if (m == 2 && is_ry(y)) // { // D[2] = 29; // return 29; // } // return ds; //} //E3M? //int Max(int a, int b) //{ // int result; // if (a >= b) // { // result = a; // } // else // { // result = b; // } // return result; //} //int Min(int a, int b) //{ // int result; // if (a <= b) // { // result = a; // } // else // { // result = b; // } // return result; //} //E3P?? //int arr[500005] = { 0 }; //E3O!! //long long read() //{ // long long x = 0, f = 1; // long long c = getchar(); // while (c > '9' || c < '0') // { // if (c == '-') // f = -f; // c = getchar(); // } // while (c >= '0' && c <= '9') // x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar(); // return x * f; //} //long long L(long long l, long long r) //{ // if (l == r) // { // return l; // } // while ((l | (l + 1)) <= r) // { // l = l | (l + 1); // } // return l | r; //} //E3W long long popcnt(long long n) { long long sum = 0; while (n) { n = n & (n - 1); sum++; } return sum; } int main() { //C3A //long long T; //scanf("%lld", &T); //unsigned int a, b; //for (int i = 0; i < T; i++) //{ // scanf("%u %u", &a, &b); // unsigned int n1 = ~a; // unsigned int n2 = a & b; // unsigned int n3 = a | b; // unsigned int n4 = a ^ b; // unsigned int n5 = a << b; // unsigned int n6 = a >> b; // printf("%u %u %u %u %u %u\n", n1, n2, n3, n4, n5, n6); //} //return 0; //C3B //long long n; //scanf_s("%lld", &n); //printf("0 "); //for (int k = 1; k <= n; k++) //{ // int sum = 0; // int arr[101]; // int f = floor(log2(k)); // int K = k; // for (int i = 0; i <= f; i++) // { // int N = K & 1; // arr[i] = N; // K = K >> 1; // } // for (int j = 0; j <= f; j++) // { // if (arr[j] == 1) // { // sum++; // } // } // printf("%d ", sum); //} //return 0; //C3C //int nn; //scanf_s("%d", &nn); //int n; //for (int i = 0; i < nn; i++) //{ // int arr[101] = {0}; // unsigned long long n; // scanf_s("%llu", &n); // unsigned long long f = floor(log2(n)); // unsigned long long N = n; // for (int i = 0; i <= f; i++) // { // unsigned long long N = n & 1; // arr[i] = N; // n = n >> 1; // } // for (int j = 63; j >= 0; j=j-4) // { // printf("%d%d%d%d ", arr[j],arr[j-1],arr[j-2],arr[j-3]); // } // printf("\n"); //} //return 0; //C3D //int n; //unsigned int sum=0; //scanf_s("%d", &n); //for (int i = 0; i < n; i++) //{ // sum = 0; // int arr[101] = {0}; // unsigned int n; // scanf_s("%u", &n); // int f = floor(log2(n)); // unsigned int N = n; // for (int i = 0; i <= f; i++) // { // unsigned int N = n & 1; // arr[i] = N; // n = n >> 1; // } // for (int j = 0; j <= 32; j++) // { // sum = sum + pow(2, j) * arr[31-j]; // } // printf("%u\n", sum); //} //C3E //int nn; //scanf_s("%d", &nn); //for (int i = 0; i < nn; i++) //{ // int arr[101]; // int n; // scanf_s("%d", &n); // int f = floor(log2(n)); // int N = n; // for (int i = 0; i <= f; i++) // { // int N = n & 1; // arr[i] = N; // n = n >> 1; // } // int k = 0; // int Arr[101]; // for (int j = f; j >= 0; j--) // { // if (arr[j] == 1) // { // Arr[k] = j; // k++; // } // } // for (int t = 0; t < k; t++) // { // printf("%.0lf ", pow(2, Arr[t])); // } // printf("\n"); //} //return 0; //C3F //int n, q; //scanf_s("%d%u%d", &n, &F[0], &q); //int k; //for (int i = 1; i <= n; i++) //{ // unsigned int x = F[i - 1]; // x ^= x << 13; // x ^= x >> 17; // x ^= x << 5; // F[i] = x; //} //while (q--) //{ // scanf_s("%d", &k); // printf("%u\n", F[k]); //} //return 0; //C3G //int n; //int a; //int b=0; //scanf_s("%d", &n); //for (int i = 0; i < n; i++) //{ // scanf_s("%d", &a); // b = b ^ a; //} //printf("%d\n", b); //return 0; //E3A //int a, b, c, x; //scanf_s("%d%d%d%d", &a, &b, &c, &x); //int A = a * x * x + b * x + c; //printf("%d", A); //return 0; //E3B //int n; //scanf_s("%d", &n); //int i = n; //for (int i = n; i > 1; i--) //{ // printf("%d bottles of beer on the wall,\n", i); // printf("%d bottles of beer.\n", i); // printf("Take one down and pass it around,\n"); // if (i == 2) // { // printf("now there's %d more bottle of beer on the wall!\n", i-1); // } // else // printf("now there's %d more bottles of beer on the wall!\n", i - 1); //} // printf("1 bottle of beer on the wall,\n"); // printf("1 bottle of beer.\n"); // printf("Take one down and pass it around,\n"); // printf("there's no more bottles of beer on the wall!\n"); //return 0; //E3C //unsigned long long a, b; //while (scanf_s("%llu%llu", &a, &b) != EOF) //{ // unsigned long long i = 0; // unsigned long long t = 0; // for (i = 0; i < 64; i++) // { // unsigned long long ai = a >> i; // unsigned long long bi = b >> i; // unsigned long long Ai = ai & 1; // unsigned long long Bi = bi & 1; // if (Ai - Bi == 1) // { // t = t | (1ULL << i); // } // } // printf("%llu\n", t); //} //return 0; //E3D //int T, n; //scanf_s("%d", &T); //arr[1] = 1; //unsigned long long temp=1; //for (int j = 2; j <= 7.5e6; j++) //{ // temp = (temp * j) % 998244353; // arr[j] = temp; //} //for (int i = 1; i <= T; i++) //{ // scanf_s("%d", &n); // printf("%llu\n", arr[n]); //} //return 0; //E3E //int T; //scanf_s("%d", &T); //double sum1 = 0.000000; //double sum2 = 0.000000; //double sign = 1.0; //for (int i = 1; i <= 100000; i++) //{ // sum1 = sum1 + sign *1.000000/ (2 * i - 1); // sign = -sign; // arr1[i] = sum1; //} //for (int i = 1; i <= 100000; i++) //{ // sum2 = sum2 + 1.000000 / (long long)((long long)(2 * i - 1) * (long long)(2 * i - 1)); // arr2[i] = sum2; //} //for (int i = 1; i <= 100000; i++) //{ // Arr[i] = arr1[i] * 4 - sqrt(8 * arr2[i]); //} //int arrn[1000]; //int arrN[1000]; //for (int j = 1; j <= T; j++) //{ // scanf_s("%d", &arrn[j]); // arrN[j] = arrn[j]; //} //for (int k = 1; k <= T; k++) //{ // printf("%.6lf\n", fabs(Arr[arrN[k]])); //} //return 0; //E3F? //int n; //int a; //scanf_s("%d", &n); //for (int i = 1; i <= n; i++) //{ // scanf_s("%d", &a); // arrn[i] = a; //} //for (int j = 1; j <= 10000; j++) //{ // for (int k = 1; k <= 1000000; k++) // { // if (j == arrn[k]) // { // arr[j]++; // } // } // if (arr[j] != 0) // { // printf("%d : %d\n", j, arr[j]); // } //} //return 0; //E3F //int n; //scanf_s("%d", &n); //for (int i = 1; i <= n; i++) //{ // scanf_s("%d",&arr[i]); // arrn[arr[i]]++; //} //for (int i = 1; i <= 10000; i++) //{ // if (arrn[i] > 0) // { // printf("%d : %d\n", i, arrn[i]); // } //} //E3G? //int m, n; //int x0, y0; //int x, y; //scanf_s("%d%d", &n, &m); //scanf_s("%d%d", &x0, &y0); //scanf_s("%d%d", &x, &y); //for (int xi = 1; xi <= n; xi++) //{ // for (int yi = 1; yi <= m; yi++) // { // printf("0"); // } // printf("0\n"); //} //E3G! //long long n, m; //long long x0, y0; //long long x, y; //scanf_s("%lld%lld", &n,& m); //scanf_s("%lld%lld", &x0, &y0); //scanf_s("%lld%lld", &x, &y); //for (long long i = x - 2; i <= x + 2; i++) //{ // for (long long j = y - 2; j <= y + 2; j++) // { // if (i == x0 && j == y0) // { // printf("+"); // } // else if (i == x0 && j != y0) // { // printf("-"); // } // else if (i != x0 && j == y0) // { // printf("|"); // } // else if (i - x0 == j - y0) // { // printf("\\"); // } // else if (i - x0 == y0 - j) // { // printf("/"); // } // else if (i - x0 < 0 && j - y0>0 && x0 - i > j - y0) // { // printf("A"); // } // else if (i < x0 && j > y0 && x0 - i < j - y0) // { // printf("B"); // } // else if (i > x0 && j > y0 && i - x0 < j - y0) // { // printf("C"); // } // else if (i > x0 && j > y0 && i - x0 > j - y0) // { // printf("D"); // } // else if (i > x0 && j < y0 && i - x0 > y0 - j) // { // printf("E"); // } // else if (i > x0 && j < y0 && i - x0 < y0 - j) // { // printf("F"); // } // else if (i < x0 && j < y0 && i - x0 < j - y0) // { // printf("H"); // } // else // { // printf("G"); // } // } // printf("\n"); //} //return 0; //E3H? //int n; //scanf_s("%d", &n); //int y, m, d, h, mi; //for (int i = 1; i <= n; i++) //{ // scanf_s("%d.%d.%d %d:%d", &y, &m, &d, &h, &mi); //} //E3H! //int n; //scanf_s("%d", &n); //while (n--) //{ // int y, m, d, h, mi; // scanf_s("%d.%d.%d %d:%d", &y, &m, &d, &h, &mi); // while (mi >= 60) // { // mi = mi - 60; // h++; // } // while (h >= 24) // { // h = h - 24; // d++; // } // while (1) // { // while (m > 12) // { // m = m - 12; // y++; // } // int DD = D(y, m); // if (d <= DD) // { // break; // } // d = d - DD; // m++; // } // if (y >= 10000) // { // printf("%05d.%02d.%02d %02d:%02d\n", y, m, d, h, mi); // } // else // { // printf("%04d.%02d.%02d %02d:%02d\n", y, m, d, h, mi); // } //} //return 0; //E3I? //long long a, b, n; //scanf_s("%lld%lld%lld", &a, &b, &n); //long long ZHENG = a / b; //long long XIAO = a % b; //long long arr[110] = {0}; //for (int i = 1; i <= n + 1; i++) //{ // XIAO = XIAO * 10; // long long A = XIAO / b; // XIAO = XIAO % b; // arr[i] = A; //} //if (arr[n + 1] >= 5) //{ // arr[n]++; // for (int i = n; i >= 2; i--) // { // if (arr[i] >= 10) // { // arr[i] = arr[i] - 10; // arr[i - 1]++; // } // } // if (arr[1] >= 10) // { // arr[1] = arr[1] - 10; // ZHENG++; // } //} //printf("%lld.", ZHENG); //for (int i = 1; i <= n; i++) //{ // printf("%lld", arr[i]); //} //printf("\n"); //return 0; //E3K //int a, b; //scanf_s("%d%d", &a, &b); //double down= floor(a * 1.000000 / b); //double up = ceil(a * 1.000000 / b); //if (down == 0.0) //{ // down = 0.0; //} //if (up == 0.0) //{ // up = 0.0; //} //printf("%.lf %.lf", down, up); //return 0; //E3L? //int T; //int a; //scanf_s("%d", &T); //for (int i = 1; i <= T; i++) //{ // scanf_s("%d", &a); // if (a % 6 == 0) // { // printf("Tom wins\n"); // } // else // { // printf("Jerry wins\n"); // } //} //return 0; //E3M? //int T; //scanf_s("%d", &T); //int a1, b1, c1, d1, a2, b2, c2, d2; //for (int i = 1; i <= T; i++) //{ // scanf_s("%d%d%d%d%d%d%d%d", &a1, &b1, &c1, &d1, &a2, &b2, &c2, &d2); // float X1 = (a1 + c1) * 1.0f / 2; // float X2 = (a2 + c2) * 1.0f / 2; // float Y1 = (d1 + b1) * 1.0f / 2; // float Y2 = (d2 + b2) * 1.0f / 2; // float rx1 = fabs(a1 - c1) * 1.0f / 2; // float rx2 = fabs(a2 - c2) * 1.0f / 2; // float ry1 = fabs(d1 - b1) * 1.0f / 2; // float ry2 = fabs(d2 - b2) * 1.0f / 2; // float dX = fabs(X1 - X2)*1.0f; // float dY = fabs(Y1 - Y2)*1.0f; // float S1 = 4 * rx1 * ry1*1.0f; // float S2 = 4 * rx2 * ry2*1.0f; // float SS = S1 + S2; // float TT = fabs(Max(a1, a2) - Min(c1, c2)) * fabs(Min(d1, d2) - Max(b1, b2)); // if (dX < rx1 + rx2 && dY < ry1 + ry2) // { // printf("%.f %.f\n", TT, SS - TT); // } // else // { // printf("%.f %.f\n", 0, SS); // } //} //return 0; //E3M! //int T; //scanf_s("%d", &T); //int a1, b1, c1, d1, a2, b2, c2, d2; //for (int i = 1; i <= T; i++) //{ // scanf_s("%d%d%d%d%d%d%d%d", &a1, &b1, &c1, &d1, &a2, &b2, &c2, &d2); // int S1 = (c1 - a1) * (d1 - b1); // int S2 = (c2 - a2) * (d2 - b2); // int SS = S1 + S2; // int dxa = Max(a1, a2); // int dxb = Max(b1, b2); // int dxc = Min(c1, c2); // int dxd = Min(d1, d2); // int TT = Max(0, dxc - dxa) * Max(0, dxd - dxb); // printf("%d %d\n", TT, SS - TT); //} //return 0; //E3N? //int T; //int a, b, c; //int x1, x2, y1, y2; //double k, m; //double delta; //double X1, X2, Y1, Y2; //scanf_s("%d", &T); //for (int i = 1; i <= T; i++) //{ // scanf_s("%d%d%d", &a, &b, &c); // scanf_s("%d%d", &x1, &y1); // scanf_s("%d%d", &x2, &y2); // if (x1 != x2) // { // k = (y1 - y2) * 1.0 / (x1 - x2); // m = -(y1 - y2) * 1.0 / (x1 - x2) * x1 + y1; // } // if (a != 0 && x1 != x2) // { // delta = (b - k) * (b - k) - 4 * a * (c - m); // if (delta > eps) // { // X1 = ((k - b) + sqrt(delta)) * 1.0 / (2 * a); // X2 = ((k - b) - sqrt(delta)) * 1.0 / (2 * a); // Y1 = a * X1 * X1 + b * X1 + c; // Y2 = a * X2 * X2 + b * X2 + c; // printf("2 "); // if (X1 < X2) // { // printf("(%.2lf,%.2lf) (%.2lf,%.2lf)\n", X1, Y1, X2, Y2); // } // else // { // printf("(%.2lf,%.2lf) (%.2lf,%.2lf)\n", X2, Y2, X1, Y1); // } // } // else if (fabs(delta) <= eps) // { // X1 = ((k - b) + sqrt(delta)) * 1.0 / (2 * a); // Y1 = a * X1 * X1 + b * X1 + c; // printf("1 "); // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // else if (delta < -eps) // { // printf("0\n"); // } // } // else if (a != 0 && x1 == x2) // { // printf("1 "); // X1 = x1; // Y1 = a * x1 * x1 + b * x1 + c; // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // else if (a == 0 && b != 0 && x1 == x2) // { // printf("1 "); // X1 = x1; // Y1 = b * x1 + c; // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // else if (a == 0 && b != 0 && x1 != x2) // { // if (fabs(k - b) < eps && fabs(c - m) < eps) // { // printf("-1\n"); // } // else if (fabs(k - b) < eps && fabs(c - m) > eps) // { // printf("0\n"); // } // else // { // printf("1 "); // X1 = (c - m) * 1.0 / (k - b); // Y1 = b * X1 + c; // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // } // else if (a == 0 && b == 0 && x1 == x2) // { // printf("1 "); // X1 = x1; // Y1 = c; // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // else if (a == 0 && b == 0 && y1 != y2 && x1 != x2) // { // printf("1 "); // X1 = (c - m) * 1.0 / (k); // Y1 = c; // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // else if (a == 0 && b == 0 && y1 == y2 && y1 == c) // { // printf("-1\n"); // } // else if (a == 0 && b == 0 && y1 == y2 && y1 != c) // { // printf("0\n"); // } //} //return 0; //E3O? //long long T = read(); //for (int i = 1; i <= T; i++) //{ // long long t = 0; // long long l = read(); // long long r = read(); // for (int i = l; i <= r; i++) // { // t = t | i; // } // printf("%lld\n", t); //} //return 0; //E3O! //long long T = read(); //for (int i = 1; i <= T; i++) //{ // long long l = read(); // long long r = read(); // long long t = L(l, r); // printf("%lld\n", t); //} //return 0; //E3P?? //int n; //scanf_s("%d", &n); //for (int i = 1; i <= n; i++) //{ // scanf_s("%d", &arr[i]); //} //for (int i = 1; i <= n; i++) //{ // int t = n; // int tmp = arr[i]; // for (int j = n; j >= i; j--) // { // if (arr[j] > tmp) // { // t = j; // tmp = arr[j]; // } // } // printf("%d ", t); //} //return 0; //E3U?? //long long a; //while (scanf_s("%lld", &a) != EOF) //{ // if (a == 2) // { // printf("Ahh\n"); // } // else // { // printf("Hoy!\n"); // } //} //return 0; //E3W int T; long long n; scanf_s("%d", &T); while (T--) { long long Sum = 0; scanf_s("%lld", &n); for (long long i = 0; i < n; i++) { Sum = Sum + popcnt(i); } printf("%lld\n",Sum); }TLE
10-03
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdbool.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <limits.h> #define EPS 1e-9 #define MOD 100000000 #define in int #define ll long long #define fl float #define db double #define ch char #define vd void //库函数解析 vd* memchr_exp(const vd* str, in c, size_t n) { //在参数str所指向的字符串的前n个字节中 //检索第一次出现字符c(+无符号)的位置 const unsigned ch* ptr = (const unsigned ch*)str; //将vd*指针转换为const unsigned ch*指针 unsigned ch uc = (unsigned ch)c; //将传入的in c显式转换为unsigned ch for (size_t i = 0; i < n; i++) //i是当前偏移量(索引) { if (ptr[i] == uc) //指针ptr等价于数组ptr //判断当前位置的字节是否等于目标字节uc { return (vd*)(ptr + i); //找到则返回偏移后地址 //匹配标准memchr的vd } } return NULL; //未找到则返回空指针 } //in main() //{ // ch str[] = "Hello, world!"; // //定义字符数组并进行初始化 // ch* result = (ch*)memchr_exp(str, 'w', sizeof(str)); // //调用函数,以数组大小14为检索范围(包括任意位置的\0) // //返回值强行转换为ch*以进行后续处理,*实现地址的传递 // if (result != NULL) // //若函数返回的不是空指针NULL // { // printf("字符'w'首次出现在索引:%d\n", result - str); // //进行地址运算,result整型形式表示地址,str表首地址 // //result经过函数操作后被赋值为'w'所在的地址 // //地址差运算值即对于数组str的下标值 // printf("子字符串:%s\n", result); // //此时字符串形式的result相当于一个字符集 // //result在指针整型的地址值做字符集首地址 // //以此输出首地址后的含被检索到的'w'的字符串 // } // else // { // printf("未找到字符'w'\n"); // } // result = (ch*)memchr_exp(str, 'x', sizeof(str)); // if (result == NULL) // { // printf("字符'x'未找到\n"); // } // return 0; //} //自定义函数 ll factorial1 (in n); ll factorial2 (in n); ll fibonacci1 (in n); ll fibonacci2 (in n); ll fibonacci2_memo (in n); ll permute (in n, in k); ll combine (in n, in k); ll pow_mod (ll base, ll exp); in digit_change (in n, in digit2); in bin_to_dec (in bin); in gcd (in a, in b); in lcm (in a, in b); in is_prime (in n); in is_palindrome (const ch* s); in contains_digit1 (in n, in p); in contains_digit2 (const ch* num_str, in p); in is_leap (in year); in get_days_of_month(in year, in month); in get_week_day (in year, in month, in day); in turn_digits (in n); vd extract_digits (in num, in digits[]); vd string_frequency (const ch* str); vd swap (in* a, in* b); vd bubble_sort (in* arr, size_t n); vd quick_sort (in arr[], in l, in r); vd permute (in arr[], in start, in end); vd reverse_arr (in arr[], in n); vd scanf_arr (in arr[], in n); vd print_arr (in arr[], in n); in sum_arr (in arr[], in n); in max_index (in arr[], in n); in binary_search (in arr[], in n, in x); db distance_2d (db x1, db y1, db x2, db y2); ll triange_area_2 (ll x1, ll y1, ll x2, ll y2, ll x3, ll y3); ll n_triangle_area_2(in n, ll x[], ll y[]); //求某个数的阶乘(非递归) ll factorial1(in n) { if (n < 0) return -1; if (n > 20) return -1; ll result = 1; for (in i = 2; i <= n; i++) { result *= i; } return result; } //求某个数的阶乘(递归) ll factorial2(in n) { if (n <= 1) { return 1; } return n * factorial2(n - 1); } //求斐波那契数列第某个数(非递归) ll fibonacci1(in n) { if (n == 0) return 0; if (n == 1) return 1; ll prev = 0; ll curr = 1; ll next; for (in i = 2; i <= n; i++) { next = prev + curr; prev = curr; curr = next; } return curr; } //求斐波那契数列第某个数(递归) ll fibonacci2(in n) { if (n == 1 || n == 2) { return 1; } return fibonacci2(n - 1) + fibonacci2(n - 2); } //求斐波那契数列第某个数(递归+优化) ll memo[1000] = { 0 }; ll fibonacci2_memo(in n) { if (n == 1 || n == 2) { return 1; } if (memo[n] != 0) { return memo[n]; } memo[n] = fibonacci2_memo(n - 1) + fibonacci2_memo(n - 2); return memo[n]; } //求排列数P(n数,排k) ll permute(in n, in k) { if (k < 0 || n < 0 || k > n) { return -1; } ll result = 1; for (in i = n - k + 1; i <= n; i++) { result *= i; } return result; } //求组合数C(n数,组k) ll combine(in n, in k) { if (k < 0 || n < 0 || k > n) { return -1; } if (k > n - k) { k = n - k; } ll result = 1; for (in i = 0; i < k; i++) { result = result * (n - i) / (i + 1); } return result; } //求快速幂(模幂运算) ll pow_mod(ll base, ll exp) { ll result = 1; while (exp > 0) { if (exp & 1) { result = (result * base) % MOD; } base = (base * base) % MOD; exp >>= 1; } return result; } //求10进制转<10进制(待拓展) in digit_change(in n, in digit2) { in rem = n; in mod[100]; in i = 0; while (rem > 0) { mod[i] = rem % digit2; rem = rem / digit2; i++; } in result = 0; in ten = 1; for (in j = 0; j < i; j++) { result += mod[j] * ten; ten = 10 * ten; } return result; } //求2进制数转10进制数 in bin_to_dec(in bin) { if (bin == 0) { return 0; } if (bin < 0) { return -1; } in decimal = 0; in base = 1; while (bin > 0) { in digit = bin % 10; if (digit != 0 && digit != 1) { return -1; } decimal += digit * base; base *= 2; bin /= 10; } return decimal; } //求两个数的最大公因数 in gcd(in a, in b) { if (a < 0) a = -a; if (b < 0) b = -b; while (b != 0) { in temp = b; b = a % b; a = temp; } return a; } //求两个数的最小公倍数 in lcm(in a, in b) { return a / gcd(a, b) * b; } //求某个数是否为素数 in is_prime(in n) { if (n <= 1) { return 0; } else if (n <= 3) { return 1; } else if (n % 2 == 0 || n % 3 == 0) { return 0; } for (in i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) { return 0; } } return 1; } //求某个数(字符串形式)是否为回文 in is_palindrome(const ch* s) { if (s == NULL) { return 0; } in left = 0; in right = strlen(s) - 1; while (left < right) { if (tolower(s[left]) != tolower(s[right])) { return 0; } left++; right--; } return 1; } //求某数是否含某数字(一般数) in contains_digit1(in n, in p) { ch str[50]; ch target = '0' + p; printf(str, "%d", n); for (in i = 0; str[i] != '\0'; i++) { if (str[i] == target) { return 1; } } return 0; } //求某数是否含某数字(超大数,%s输入num_str) in contains_digit2(const ch* num_str, in p) { ch target = '0' + p; for (in i = 0; num_str[i] != '\0'; i++) { if (num_str[i] < '0' || num_str[i] > '9') { continue; } if (num_str[i] == target) { return 1; } } return 0; } //求某年某月的天数 in is_leap(in year) { return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0); } in get_days_of_month(in year, in month) { in days = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: days = is_leap(year) ? 29 : 28; break; } return days; } //求某年某月某日为星期几 in get_week_day(in year, in month, in day) { in century, weekday; if (month < 3) { year--; month += 12; } century = year / 100; year %= 100; weekday = (year + year / 4 + century / 4 - 2 * century + (26 * (month + 1)) / 10 + day - 1) % 7; if (weekday < 0) { weekday += 7; } return weekday; } //求某个数字的反向输出 in turn_digits(in n) { in digits[10] = { 0 }; in i = 0; in num = n; while (num > 0) { digits[i++] = num % 10; num /= 10; } in ans = 0; for (in j = 0; j < i; j++) { ans = ans * 10 + digits[j]; } return ans; } //求某十进制数的各位数 vd extract_digits(in num, in digits[]) { if (num == 0) { digits[0] = 0; digits[1] = -1; return; } in i = 0; while (num > 0) { digits[i++] = num % 10; num /= 10; } } //求给定字符串出现频次 vd string_frequency(const ch* str) { in freq[128] = { 0 }; for (in i = 0; str[i] != '\0'; i++) { unsigned ch c = str[i]; if (c == ' ' || isprint(c)) { freq[c]++; } } int count = 0; for (in i = 0; i < 128; i++) { if (freq[i] > 0) { count++; if (i == ' ') { printf("blank: %d\n", freq[i]); } else { printf("'%c': %d\n", i, freq[i]); } } } } //要求两个整数交换 vd swap(in* a, in* b) { in temp = *a; *a = *b; *b = temp; } //要求升序排列(冒泡排序) vd bubble_sort(in* arr, size_t n) { if (arr == NULL || n < 2) { return; } for (size_t i = 0; i < n - 1; i++) { in swapped = 0; for (size_t j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { in temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; swapped = 1; } } if (!swapped) { break; } } } //要求升序排列(快速排序1到r) vd quick_sort(in arr[], in l, in r) { if (l >= r) { return; } in i = l, j = r; in pivot = arr[(l + r) / 2]; while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { swap(&arr[i], &arr[j]); i++; j--; } } quick_sort(arr, l, j); quick_sort(arr, i, r); } //要求全排数组(start到end) vd permute(in arr[], in start, in end) { if (start == end) { for (in i = 0; i <= end; i++) { printf("%d ", arr[i]); } printf("\n"); } else { for (in i = start; i <= end; i++) { swap(&arr[start], &arr[i]); permute(arr, start + 1, end); swap(&arr[start], &arr[i]); } } } //要求翻转数组 vd reverse_arr(in arr[], in n) { for (in i = 0; i < n / 2; i++) { in t = arr[i]; arr[i] = arr[n - 1 - i]; arr[n - 1 - i] = t; } } //要求数组输入 vd scanf_arr(in arr[], in n) { for (in i = 0; i < n; i++) { scanf("%d", &arr[i]); } } //要求数组输出 vd print_arr(in arr[], in n) { for (in i = 0; i < n; i++) { printf("%d ", arr[i], " \n"[i == n - 1]); } } //求数组各元素的和 in sum_arr(in arr[], in n) { in s = 0; for (in i = 0; i < n; i++) { s += arr[i]; } return s; } //求数组最大元素下标 in max_index(in arr[], in n) { in idx = 0; for (in i = 1; i < n; i++) { if (arr[i] > arr[idx]) { idx = i; } } return idx; } //求数组某元素的位置(升序,n元,二分找x) in binary_search(in arr[], in n, in x) { in left = 0, right = n - 1; while (left <= right) { in mid = (left + right) / 2; if (arr[mid] == x) { return mid; } else if (arr[mid] < x) { left = mid + 1; } else right = mid - 1; } return -1; } //求平面上两点的距离 db distance_2d(db x1, db y1, db x2, db y2) { db dx = x2 - x1; db dy = y2 - y1; return sqrt(dx * dx + dy * dy); } //求三角形的面积(2倍) ll triange_area_2(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3) { ll area_2 = (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1); return area_2 < 0 ? -area_2 : area_2; } //求凸多边形面积(2倍) ll n_triangle_area_2(in n, ll x[], ll y[]) { ll area_2 = 0; for (in i = 0; i < n; i++) { in j = (i + 1) % n; area_2+= x[i] * y[j] - x[j] * y[i]; } return area_2 > 0 ? area_2 : -area_2; }检查正误
最新发布
10-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值