训练一: 零起点学算法84——Box of Bricks
Description
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. “Look, I’ve built a wall!”, he tells his older sister Alice. “Nah, you should make all stacks the same height. Then you would have a real wall.”, she retorts. After a little consideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum number of bricks moved. Can you help?
Input
The input consists of several data sets. Each set begins with a line containing the number n of stacks Bob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume 1≤n≤50 and 1≤hi≤100.
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
The input is terminated by a set starting with n = 0. This set should not be processed.
Output
For each set, print the minimum number of bricks that have to be moved in order to make all the stacks the same height.
Output a blank line between each set.
Sample Input
6
5 2 4 1 7 5
0
Sample Output
5
代码一:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n;
while (scanf("%d", &n) != EOF, n) //当输入的n为0时退出循环
{
int i = 0,j;
int *ptos = calloc(n, sizeof(int));
j = n;
while (j--) scanf("%d", ptos + i++);
int ave = 0; //用它来储存平均的高度
for (i = 0; i < n; i++)
ave += *(ptos + i);
ave /= n;
int counter = 0; //用它来储存搬砖的次数
for (i = 0; i < n; i++)
{
while (*(ptos + i) > ave)
{
*(ptos + i) -= 1;
counter++;
}
}
printf("%d\n\n", counter);
}
return 0;
}
while的新用法:
while (scanf(“%d”, &n) != EOF, n) //当输入的n为0时退出循环
运行结果:
练习二: 零起点学算法87——超级楼梯
Description
有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
Input
输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
Output
对于每个测试实例,请输出不同走法的数量。
Sample Input
2
2
3
Sample Output
1
2
测试代码一:
#include<stdio.h>
int fun_c(int n, int j);
int fun_j(int k);
int main()
{
int n;
scanf("%d", &n);
int t = n;
while (t--)
{
int i;
int number;
scanf("%d", &number);
int number0 = number - 1;
int number2 = number0 / 2;
int ways = 1;
for (i = 1; i <= number2; i++)
ways += fun_c(number0 - i, i);
printf("%.0f\n", ways);
}
return 0;
}
int fun_c(int n, int j)
{
return n == j ? 1 : (fun_j(n) / fun_j(n - j)) / fun_j(j);
}
int fun_j(int k)
{
int i;
int jc = 1;
for (i = 2; i <= k; i++)
jc *= i;
return jc;
}
运行结果:
运行结果:
Runtime Error
分析:
感谢@guoyiwei111
修改代码:
#include<stdio.h>
double fun_c(int n, int j);
double fun_j(int k);
int main()
{
int n;
scanf("%d", &n);
int t = n;
while (t--)
{
int i;
int number;
scanf("%d", &number);
int number0 = number - 1;
int number2 = number0 / 2;
double ways = 1;
for (i = 1; i <= number2; i++)
ways += fun_c(number0 - i, i);
printf("%.0f\n", ways);
}
return 0;
}
double fun_c(int n, int j)
{
return n == j ? 1 : (fun_j(n) / fun_j(n - j)) / fun_j(j);
}
double fun_j(int k)
{
int i;
double jc = 1;
for (i = 2; i <= k; i++)
jc *= i;
return jc;
}
将求阶乘的函数返回类型int换为double
测试结果: