水题总结

来源:互联网 发布:淘宝无线优惠券转化 编辑:程序博客网 时间:2024/06/01 13:59

水平不够,就只能先水水题了。水题也学到了一些新知识。

阶乘的位数:
题目链接:NYIST大一水题之旅 Problem d - ACM在线评测系统
http://acm.nyist.net/JudgeOnline/problem.php?cid=317&cpid=36

数的长度时间限制:3000 ms  |  内存限制:65535 KB描述    N!阶乘是一个非常大的数,大家都知道计算公式是N!=N*(N-1)······*2*1.现在你的任务是计算出N!的位数有多少(十进制)?输入首行输入n,表示有多少组测试数据(n<10)随后n行每行输入一组测试数据 N( 0 < N < 1000000 )输出对于每个数N,输出N!的(十进制)位数。样例输入31332000样例输出11130271/*自己不会的时候都要疯了,查了一下,竟然有个公式。哎呀,自己之前写程序把阶乘求出来可是超时。*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>/*斯特林[striling]公式(求阶乘(n!)的位数)例如1000阶乘位数:log10(1)+log10(2)+···+log10(1000)取整后加1*/int main(){    int N;    scanf("%d",&N);    while(N--)    {        int t;        scanf("%d",&t);        double ans=0; int i;        for(i=1;i<=t;i++)            ans+=log10(i);        printf("%d\n",(int)ans+1);    }    return 0;}

百度百科关于斯特林公式:_百度百科
http://baike.baidu.com/link?url=jf8xGU_bL66gC_cOaH_x896dPqgamkqFidnrANnaapTT4nTm637cihiSN7CuUHhDV2hzPmWllf585a8RHzNf7K

Dinner:NYIST大一水题之旅 Problem g - ACM在线评测系统
http://acm.nyist.net/JudgeOnline/problem.php?cid=317&cpid=39

Dinner
时间限制:100 ms | 内存限制:65535 KB
描述
Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he decided to invite all to have one meal. As bowl, knife and other tableware is not enough in the kitchen, Little A goes to take backup tableware in warehouse. There are many boxes in warehouse, one box contains only one thing, and each box is marked by the name of things inside it. For example, if “basketball” is written on the box, which means the box contains only basketball. With these marks, Little A wants to find out the tableware easily. So, the problem for you is to help him, find out all the tableware from all boxes in the warehouse.
输入
There are many test cases. Each case contains one line, and one integer N at the first, N indicates that there are N boxes in the warehouse. Then N strings follow, each string is one name written on the box.
输出
For each test of the input, output all the name of tableware.
样例输入
3 basketball fork chopsticks
2 bowl letter
样例输出
fork chopsticks
bowl
提示
The tableware only contains: bowl, knife, fork and chopsticks.
来源
辽宁省10年省赛
上传者
ACM_李如兵

主要用到了字符比较。

#include <stdio.h>#include <stdlib.h>#include <string.h>#define maxn 1000+10char a[maxn];int main(){    int N;    while(~scanf("%d",&N))    {        memset(a,0,sizeof(a));        int i;        for(i=0; i<N; i++)        {            scanf("%s",a);            if(strcmp(a,"bowl")==0)            {                printf("bowl");printf(" ");            }            if(strcmp(a,"knife")==0)            {                printf("knife");                printf(" ");            }            if(strcmp(a,"chopsticks")==0)            {                printf("chopsticks");                printf(" ");            }            if(strcmp(a,"fork")==0)            {                printf("fork");                printf(" ");            }       if(i==N-1) printf("\n"); }        //bowl, knife, fork and chopsticks.    }    return 0;}

小明的调查作业:
NYIST大一水题之旅 Problem b - ACM在线评测系统
http://acm.nyist.net/JudgeOnline/problem.php?cid=317&cpid=34
.

主要是要去重,其实去重并不复杂,自己总是给想复杂了。

/*小明的调查作业时间限制:1000 ms  |  内存限制:65535 KB描述小明的老师布置了一份调查作业,小明想在学校中请一些同学一起做一项问卷调查,聪明的小明为了实验的客观性,想利用自己的计算机知识帮助自己。他先用计算机生成了N个1到1000之间的随机整数(0<N≤1000),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。输入输入有2行,第1行为1个正整数,表示所生成的随机数的个数:N第2行有N个用空格隔开的正整数,为所产生的随机数。输出输出也是2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。样例输入1020 40 32 67 40 20 89 300 400 15样例输出815 20 32 40 67 89 300 400来源[hzyqazasdf]原创上传者hzyqazasdf*///练习归并排序#include <stdio.h>#include <stdlib.h>#include <string.h>#define maxn 1000+10int a[maxn],b[maxn];void merge(int *a,int p,int q,int r){    int n1,n2;    n1=q-p+1;    n2=r-q;    int left[n1],right[n2];    int i,j,k;    for(i=0; i<n1; i++)        left[i]=a[i+p];    for(i=0; i<n2; i++)        right[i]=a[q+1+i];    i=j=0;    k=p;    while(i<n1&&j<n2)    {        if(left[i]>right[j])  a[k++]=right[j++];        else a[k++]=left[i++];    }    while(i<n1)    {        a[k++]=left[i++];    }    while(j<n2)    {        a[k++]=right[j++];    }}void mergesort(int *a,int p,int r){    int m;    if(p<r)    {        m=(p+r)/2;        mergesort(a,p,m);        mergesort(a,m+1,r);        merge(a,p,m,r);    }}int main(){    memset(a,0,sizeof(a));    memset(b,0,sizeof(b));    int n;    scanf("%d",&n);    int i;    for(i=0; i<n; i++)        scanf("%d",&a[i]);    mergesort(a,0,n-1);    int k=0;    for(i=0; i<n; i++)        /* printf("%d\n",a[i]); */if(a[i]!=a[i+1]) b[k++]=a[i];        printf("%d\n",k);    for(i=0; i<k; i++)    {        printf("%d",b[i]);        if(i==k-1) printf("\n");        else printf(" ");    }    return 0;}

