杭电刷题

此博客链接:https://www.cnblogs.com/ping2yingshi/p/12395220.html

1.算菜价(22min)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2090

Problem Description
妈妈每天都要出去买菜,但是回来后,兜里的钱也懒得数一数,到底花了多少钱真是一笔糊涂帐。现在好了,作为好儿子(女儿)的你可以给她用程序算一下了,呵呵。
 
Input
输入含有一些数据组,每组数据包括菜种(字串),数量(计量单位不论,一律为double型数)和单价(double型数,表示人民币元数),因此,每组数据的菜价就是数量乘上单价啊。菜种、数量和单价之间都有空格隔开的。
 
Output
支付菜价的时候,由于最小支付单位是角,所以总是在支付的时候采用四舍五入的方法把分头去掉。最后,请输出一个精度为角的菜价总量。
 
Sample Input
青菜 1 2
罗卜 2 1.5
鸡腿 2 4.2
 
Sample Output
13.4
题解:
         这题主要考察怎么同时读入字符串和数字以及不知道给的样例有多少时对数据的处理。
        思路:
        1.字符串要定义为字符类型。
        2.对scanf()取反可以不断输入,直到遇到文件结束符。
       注意:在使用VS编辑器时,需要在输入字符串后面填上大小,否则编译错误。
       代码如下:
int main()
{
    char vegname[100];
    double count;
    double prise;
    double sum=0;
    while (scanf_s("%s%lf%lf", vegname,100,&count,&prise)!=EOF )
    { 
        sum += count * prise; 
    }
    printf("%0.1lf\n", sum);
    return 0;
}

2.Fibbonacci Number(50min)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2070

Problem Description
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
Note:
you can use 64bit integer: __int64
题解:
        方法:递归或者循环。
        注意:此题可能写的挺快的,但是还是ac不了,原因是看清题目给的数范围,此题结果可能非常大,我按照题目给的类型_int64定义变量,注意输出格式。
       耗时原因:一开始我使用递归做的此题,但是对于数据类型是__int64的返回值不能直接返回__int64,这里找了好多资料页没有找到怎么返回__int64类型的函数值。
代码如下:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int n;
    _int64 result[10000];
    while (~scanf_s("%d",&n))
    { 
        if (n == -1)
            break;
        result[0] = 0;
        result[1] = 1;
        int i = 0;
        for (i = 2; i <= n; i++)
            result[i] = result[i - 1] + result[i - 2];
        printf("%I64d\n", result[n]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ping2yingshi/p/12395220.html