练习一:
1021: 零起点学算法63——弓型矩阵
Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 89 Accepted: 38
[ Submit][ Status][ Web Board]
Description
输出n*m的弓型矩阵
Input
多组测试数据
每组输入2个整数 n和m(不大于20)
每组输入2个整数 n和m(不大于20)
Output
输出n*m的弓型矩阵,要求左上角元素是1,(每个元素占2个位置,靠右)
Sample Input ![](http://acm.wust.edu.cn/image/copy.gif)
4 3
Sample Output
1 2 3
6 5 4
7 8 9
12 11 10
第一次测试代码:
#include<stdio.h>
#include<stdlib.h>
/*
Description
输出n*m的弓型矩阵
Input
多组测试数据
每组输入2个整数 n和m(不大于20)
Output
输出n*m的弓型矩阵,要求左上角元素是1,(每个元素占2个位置,靠右)
Sample Input
4 3
Sample Output
1 2 3
6 5 4
7 8 9
12 11 10
*/
int main()
{
int n, m;
while (scanf("%d%d", &n, &m) != EOF)
{
int *ptos = calloc(n*m, sizeof(int));
int i, j;
int tr = 1; //利用tr*=-1;进行判断输出顺序 tr为1时正序输出 -1时逆序
for (i = 1; i <= n*m; i++)
*(ptos + i - 1) = i;
for (i = 0, j = 0; i < n*m;)
{
if (tr == 1)
{
for (j = i; j <= i+m-1; j++)
{
if (j == i+m-1) printf("%2d \n", *(ptos + j));
else printf("%2d ", *(ptos + j));
}
tr *= -1;
i += m;
}
else
{
for (j = i+m-1; j >= i; j--)
{
if (j == i)
{
printf("%2d \n", *(ptos + j));
}
else printf("%2d ", *(ptos + j));
}
tr *= -1;
i += m;
}
}
}
return 0;
}
运行结果:
测试结果:
Result: Presentation Error
测试二:
#include<stdio.h>
#include<stdlib.h>
/*
Description
输出n*m的弓型矩阵
Input
多组测试数据
每组输入2个整数 n和m(不大于20)
Output
输出n*m的弓型矩阵,要求左上角元素是1,(每个元素占2个位置,靠右)
Sample Input
4 3
Sample Output
1 2 3
6 5 4
7 8 9
12 11 10
*/
int main()
{
int n, m;
while (scanf("%d%d", &n, &m) != EOF)
{
int *ptos = calloc(n*m, sizeof(int));
int i, j;
int tr = 1; //利用tr*=-1;进行判断输出顺序 tr为1时正序输出 -1时逆序
for (i = 1; i <= n*m; i++)
*(ptos + i - 1) = i;
for (i = 0, j = 0; i < n*m;)
{
if (tr == 1)
{
for (j = i; j <= i+m-1; j++)
{
if (j == i+m-1) printf("%2d\n", *(ptos + j));
else printf("%2d ", *(ptos + j));
}
tr *= -1;
i += m;
}
else
{
for (j = i+m-1; j >= i; j--)
{
if (j == i)
{
printf("%2d\n", *(ptos + j));
}
else printf("%2d ", *(ptos + j));
}
tr *= -1;
i += m;
}
}
}
return 0;
}
运行结果:
测试结果: Accepted !!
练习二
1022: 零起点学算法65——进制转换
Description
输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Sample Input ![](http://acm.wust.edu.cn/image/copy.gif)
7 2
23 12
-4 3
Sample Output
111
1B
-11
测试一代码:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/*
Description
输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Sample Input
7 2
23 12
-4 3
Sample Output
111
1B
-11
*/
int fun_change(int N, char *ptoR, int R) //将N转化成R进制并储存,返归尾部指针
{
int y; //用于储存余数
int cnt = 0;//指出当前要存入的位置
while (N / R != 0)
{
y = N%R;
if (R>10 && y>9)
switch (y)
{
case 10: *(ptoR + cnt++) = 'A'; break;
case 11: *(ptoR + cnt++) = 'B'; break;
case 12: *(ptoR + cnt++) = 'C'; break;
case 13: *(ptoR + cnt++) = 'D'; break;
case 14: *(ptoR + cnt++) = 'E'; break;
case 15: *(ptoR + cnt++) = 'F'; break;
}
else *(ptoR + cnt++) = y+48;
N /= R;
}
y = N;
if (R>10 && y>9)
switch (y)
{
case 10: *(ptoR + cnt++) = 'A'; break;
case 11: *(ptoR + cnt++) = 'B'; break;
case 12: *(ptoR + cnt++) = 'C'; break;
case 13: *(ptoR + cnt++) = 'D'; break;
case 14: *(ptoR + cnt++) = 'E'; break;
case 15: *(ptoR + cnt++) = 'F'; break;
}
else *(ptoR + cnt++) = y + 48;
return cnt;
} //将N转化成R进制并储存,返回尾部指针
int main()
{
int N, R;
while (scanf("%d%d", &N, &R) != EOF)
{
char *ptoR = calloc(32, sizeof(char));
int cnt;
cnt = fun_change(fabs(N), ptoR, R);
int i;
if (N < 0) printf("-");
for (i = cnt - 1; i >= 0; i--)
if (i == 0) printf("%c\n", *(ptoR + i));
else printf("%c", *(ptoR + i));
}
return 0;
}
运行结果:
测试结果: Accepted!