无主之地1:
NYIST大一水题之旅 Problem L - ACM在线评测系统
http://acm.nyist.net/JudgeOnline/problem.php?cid=317&cpid=12

这一题最可恨,不让排序。

/*无主之地1时间限制:1000 ms  |  内存限制:65535 KB描述子晓最近在玩无主之地1,他对这个游戏的评价不错,结合了FPS与RPG元素,可玩度很高。不过,他发现了一代的任务系统做的不好,任务系统并没有帮他统计清楚哪个区域有多少任务,而且,给任务的时候呢,也比较散乱。比如,在1区域的一个任务点,你领到了4个任务;2区域的一个任务点,你领到了3个任务;游戏一段时间后,你又在1区域另一个任务点个领到了3任务(之前任务没有完成),3区域领到了9个任务……他感觉很凌乱,现在他要设计一个程序来统计每个区域有多少个任务。输入多组测试数据,以输入0 0结束每组数据占一行,输入m区域,n个任务(0<m,n<100)输出输出各个区域的统计结果(不要求排序)样例输入1 32 33 41 70 0样例输出1 102 33 4来源原创上传者TC_高金*/#include <stdio.h>#include <stdlib.h>#include<string.h>#define maxn  100+10int main(){    int m,n;    int i,p=0;    int a[maxn],b[maxn];    memset(a,0,sizeof(a));    memset(b,0,sizeof(b));    while(scanf("%d%d",&m,&n)!=0)    {        if(m==0&&n==0)            break ;        if(a[m]==0)            b[p++]=m;        a[m]+=n;    }    for(i=0; i<p; i++)        printf("%d %d\n",b[i],a[b[i]]);    return 0;}

算菜价:
NYIST大一水题之旅 Problem i - ACM在线评测系统
http://acm.nyist.net/JudgeOnline/problem.php?cid=317&cpid=41

这是自己第一次玩结构体233333
算菜价
时间限制:3000 ms | 内存限制:65535 KB
描述
妈妈每天都要出去买菜,但是回来后,兜里的钱也懒得数一数,到底花了多少钱真是一笔糊涂帐。现在好了,作为好儿子(女儿)的你可以给她用程序算一下了,呵呵。
输入
输入含有一些数据组,每组数据包括菜种(字串),数量(计量单位不论,一律为double型数)和单价(double型数,表示人民币元数),因此,每组数据的菜价就是数量乘上单价啊。菜种、数量和单价之间都有空格隔开的。
注意:程序以文件结束符“EOF”结束输入。
输出
支付菜价的时候,由于最小支付单位是角,所以总是在支付的时候采用四舍五入的方法把分头去掉。最后,请输出一个精度为角的菜价总量。
样例输入
青菜 1 2
罗卜 2 1.5
鸡腿 2 4.2
样例输出
13.4
来源
hdu热身
上传者
王冲5213

//目标:解决算菜价问题#include <stdio.h>#include <stdlib.h>#include <string.h>#define maxn 1000+10//第一次玩结构体struct my_struct{    char s[maxn];    double a;    double b;} pt[maxn];int main(){    int i=0;    while(scanf("%s%lf%lf",pt[i].s,&pt[i].a,&pt[i].b)!=EOF)    {        i++;    }    /*int i;    for(i=0;i<3;i++)        scanf("%s%lf%lf",pt[i].s,&pt[i].a,&pt[i].b);*/    int j;    double sum=0;    for(j=0; j<i; j++)        sum+=pt[j].a*pt[j].b;    printf("%.1lf",sum);    return 0;}

整除的个数:
NYIST大一水题之旅 Problem k - ACM在线评测系统
http://acm.nyist.net/JudgeOnline/problem.php?cid=317&cpid=43

自己把问题给想复杂了,而且还超时,谢谢学长的指点,今天学长不冷漠。

整除个数时间限制:3000 ms  |  内存限制:65535 KB描述1、2、3… …n这n(0<n<=1000000000)个数中有多少个数可以被正整数b整除。输入输入包含多组数据每组数据占一行,每行给出两个正整数n、b。输出输出每组数据相应的结果。样例输入2 15 310 4样例输出212来源自编上传者mix_math
#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){    long  a,b;    while(scanf("%ld%ld",&a,&b)!=EOF)    {                printf("%ld\n",(long)floor(a/b));    }    return 0;}

很多个题,自己都想复杂了。最重要的原因是自己还不擅长这个吧。

0 0
原创粉丝点击