#include<stdio.h> int main() { int b, tn=1, sn=0, n=1; for (b = 0;b <= 20;b++) { tn = tn * n; n++; sn = tn + sn; } printf("%d", sn); return 0; } 大神们我怎么去终止计算呀 我怎么让这个在其特定的地方停止计算呐???
明明是对的,又是编译错误。 #include<iostream> using namespace std; int jc(int n) { if (n == 1) return 1; else return n * jc(n - 1); } int main() { int a = 0; int m; cin >> m; for (int i = 1; i <= m; i++) { a = a+jc(i); } cout << a; }
乐迪钱魏润禹 2023-12-09 11:42:41 |
#include<iostream> using namespace std; long long jc(int n) { if (n == 1) return 1; else return n * jc(n - 1); } int main() { long long a = 0; int m; cin >> m; for (int i = 1; i <= m; i++) { a = a+jc(i); } cout << a; }
乐迪钱魏润禹 2023-12-09 11:43:04 |
把类型改成long long就可以了
乐迪钱魏润禹 2023-12-09 11:43:15 |
应该是内存超了
乐迪钱魏润禹 2023-12-09 11:43:28 |
我试了,编译没有错误
#include<iostream> #include<cmath> using namespace std; long num(int n){ if(n == 1) return n; else return n * num(n-1); } int main() { int n; long Sn=0; cin >> n; if(n <= 20){ for(int i = 1; i <= n; i++){ Sn += num(i); } cout << Sn; return 0; } }
#include <iostream> #include <cmath> using namespace std; int main() { long a = 0; long int Sn = 1; int n; cin>>n; for(int i=0;i<n;++i) { Sn = Sn*(i+1); a=a+Sn; } cout<<a<<endl; return 0; }
xiaowc 2023-10-31 10:51:02 |
单层for循环
#include<stdio.h> int main() { int m,i,j,; long long scanf("%d",&m); for(i=1;i<=m;i++) { int k=1; for(j=1;j<=i;j++) k*=j; sum+=k; } printf("%d",sum); return 0;