Update Source Files

This commit is contained in:
Hmtsai 2023-12-03 13:30:15 +08:00
parent 7dcbce15f6
commit 01a6e267f7
22 changed files with 469 additions and 36 deletions

7
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}

View File

@ -1,3 +1,6 @@
{
"cmake.configureOnOpen": false
"cmake.configureOnOpen": false,
"files.associations": {
"iostream": "cpp"
}
}

28
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}

28
src/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}

Binary file not shown.

View File

@ -0,0 +1,113 @@
#include <iostream>
#include <cstring>
using namespace std;
int a[101], b[101], c[101], d, i;
void init(int a[])
{
string s;
cin >> s;
// 读入字符串s
a[0] = s.length();
// 用a[0]计算字符串 s的位数
for (i = 1; i <= a[0]; i++)
a[i] = s[a[0] - i] - '0';
// 将数串s转换为数组a,并倒序存储.
}
void print(int a[])
// 打印输出
{
if (a[0] == 0)
{
cout << 0 << endl;
return;
}
for (int i = a[0]; i > 0; i--)
cout << a[i];
cout << endl;
}
int compare(int a[], int b[])
// 比较a和b的大小关系,若a>b则为1,a<b则为-1,a=b则为0
{
int i;
if (a[0] > b[0])
return 1;
// a的位数大于b则a比b大
if (a[0] < b[0])
return -1;
// a的位数小于b则a比b小
for (i = a[0]; i > 0; i--)
// 从高位到低位比较
{
if (a[i] > b[i])
return 1;
if (a[i] < b[i])
return -1;
}
return 0;
// 各位都相等则两数相等。
}
void numcpy(int p[], int q[], int det)
// 复制p数组到q数组从det开始的地方
{
for (int i = 1; i <= p[0]; i++)
q[i + det - 1] = p[i];
q[0] = p[0] + det - 1;
}
void jian(int a[], int b[])
// 计算a=a-b
{
int flag, i;
flag = compare(a, b);
// 调用比较函数判断大小
if (flag == 0)
{
a[0] = 0;
return;
} // 相等
if (flag == 1)
// 大于
{
for (i = 1; i <= a[0]; i++)
{
if (a[i] < b[i])
{
a[i + 1]--;
a[i] += 10;
} // 若不够减则向上借一位
a[i] -= b[i];
}
while (a[0] > 0 && a[a[0]] == 0)
a[0]--;
// 修正a的位数
}
}
void chugao(int a[], int b[], int c[])
{
int tmp[101];
c[0] = a[0] - b[0] + 1;
for (int i = c[0]; i > 0; i--)
{
memset(tmp, 0, sizeof(tmp));
// 数组清零
numcpy(b, tmp, i);
while (compare(a, tmp) >= 0)
{
c[i]++;
jian(a, tmp);
} // 用减法来模拟
}
while (c[0] > 0 && c[c[0]] == 0)
c[0]--;
}
int main()
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
init(a);
init(b);
chugao(a, b, c);
print(c);
print(a);
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,37 @@
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char a1[100];
int a[100], c[100], i, lena, lenc, x = 0, b;
memset(a, 0, sizeof(a));
memset(c, 0, sizeof(c));
cin.getline(a1, 100);
cin >> b;
if(b==0){
cout<<"0"<<endl;
return 0;
}
lena = strlen(a1);
for (int i = 0; i < lena; i++)
{
a[i + 1] = a1[i] - '0';
}
for (int i = 1; i <= lena; i++)
{
c[i] = (x * 10 + a[i]) / b;
x = (x * 10 + a[i]) % b;
}
lenc=1;
while (c[lenc] == 0 && lenc < lena)
{
lenc++;
}
for (int i = lenc; i <= lena; i++)
{
cout << c[i];
}
cout << endl;
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,27 @@
#include <iostream>
#include <cstring>
using namespace std;
int a[10000];
int main()
{
int n,i,j,x,len=1;
cin>>n;
a[1]=1;
for (i = 1; i <= n; i++)
{
x=0;
for (j = 1; j <= len; j++)
{
a[j]=a[j]*2+x;
x=a[j]/10;
a[j]=a[j]%10;
if(x!=0&&j==len) len++;
}
}
for (int i = len; i >= 1; i--)
{
cout<<a[i];
}
cout<<endl;
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,47 @@
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char a1[101], b1[101];
int a[101], b[101], c[10001], lenc, x, i, j;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
cout<<"Enter the first: ";
cin >> a1;
cout<<"Enter the second: ";
cin >> b1;
int lena = strlen(a1);
int lenb = strlen(b1);
for (int i = 0; i <= lena - 1; i++)
{
a[lena - i] = a1[i] - '0';
}
for (int i = 0; i <= lenb - 1; i++)
{
b[lenb - i] = b1[i] - '0';
}
for (i = 1; i <= lena; i++)
{
x = 0;
for (j = 1; j <= lenb; j++)
{
c[i + j - 1] = a[i] * b[j] + x + c[i + j - 1];
x = c[i + j - 1] / 10;
c[i + j - 1] %= 10;
}
c[i+lenb]=x;
}
lenc = lena+lenb;
while ((c[lenc] == 0) && (lenc > 1))
{
lenc--;
}
for (i = lenc; i >= 1; i--)
{
cout << c[i];
}
cout << endl;
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,64 @@
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[101];
int a[101], b[101], lena, i, j;
memset(str, '\0', sizeof(str));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
cin >> str;
lena = strlen(str);
for (int i = 0; i <= lena - 1; i++)
{
a[lena - i] = str[i] - '0';
}
for (int i = 1; i <= lena; i++)
{
b[i] = str[i - 1] - '0';
}
for (j = 1; j <= 30; j++)
{
int flag = 0;
int carry = 0;
if (j != 0)
{
for (int i = 0; i <= lena + 1; i++)
{
b[lena - i + 1] = a[i];
}
}
for (i = 1; i <= lena + 1; i++)
{
a[i] += b[i] + carry;
carry = a[i] / 10;
a[i] = a[i] % 10;
if (carry != 0 && i == lena)
{
lena++;
}
}
for (int i = 0; i <= lena/2; i++)
{
if (a[i] != a[lena - i + 1])
{
flag = 1;
break;
}
}
if (flag != 1)
{
break;
}
}
if (j >= 30)
{
cout << "Impossible";
return 0;
}
cout << j << endl;
etyj
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,47 @@
/*高精度加法程序*/
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char n[256], n1[256], n2[256];
int a[256], b[256], c[256], lena, lenb, lenc, i;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
memset(n, 0, sizeof(n));
memset(n1, 0, sizeof(n1));
memset(n2, 0, sizeof(n2));
printf("Input minuend:");
cin >> n1;
printf("Input minuend:");
cin >> n2;
lena = strlen(n1);
lenb = strlen(n2);
for (i = 0; i <= lena - 1; i++)
{
a[lena - i] = n1[i] - '0';
}
for (i = 0; i <= lenb - 1; i++)
{
b[lenb - i] = n2[i] - '0';
}
int carry = 0;
for (i = 1; i <= lena || i <= lenb; i++)
{
c[i] = a[i] + b[i] + carry;
carry = c[i] / 10;
c[i] = c[i] % 10;
}
lenc = i;
while ((c[lenc] == 0) && (lenc > 1))
{
lenc--;
}
for (i = lenc; i >= 1; i--)
{
cout << c[i];
}
cout << endl;
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,57 @@
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char n[256], n1[256], n2[256];
int a[256], b[256], c[256], lena, lenb, lenc, i = 1;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
memset(n, 0, sizeof(n));
memset(n1, 0, sizeof(n1));
memset(n2, 0, sizeof(n2));
printf("Input minuend:");
cin >> n1;
printf("Input minuend:");
cin >> n2;
if (strlen(n1) < strlen(n2) || ((strlen(n1) == strlen(n2)) && (strcmp(n1, n2) < 0)))
{
strcpy(n, n1);
strcpy(n1, n2);
strcpy(n2, n);
cout << "-";
}
lena = strlen(n1);
lenb = strlen(n2);
for (i = 0; i <= lena - 1; i++)
{
a[lena - i] = n1[i] - '0';
}
for (i = 0; i <= lenb - 1; i++)
{
b[lenb - i] = n2[i] - '0';
}
i = 1;
while (i <= lena || i <= lenb)
{
if (a[i] < b[i])
{
a[i] += 10;
a[i + 1]--;
}
c[i] = a[i] - b[i];
i++;
}
lenc = i;
while ((c[lenc] == 0) && (lenc > 1))
{
lenc--;
}
for (i = lenc; i >= 1; i--)
{
cout << c[i];
}
cout << endl;
return 0;
}

