//水题五道//Fibbonacci Number//A1 = ?//Box of Bricks//Max Num//A|B?

Fibbonacci Number

Your objective for this question is to develop a program which will generate a fibbonacci number. The fibbonacci function is defined as such:
f(0) = 0
f(1) = 1
f(n) = f(n-1) + f(n-2)
Your program should be able to handle values of n in the range 0 to 50.
Input
Each test case consists of one integer n in a single line where 0≤n≤50. The input is terminated by -1.
Output
Print out the answer in a single line for each test case.
Sample Input
3
4
5
-1
Sample Output
2
3
5

解题思路:
先打表再输出,要不然会超时。要用__int64型输入输出。

#include<stdio.h>
__int64 num[55];

void init()
{
    num[0]=0;
    num[1]=1;
    for(int i=2;i<=50;i++)
    {
        num[i]=num[i-1]+num[i-2];
    }
}

int main()
{
    init();
    int n;
    while(1)
    {
        scanf("%d",&n);
        if(n==-1)return 0;
        printf("%I64d\n",num[n]);
    }
}

A1 = ?

有如下方程:A i = (A i-1 + A i+1)/2 - C i (i = 1, 2, 3, …. n).
若给出A 0, A n+1, 和 C 1, C 2, …..C n.
请编程计算A 1 = ?
Input
输入包括多个测试实例。
对于每个实例,首先是一个正整数n,(n <= 3000); 然后是2个数a 0, a n+1.接下来的n行每行有一个数c i(i = 1, ….n);输入以文件结束符结束。
Output
对于每个测试实例,用一行输出所求得的a1(保留2位小数).
Sample Input
1
50.00
25.00
10.00
2
50.00
25.00
10.00
20.00
Sample Output
27.50
15.00

解题思路:
跟编程没什么关系,找规律,得出能用a0,an+1,c1……cn,求a1的数学公式。

#include<stdio.h>

int main()
{
    int n;
    double a0,a1,c,sum,ans;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%lf%lf",&a0,&a1);
        sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf",&c);
            sum+=2*(n-i)*c;
        }
        ans=(a1-sum+n*a0)/(n+1);
        printf("%.2lf\n",ans);
    }
}

Box of Bricks

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

解题思路:
明明是水题还一直PE。后来发现最后一个样例后面不能有空行,所以改成第一个样例后不输出空行,其他样例前输出空行。

#include<stdio.h>
#include<math.h>

int main()
{
    int i,n,flag=1,sum,ans,num[55];
    while(1)
    {
        scanf("%d",&n);
        if(n==0)return 0;
        sum=0;
        ans=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            sum+=num[i];
        }
        sum=sum/n;
        for(i=1;i<=n;i++)
        {
            if(num[i]>sum)
                ans+=num[i]-sum;
        }
        if(flag==1)
        {
            flag=0;
        }
        else
        {
            printf("\n");
        }
        printf("%d\n",ans);
    }
}

Max Num

There are some students in a class, Can you help teacher find the highest student .
Input
There are some cases. The first line contains an integer t, indicate the cases; Each case have an integer n ( 1 ≤ n ≤ 100 ) , followed n students’ height.
Output
For each case output the highest height, the height to two decimal plases;
Sample Input
2
3 170.00 165.00 180.00
4 165.00 182.00 172.00 160.00
Sample Output
180.00
182.00

解题思路:
纯水题,输入一个处理一个,不断更新最大值。

#include<stdio.h>

int main()
{
    int t,n,i;
    double tmp,max;
    scanf("%d",&t);
    while(t--)
    {
        max=0;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%lf",&tmp);
            tmp>max?max=tmp:max=max;
        }
        printf("%.2lf\n",max);
    }
}

A|B?

正整数A是否能被正整数B整除,不知道为什么xhd会研究这个问题,来帮帮他吧。
Input
输入数据的第一行是一个数据T,表示有T组数据。
每组数据有两个正整数A和B(A,B<10^9)。
Output
对于每组输入数据,输出”YES”表示可以被整除,”NO”表示不能被整除。
Sample Input
2
4 2
5 3
Sample Output
YES
NO

解题思路:
超级大水题,直接模运算就好了。

#include<stdio.h>

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        if(a%b==0)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值