内容纲要
1051. 矩阵乘法
//********** Specification of multiply **********
void multiply(int (*A)[N], int (*B)[N], int (*C)[N],int n);
/* PreCondition:
A, B, and C are addresses of three matrices
and n (n<=N) is a positive integer
PostCondition:
C is the product of A and B.
*/
题解
由于确定每一个元素需要两层循环,乘法需要一层循环,因此使用三层循环就能解决问题。
代码
#include <stdio.h>
#define N 10
//********** Specification of multiply **********
void multiply(int (*A)[N], int (*B)[N], int (*C)[N],int n)
/* PreCondition:
A, B, and C are addresses of three matrices
and n (n<=N) is a positive integer
PostCondition:
C is the product of A and B.
*/
{
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
C[i][j]=0;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(int k = 0; k < n; k++){
C[i][j] += (A[i][k]) * (B[k][j]);
}
}
}
}
/***************************************************************/
int main()
{
int A[N][N], B[N][N], C[N][N], n, i, j;
scanf("%d",&n);
for (i=0;i<n;i++)
for (j=0;j<n;j++)
scanf("%d",&A[i][j]);
for (i=0;i<n;i++)
for (j=0;j<n;j++)
scanf("%d",&B[i][j]);
/********** multiply is called here **************/
multiply(A,B,C,n);
/**************************************************/
for (i=0;i<n;i++)
for (j=0;j<n;j++)
printf("%d%c",C[i][j],j<n-1?' ':'\n');
return 0;
}
1052. Number2Alpha
将数字月份转化为字符串。
题解
12个switch就解决了,这题的重点在如何将字符串赋值给数组。当然,也可以写个新函数用循环一个一个往里塞,肯定不会错。
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *Number2Alpha(int num){
char *month = (char*)malloc(10*sizeof(char));
switch (num)
{
case 1:
{strcpy(month,"January");
return month;}
case 2:
{strcpy(month,"February");
return month;}
case 3:
{strcpy(month,"March");
return month;}
case 4:
{strcpy(month,"April");
return month;}
case 5:
{strcpy(month,"May");
return month;}
case 6:
{strcpy(month,"June");
return month;}
case 7:
{strcpy(month,"July");
return month;}
case 8:
{strcpy(month,"August");
return month;}
case 9:
{strcpy(month,"September");
return month;}
case 10:
{strcpy(month,"October");
return month;}
case 11:
{strcpy(month,"November");
return month;}
case 12:
{strcpy(month,"December");
return month;}
default:
return 0;
}
}
int main(){
int n;
scanf("%d", &n);
char *month;
month = Number2Alpha(n);
printf("%s\n", month);
return 0;
}
1053. Sort
输入n个字符串,按照字符串大小升序排序。使用qsort。
题解
建议还是使用c自带的qsort函数,手写一个cmp。查一查strcmp的参数列表和返回值,理解一下cmp怎么用。
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 80
int cmp(const void* a, const void* b){
return strcmp(a,b);
}
void Sort(char (*a)[LEN + 1], int n){
qsort(a,n,sizeof(a[0]),cmp);
}
int main(){
char a[12][LEN+1];
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%s", a[i]);
Sort(a,n);
for(int i = 0; i < n; i++)
printf("%s\n", a[i]);
return 0;
}
1054. 矩阵排序
输入 n 和 m, 表示一个int类型的矩阵的行数和列数,然后输入矩阵的各个元素。
用 qsort 对矩阵排序,要求按照每一行中 m 个数的总和的降序排序,若有两行总和相等,则按输入顺序排序。
输出排序后的矩阵。
题解
还是建议用万能的cmp。这题的数据没明说,但如果求和的话需要开long long,卡了我好久。
记得将cmp最后的返回值改成int范围内的数据,如果不强制转换成int范围内数据会导致qsort出错。
这题数据像个闸总。
代码
#include <stdio.h>
#include <stdlib.h>
int n, m;
long long getSum(long long *a){
long long s = 0;
for(int j = 0; j < m; j++)
s += *(a+j);
return s;
}
int cmp(const void *a, const void *b){
if (getSum((long long *)b) - getSum((long long *)a) > 0)
return 1;
else
return -1;
}
int main(){
//int a[101][101];
scanf("%d %d", &n, &m);
long long *a = (long long *)malloc(sizeof(long long )*(n*m+100));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
scanf("%lld ", a+i*m+j);
qsort(a,n,sizeof(long long)*m,cmp);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++)
printf("%lld ",*(a+i*m+j));
printf("\n");
}
free(a);
return 0;
}
最后一个范围之坑,卡了好久
强的不行
强👍,之前没理清最后一道题的思路,cmp和范围也卡了。感谢大佬x
求和那个数据类型就oj测试数据而言,貌似开到long就够了,不过当时确实没想到这个范围也要改orz
离谱,才发觉long范围和int一样,但是oj就能过
能开long long就尽量别开long 很多机子long和int没区别 这是习惯