7-81 打印杨辉三角 分数 20
本题要求按照规定格式打印前N行杨辉三角。
输入格式:
输入在一行中给出N(1≤N≤10)。
输出格式:
以正三角形的格式输出前N行杨辉三角。每个数字占固定4位。
输入样例:
6
输出样例:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int st[N];
int n;
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
for (int j = n - i; j > 0; --j) putchar(' ');
for (int j = i; j >= 1; --j)
{
if (j == 1 || j == i) st[j] = 1;
else st[j] = st[j - 1] + st[j];
printf("%4d", st[j]);
}
putchar('\n');
}
return 0;
}
7-82 方阵循环右移 分数 20
本题要求编写程序,将给定n×n方阵中的每个元素循环向右移m个位置,即将第0、1、⋯、n−1列变换为第n−m、n−m+1、⋯、n−1、0、1、⋯、n−m−1列。
输入格式:
输入第一行给出两个正整数m和n(1≤n≤6)。接下来一共n行,每行n个整数,表示一个n阶的方阵。
输出格式:
按照输入格式输出移动后的方阵:即输出n行,每行n个整数,每个整数后输出一个空格。
输入样例:
2 3
1 2 3
4 5 6
7 8 9
输出样例:
2 3 1
5 6 4
8 9 7
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
using namespace std;
const int N = 1010;
int st[N][N];
int n, m;
// 三次倒置即可
void cg(int p, int l, int r)
{
for(int i = 0; i <= r - l >> 1; ++ i)
swap(st[p][l + i], st[p][r - i]);
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; ++ i)
for(int j = 1; j <= m; ++ j)
cin >> st[i][j];
n %= m;
int k = m - n;
for(int i = 1; i <= m; ++ i)
cg(i, 1, k), cg(i, k + 1, m), cg(i, 1, m);
for(int i = 1; i <= m; ++ i)
{
for(int j = 1; j <= m; ++ j)
printf("%d ", st[i][j]);
putchar('\n');
}
return 0;
}
7-83 找鞍点 分数 20
一个矩阵元素的“鞍点”是指该位置上的元素值在该行上最大、在该列上最小。
本题要求编写程序,求一个给定的n阶方阵的鞍点。
输入格式:
输入第一行给出一个正整数n(1≤n≤6)。随后n行,每行给出n个整数,其间以空格分隔。
输出格式:
输出在一行中按照“行下标 列下标”(下标从0开始)的格式输出鞍点的位置。如果鞍点不存在,则输出“NONE”。题目保证给出的矩阵至多存在一个鞍点。
输入样例1:
4
1 7 4 1
4 8 3 6
1 6 1 2
0 7 8 9
输出样例1:
2 1
输入样例2:
2
1 7
4 1
输出样例2:
NONE
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
using namespace std;
const int N = 1010;
int st[N][N];
int n;
int x, y;
bool flag = true;
bool check(int x, int y)
{
for(int i = 1; i <= n; ++ i)
if(i != y && st[x][y] < st[x][i]) return false;
for(int i = 1; i <= n; ++ i)
if(i != x && st[x][y] > st[i][y]) return false;
return true;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
cin >> st[i][j];
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
if(check(i, j))
{
x = i, y = j;
flag = false;
break;
}
if(flag) puts("NONE");
else cout << x - 1 << " " << y - 1 << endl;
return 0;
}
7-84 螺旋方阵 分数 20
所谓“螺旋方阵”,是指对任意给定的N,将1到N×N的数字从左上角第1个格子开始,按顺时针螺旋方向顺序填入N×N的方阵里。本题要求构造这样的螺旋方阵。
输入格式:
输入在一行中给出一个正整数N(<10)。
输出格式:
输出N×N的螺旋方阵。每行N个数字,每个数字占3位。
输入样例:
5
输出样例:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
using namespace std;
const int N = 1010;
int st[N][N];
int n;
int x = 1, y = 1;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main()
{
cin >> n;
for(int i = 1, p = 0; i <= n * n; ++ i)
{
// 撞墙后更换方向即可
st[x][y] = i;
int a = x + dx[p], b = y + dy[p];
if(a <= 0 || a > n || b <= 0 || b > n || st[a][b])
{
p = (p + 1) % 4;
a = x + dx[p], b = y + dy[p];
}
x = a, y = b;
}
for(int i = 1; i <= n; ++ i)
{
for(int j = 1; j <= n; ++ j)
printf("%3d", st[i][j]);
putchar('\n');
}
return 0;
}
7-85 矩阵运算 分数 20
给定一个n×n的方阵,本题要求计算该矩阵除副对角线、最后一列和最后一行以外的所有元素之和。副对角线为从矩阵的右上角至左下角的连线。
输入格式:
输入第一行给出正整数n(1<n≤10);随后n行,每行给出n个整数,其间以空格分隔。
输出格式:
在一行中给出该矩阵除副对角线、最后一列和最后一行以外的所有元素之和。
输入样例:
4
2 3 4 1
5 6 1 1
7 1 8 1
1 1 1 1
输出样例:
35
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
using namespace std;
const int N = 1010;
int st[N][N];
int n;
long long ans;
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
{
cin >> st[i][j];
if(i + j != n + 1 && j != n && i != n) ans += st[i][j];
}
cout << ans << endl;
return 0;
}
7-86 统计大写辅音字母 分数 15
英文辅音字母是除A
、E
、I
、O
、U
以外的字母。本题要求编写程序,统计给定字符串中大写辅音字母的个数。
输入格式:
输入在一行中给出一个不超过80个字符、并以回车结束的字符串。
输出格式:
输出在一行中给出字符串中大写辅音字母的个数。
输入样例:
HELLO World!
输出样例:
4
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
#include <string>
using namespace std;
string input;
int cnt;
int main()
{
getline(cin, input);
for(char c : input)
if(c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U' && isupper(c)) cnt ++;
cout << cnt << endl;
return 0;
}
7-87 统计字符出现次数 分数 20
本题要求编写程序,统计并输出某给定字符在给定字符串中出现的次数。
输入格式:
输入第一行给出一个以回车结束的字符串(少于80个字符);第二行输入一个字符。
输出格式:
在一行中输出给定字符在给定字符串中出现的次数。
输入样例:
programming is More fun!
m
输出样例:
2
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
#include <string>
using namespace std;
string input;
char op;
int cnt;
int main()
{
getline(cin, input);
op = getchar();
for(char c : input)
if(c == op) cnt ++;
cout << cnt << endl;
return 0;
}
7-88 字符串逆序 分数 15
输入一个字符串,对该字符串进行逆序,输出逆序后的字符串。
输入格式:
输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。
输出格式:
在一行中输出逆序后的字符串。
输入样例:
Hello World!
输出样例:
!dlroW olleH
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
#include <string>
using namespace std;
string input;
int main()
{
getline(cin, input);
for(int i = 0; i <= input.size() - 1 >> 1; ++ i)
swap(input[i], input[input.size() - i - 1]);
cout << input << endl;
return 0;
}
7-89 字符串字母大小写转换 分数 15
本题要求编写程序,对一个以“#”结束的字符串,将其小写字母全部转换成大写字母,把大写字母全部转换成小写字母,其他字符不变输出。
输入格式:
输入为一个以“#”结束的字符串(不超过30个字符)。
输出格式:
在一行中输出大小写转换后的结果字符串。
输入样例:
Hello World! 123#
输出样例:
hELLO wORLD! 123
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
#include <string>
using namespace std;
string input;
int main()
{
getline(cin, input);
for(char c : input)
if(isupper(c)) putchar(c - 'A' + 'a');
else if(islower(c)) putchar(c - 'a' + 'A');
else if(c != '#')putchar(c);
return 0;
}
7-90 查找指定字符 分数 15
本题要求编写程序,从给定字符串中查找某指定的字符。
输入格式:
输入的第一行是一个待查找的字符。第二行是一个以回车结束的非空字符串(不超过80个字符)。
输出格式:
如果找到,在一行内按照格式“index = 下标”输出该字符在字符串中所对应的最大下标(下标从0开始);否则输出"Not Found"。
输入样例1:
m
programming
输出样例1:
index = 7
输入样例2:
a
1234
输出样例2:
Not Found
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
#include <string>
using namespace std;
string input;
char op;
int main()
{
// 吸收换行符\n
op = getchar(), getchar();
getline(cin, input);
int pos = input.rfind(op);
if(pos != -1) printf("index = %d\n", pos);
else puts("Not Found");
return 0;
}