Lolaandd @ 2023-11-20 19:03:13
#include<stdio.h>
#define MAXN 100
void multiply(int num,int arr[],int *size)
{
for (int i=0;i<*size;i++)
{
arr[i]=arr[i]*num;
}
for (int i=0;i<*size;i++)
{
if (arr[i]>=10)
{
arr[i+1]+=arr[i]/10;
arr[i]=arr[i]%10;
}
}
while(arr[*size-1]>=10)
{
arr[*size]+=arr[*size-1]/10;
arr[*size-1]%=10;
(*size)++;
}
}
void sum (int arr1[],int arr2[])
{
int max_size=0;
for(int i=0;i<=MAXN;i++)
{
if (arr1[i]==0&&arr2[i]==0)
{
max_size=i;
break;
}
}
for (int i=0;i<max_size;i++)
{
arr1[i]+=arr2[i];
}
for (int i=0;i<max_size;i++)
{
if(arr1[i]>=10)
{
arr1[i+1]+=arr1[i]/10;
arr1[i]=arr1[i]%10;
}
}
}
int main ()
{
int n,size=1;
int result[MAXN]={0};
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int temp[MAXN]={1};
for(int m=1;m<=i;m++)
{
multiply(m,temp,&size);
}
sum (result,temp);
}
for(int i=size;i>=0;i--)
{
if(result[i]==0) continue;
printf("%d",result[i]);
}
return 0;
}
by heyx0201 @ 2023-11-20 19:19:58
@Lolaandd 什么思路?????????
by Lolaandd @ 2023-11-20 19:24:57
@heyx0201 就是multiply函数用数组模拟乘法 sum用数组模拟加法 然后在主函数里算阶乘之和存到result里 最后逆序输出
by Lolaandd @ 2023-11-20 19:36:56
@heyx0201 听不懂 吸吸
by Lolaandd @ 2023-11-20 19:38:12
@heyx0201 蛤?自己写的啊
by heyx0201 @ 2023-11-20 19:40:34
@Lolaandd 看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了看错了对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起对不起
by Lolaandd @ 2023-11-20 19:47:12
@heyx0201 sos那顺便看看我错哪里了 瞪了俩小时了没看出来
by heyx0201 @ 2023-11-20 19:58:25
@Lolaandd 看看这个,你会理解的
// Problem: P1009 [NOIP1998 普及组] 阶乘之和
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1009
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Date: 2023-11-12 11:32:36
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
namespace Integer {
const string symbol = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <size_t kMaxLength, int binary = 10, typename T = int> struct BigInt {
T n, a[kMaxLength];
bool f;
BigInt() { Init(); }
T &operator[](int i) { return a[i]; }
void Init() { // 初始化函数
n = f = 1;
fill(a, a + kMaxLength, 0);
}
/* 别管就是用 cin,cout 输入输出的东西用自己写的就行了 */
friend istream &operator>>(istream &tempStream, BigInt &a) {
string s;
tempStream >> s;
a.n = s.size();
if (s[0] == '-') {
a.f = 0;
a.n--, s.erase(0, 1);
}
for (int i = 0; i < a.n; i++) {
a[i] = (s[a.n - i - 1] >= 'A' && s[a.n - i - 1] <= 'Z'
? s[a.n - i - 1] - 'A' + 10
: s[a.n - i - 1] - '0');
}
return tempStream;
}
friend ostream &operator<<(ostream &tempStream, BigInt a) {
for (; a.n > 1 && !a[a.n - 1]; a.n--) {
}
if (!a.f) {
cout << '-';
}
for (int i = a.n - 1; i >= 0; i--) {
tempStream << symbol[a[i]];
}
return tempStream;
}
/* 运算符 */
void operator=(int x) {
Init();
if (!x) {
return;
}
if (x < 0) {
x = -x, f = 0;
}
n = 0;
while (x) {
a[n++] = x % binary;
x /= binary;
}
}
void operator=(string x) {
Init();
int st = 0;
if (x[0] == '-') {
f = 0, st++;
}
n = 0;
int len = x.size();
for (int i = st; i < len; i++) {
a[n++] = x[len - i - 1] - '0';
}
}
void operator=(BigInt x) {
Init();
n = x.n;
f = x.f;
for (int i = 0; i < n; i++) {
a[i] = x[i];
}
}
bool operator==(BigInt x) {
if (n != x.n) {
return 0;
}
for (int i = n - 1; i >= 0; i++) {
if (a[i] != x[i]) {
return 0;
}
}
return 1;
}
bool operator!=(BigInt x) { return !operator==(x); }
bool operator<(BigInt x) {
if (n == x.n) {
for (int i = n - 1; i >= 0; i--) {
if (a[i] != x[i]) {
return a[i] < x[i];
}
}
}
return n < x.n;
}
bool operator>(BigInt x) {
if (n == x.n) {
for (int i = n - 1; i >= 0; i--) {
if (a[i] != x[i]) {
return a[i] > x[i];
}
}
}
return n > x.n;
}
bool operator<=(BigInt x) { return !operator>(x); }
bool operator>=(BigInt x) { return !operator<(x); }
bool operator!() {
if (n != 1) {
return 0;
}
return !a[0];
}
bool operator==(int x) {
BigInt y;
y = x;
return operator==(y);
}
bool operator!=(int x) {
BigInt y;
y = x;
return operator!=(y);
}
bool operator<(int x) {
BigInt y;
y = x;
return operator<(y);
}
bool operator>(int x) {
BigInt y;
y = x;
return operator>(y);
}
bool operator<=(int x) {
BigInt y;
y = x;
return operator<=(y);
}
bool operator>=(int x) {
BigInt y;
y = x;
return operator>=(y);
}
/* 加法 */
BigInt operator+(BigInt x) {
BigInt y;
y.n = max(n, x.n);
for (int i = 0; i < y.n; i++) { // 加
y[i] = a[i] + x[i];
}
for (int i = 0; i < y.n - 1; i++) { // 进位
y[i + 1] += y[i] / 10;
y[i] %= 10;
}
for (; !y.a[y.n - 1] && y.n > 1; y.n--) { // 前导零
}
return y;
}
void operator+=(BigInt x) {
BigInt z = *this;
*this = z + x;
}
void operator+=(int x) {
BigInt z = *this;
*this = z + x;
}
BigInt operator+(int x) {
BigInt y;
y = x;
return operator+(y);
}
/* 乘法 */
BigInt operator*(BigInt x) {
BigInt y;
y.n = n + x.n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < x.n; j++) {
y[i + j] += a[i] * x[j]; // 乘
}
}
for (int i = 0; i < y.n - 1; i++) { // 进位
y[i + 1] += y[i] / binary, y[i] %= binary;
}
for (; !y.a[y.n - 1] && y.n > 1; y.n--) { // 前导零
}
return y;
}
void operator*=(BigInt x) {
BigInt z = *this;
*this = z * x;
}
void operator*=(int x) {
BigInt z = *this;
*this = z * x;
}
BigInt operator*(int x) {
BigInt ans;
ans.n = n + 2;
for (int i = 0; i < n; i++) {
ans[i] = a[i] * x;
}
for (int i = 0; i < n - 1; i++) {
ans[i + 1] += ans[i] / 10, ans[i] %= 10;
}
return ans;
}
};
};
using namespace Integer;
int n;
int main() {
cin >> n;
BigInt<100> ans;
ans = 0;
BigInt<100> sum;
sum = 1;
for (int i = 1; i <= n; i++) {
sum *= i;
ans += sum;
}
cout << ans;
return 0;
}
我是写 C++ 的,所以凑合着看吧。。。namespace
和 template
C 语言里没有的你就去掉吧。。
by Lolaandd @ 2023-11-20 20:00:26
@heyx0201 OK 3Q!