目录
- 二维矩阵顺时针旋转90°
- 二维矩阵逆时针旋转180°
- 对字符串按照数字在前字母在后的规则进行排序
1.二维矩阵顺时针旋转90°
题目描述
给定一个
n
∗
n
n * n
n∗n的二维矩阵matrix表示一个图像。请你将图像顺时针旋转90度。
验证
输入:[ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
输出:[ [7, 4, 1], [8, 5, 2], [9, 6, 3] ]
由上图可知,旋转90度后,列下标从小到大(0 → 2),行下标从大到小(2 → 0)。
同理
逆时针旋转九十度时,行列下标变化相似,列从大到小,行从小到大。
2.二维矩阵逆时针旋转180°
由上图可知,逆时针旋转180度后,行和列下标从大到小(0 → 2),显然顺时针旋转180度和逆时针旋转180度
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n][n] = {0};
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
// 旋转之前
cout << "旋转之前:" << endl;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
// 顺时针旋转90度,外层循环代表列,内层循环代表行
cout << "\n顺时针旋转90度:" << endl;
for(int j = 0; j < n; j++)
{
for(int i = n - 1; i >= 0; i--)
{
cout << a[i][j] << " ";
}
cout << endl;
}
// 逆时针旋转90度
cout << "\n逆时针旋转90度:" << endl;
for(int j = n - 1; j >= 0; j--)
{
for(int i = 0; i < n; i++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
//旋转180度之后
cout << "\n旋转180度之后:" << endl;
for(int i = n - 1; i >= 0; i--)
{
for(int j = n - 1; j >= 0; j--)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
3.对字符串按照数字在前字母在后的规则进行排序
题目描述:
将数组中的内容按照一定顺序进行排序,要求数字在前,字母在后,并且数字和字母中也按照ASCII进行排序
示例:
输入:132ba9
输出:1239ab
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s1, sCh, sNum;
cin >> s1;
sort(s1.begin(), s1.end());
for(int i = 0; i < s1.size(); i++)
{
if(s1[i] >= '0' && s1[i] <= '9')
sNum += s1[i];
else if((s1[i] >= 'A' && s1[i] <= 'Z') || (s1[i] >= 'a' && s1[i] <= 'z'))
sCh += s1[i];
}
cout << endl;
cout << sNum + sCh << endl;
return 0;
}
4.对字符串进行排序,按照数字、小写字母、大写字母、其它字符的排列规则进行排序,限定C语言,且不能使用库函数
#include <stdio.h>
int main()
{
char str[20], num[20], ch[20], chCap[20], oth[20]; // 输入字符、数字、小写字母、大写字母、其它字符
int iNum = 0, iCh = 0,iChcap = 0, iOth = 0;
int count = 0;
scanf("%s", str);
while(str[count] != '\0')
{
count++ ;
}
//对字符串排序
for(int i = 0; i < count - 1; i++)
{
for(int j = 0; j < count - 1 - i; j++)
{
char temp;
if(str[j] > str[j + 1])
{
temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
printf("排序后:%s\n", str);
//分离数字、小写字母、大写字母、其他字符
for(int i = 0; i < count; i++)
{
if(str[i] >= '0' && str[i] <= '9')
num[iNum++] = str[i];
else if(str[i] >= 'a' && str[i] <= 'z')
ch[iCh++] = str[i];
else if(str[i] >= 'A' && str[i] <= 'Z')
chCap[iChcap++] = str[i];
else
oth[iOth++] = str[i];
}
printf("num: %s\n ch: %s\n oth: %s\n", num, ch, oth); // 打印分离后的字符数组
printf("iNum: %d\t iCh: %d\t iOth: %d\n iChcap:%d\n", iNum, iCh, iOth, iChcap);
for(int i = 0; i < iNum || i < iCh || i < iChcap || i < iOth; i++)
{
str[i] = num[i];
str[i + iNum] = ch[i];
str[i + iNum + iCh] = chCap[i];
str[i + iNum + iCh + iChcap] = oth[i];
}
printf("%s", str);
return 0;
}
5.请编写一个C函数,该函数给出一个字节中被置1的位的个数
输入:一个char 类型数据
输出:返回该字节中1的个数
#include <stdio.h>
int fuc(char ch)
{
int count = 0;
while( ch &= (ch - 1) )
{
count++;
}
return count - 1; // 不知为何总是比正确数字多一,所以使用减一
}
int main()
{
char ch;
int num;
printf("输入字符:\n" );
scanf(" %c", &ch);
num = fuc(ch);
printf("%d", num);
return 0;
}
6.用变量a给出以下定义:
int a; // 一个整型数;
int *a; // 一个指向整型数的指针
int **a; // 一个指向指针的指针,它指向的指针是一个整数
int a[10]; // 一个有10个整型数的数组
int *a[10]; // 一个有10个指针的数组,该指针是指向一个整型数的
int (*a)[10]; // 一个指向有10个整型数数组的指针
int (*a)(int); // 一个指向函数的指针,该函数有一个整型参数并返回一个整型数
int (*a[10))(int ) // 一个有10个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型数
7.结构体的大小
#include <stdio.h>
#include <stdlib.h>
struct pack{
int c;
double b;
char a;
}pack;
int main()
{
printf("pack.a:%d\n",sizeof(pack.a));
printf("pack.d:%d\n",sizeof(pack.b));
printf("pack.c:%d\n",sizeof(pack.c));
printf("%d\n", sizeof(pack));
return 0;
}
结构体存储时,为方便处理数据,以指定的字节大小进行填补,本题中int型、char型变量都是以double类型变量大小进行存储的。