数列特征
【AC代码】:
#include <iostream> #include <algorithm> using namespace std; #define MAX 10000+10 int main() { int n = 0, i = 0; int num[MAX]; cin >> n; for (i = 0; i < n; i++) cin >> num[i]; int mmin = num[0], mmax = num[0], sum = num[0]; for (i = 1; i < n; i++) { if (num[i]>mmax) mmax = num[i]; if (num[i]<mmin) mmin = num[i]; sum += num[i]; } cout << mmax << endl; cout << mmin << endl; cout << sum << endl; }
查找整数
【AC代码】:注意-1
#include <iostream> #include <algorithm> using namespace std; #define MAX 1000+10 int main() { int n = 0, x = 0, i = 0; int num[MAX]; cin >> n; for (i = 0; i < n; i++) cin >> num[i]; cin >> x; for (i = 0; i < n; i++) { if (num[i] == x) { cout << i+1; return 0; } } cout << -1; }
杨辉三角形
【AC代码】:必须初始化。最后又没有空格都行。
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; #define MAX 34+2 int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int n = 0, i = 0, j = 0; int tra[MAX][MAX]; cin >> n; //initial first col memset(tra, 0, sizeof(tra)); for (i = 0; i < n; i++) tra[i][0] = 1; //fill for (i = 1; i < n; i++) { for (j = 1; j < MAX-1; j++) { tra[i][j] = tra[i-1][j-1] + tra[i-1][j]; } } //output for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (0 != tra[i][j]) { if (0 == j) cout << tra[i][j]; else cout << " " << tra[i][j]; } } cout << endl; } }
时间: 2024-10-14 12:59:48