本文主要是介绍C++ 基础速通【数组】Ac-Wing,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
数组替换
#include <iostream>
#include <cstdio>
using namespace std;
int main(){for(int i = 0; i < 10; i ++ ){int x; cin >> x;printf("X[%d] = %d\n", i, x <= 0 ? 1 : x);}return 0;
}
数组中的行
#include <iostream>using namespace std;int main()
{int l;char op;cin >> l >> op;double s=0;for(int i=0;i<12;i++){for(int j=0;j<12;j++){double a;cin >> a;if(i==l) s+=a;}}printf("%.1lf",op=='S' ? s : s/12);
}
数组的右上半部分
#include <iostream>
using namespace std;
double m, s;//m用来读入,s用来计算和
char op;
int main()
{cin >> op;for (int i = 0; i < 12; i ++)for (int j = 0; j < 12; j ++){cin >> m;//重复读入mif (i < j)s += m;}if (op == 'S')printf("%.1lf", s);else printf("%.1lf", s / 66);return 0;
}
数组的左上半部分
#include <iostream>
using namespace std;
double m, s;//m用来读入,s用来计算和
char op;
int main()
{cin >> op;for (int i = 0; i < 12; i ++)for (int j = 0; j < 12; j ++){cin >> m;//重复读入mif (i+j<11)s += m;}if (op == 'S')printf("%.1lf", s);else printf("%.1lf", s / 66);return 0;
}
数组的上方区域
#include <iostream>
using namespace std;
double m, s;//m用来读入,s用来计算和
char op;
int main()
{cin >> op;for (int i = 0; i < 12; i ++)for (int j = 0; j < 12; j ++){cin >> m;//重复读入mif (i<j&&i+j<11)s += m;}if (op == 'S')printf("%.1lf", s);else printf("%.1lf", s / 30);return 0;
}
数组的左方区域
#include <iostream>
using namespace std;
double m, s;//m用来读入,s用来计算和
char op;
int main()
{cin >> op;for (int i = 0; i < 12; i ++)for (int j = 0; j < 12; j ++){cin >> m;//重复读入mif (i>j&&i+j<11)s += m;}if (op == 'S')printf("%.1lf", s);else printf("%.1lf", s / 30);return 0;
}
平方矩阵 I
- 作者:小张同学
链接:https://www.acwing.com/solution/content/9554/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
平方矩阵 II
#include <iostream>
using namespace std;
const int N = 110;
int a[N][N];int main()
{int n;cin >> n;while (n){for (int i = 0; i < n; i++){a[i][0] = i + 1;//第一行a[0][i] = i + 1;//第一列}for (int i = 1; i < n; i++){for (int j = 1; j < n; j++){a[i][j] = a[i - 1][j - 1];//其他位置,a[i][j] = a[i - 1][j - 1]}}for (int i = 0; i < n; i++)//输出矩阵{for (int j = 0; j < n; j++){cout << a[i][j] << " ";}cout << endl;}cout << endl;cin >> n;}
}作者:Hasity
链接:https://www.acwing.com/solution/content/31030/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
平方矩阵 III
#include <iostream>using namespace std;int main()
{int n;while(cin >> n,n){for(int i = 0; i < n; i ++){for(int j = 0; j < n; j ++)cout << (1 << i) * (1 << j) << ' ';//两个乘数 后者控制基数 1 ~ 2^(n-1) ,前者控制倍数cout << endl;}cout << endl;}return 0;
}作者:一只草莓
链接:https://www.acwing.com/solution/content/7673/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
蛇形矩阵
#include <iostream>
using namespace std;const int N = 110;
int a[N][N];int main()
{int r,c;cin >> r >> c;int left = 0, right = c - 1;int top = 0, bottom = r - 1;int k = 1;while(left <= right || top <= bottom){for(int i = left; i <= right && top <= bottom; i++)//构造最上面一行{a[top][i] = k++;}top++;for(int i = top; i <= bottom && left <= right; i++)//构造最右侧一列{a[i][right] = k++;}right--;for(int i = right; i >= left && top <= bottom; i--)//构造最下面一行{a[bottom][i] = k++;}bottom--;for(int i = bottom; i >= top && left <= right; i--)//构造最左侧一列{a[i][left] = k++;}left++;}for(int i = 0; i < r; i++){for(int j = 0; j < c; j++) cout<< a[i][j] << " ";cout << endl;}return 0;
}作者:Hasity
链接:https://www.acwing.com/solution/content/28991/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
这篇关于C++ 基础速通【数组】Ac-Wing的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!