Binary file not shown.

View File

@ -1,35 +0,0 @@
#include <cstring>
#include <iostream>
using namespace std;
void myplus(int list1[], int list2[], int list1length, int list2length,
int *count) {
for (int i = 1, x = 0; i <= max(list1length, list2length); i++) {
count[i] = list1[i] + list2[i];
x = count[1] / 10;
count[i] %= 10;
i++;
}
}
void init() {
string snum1, snum2;
cin >> snum1 >> snum2;
int inum1[snum1.size()], inum2[snum2.size()];
for (int i = 0; i < snum1.size(); i++) {
inum1[i] = snum1.c_str()[i] - '0';
}
for (int i = 0; i < snum2.size(); i++) {
inum2[i] = snum2.c_str()[i] - '0';
}
int count[max(snum1.size(), snum2.size()) + 1];
myplus(inum1, inum2, snum1.size(), snum2.size(), count);
for (int i = 0; i < max(snum1.size(), snum2.size()) + 1; i++) {
cout << count[i];
}
}
int main() {
init();
return 0;
}

BIN
src/lib/memset/memset Executable file

Binary file not shown.

10
src/lib/memset/memset.cpp Normal file
View File

@ -0,0 +1,10 @@
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char *s = "GoldenGlobalView";
memset(s, 'G', 6);
return 0;
}