• 每日一水 POJ8道水题


    1. POJ 3299 Humidex

    链接: http://poj.org/problem?id=3299

    这道题是已知H,D,T三者的运算关系,然后告诉你其中两个。求另一个。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    int main ()
    {
        char c,h;
        double T,D,H;
        while(cin>>c)
        {
            T=D=H=1000;
            if (c=='E') break;
            if (c=='T') cin>>T;
            else if (c=='D') cin>>D;
            else cin>>H;//用这种方式输入比较方便
            cin>>h;
            if (h=='T') cin>>T;
            else if (h=='D') cin>>D;
            else cin>>H;
            if (H==1000)
            {
                H=T+0.5555*(6.11*exp (5417.7530 * ((1/273.16) - (1/(D+273.16))))-10.0);//exp(t) 表示e的t次
            }
            if (D==1000)
            {
                D=1/(1/273.16-log(((H-T)/0.5555+10.0)/6.11)/5417.7530)-273.16;//log(t) 实际上是lnt
            }
            if (T=1000)
                T=H-0.5555*(6.11*exp (5417.7530 * ((1/273.16) - (1/(D+273.16))))-10.0);
            printf("T %.1f D %.1f H %.1f
    ",T,D,H);//G++上面用.f才AC。
        }
        return 0;
    }
    


    2. POJ 2159 Ancient Cipher

    链接:  http://poj.org/problem?id=2159

    这道题大致是一串密码,经过字母置换和乱序排序之后得到新的一串加密字符串。然后给出一串加密的和一串专家推测出来的密码。问是否为同一个

    这道题其实并不能得到确切的,因为乱序的顺序并没有告诉我们,所以只要判断每个字符出现的次数是否相同即可。

    这道题一开始做的时候被坑了,以为重置的就是简单向右移动一位。。

    //这道题坑爹之处在于密码置换的时候并不是简单的向左加1.。 而是可以任意替换
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int map[30],b[30];
    int main ()
    {
        int i;
        char eng[105];
        char str[105];
        cin>>eng>>str;
        for (i=0; i<strlen(eng); i++)
            map[eng[i]-'A']++;
        for (i=0; i<strlen(str); i++)
            b[str[i]-'A']++;
        sort(map,map+26);
        sort(b,b+26);
        int flag=1;
        for (i=0; i<26; i++)
            if (map[i]!=b[i])
                flag=0;
        if (flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        return 0;
    }
    


    3. POJ 2739 sum of consecutive Primer Numbers

    链接: http://poj.org/problem?id=2739

    这道题的题意是将一个数表示成任意个相邻的素数之和,问能有多少组。

    其实这道题用筛选法筛选出素数表之后,简单枚举就好了

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int prime[10010];
    int a[10010];
    int main ()
    {
        int n,i,j,t=0;
        for (i=2; i<10000; i++)
            if (!prime[i])
            {
                a[t++]=i;
                for (j=2*i; j<10000; j+=i)
                    prime[j]=1;
            }
        while(cin>>n)
        {
            int p=0,s=0;
            if (n==0) break;
            for (i=0; i<t; i++)
            {
                s=0;
                if (a[i]>n) break;
                j=i;
                while(s<n)
                {
                    s+=a[j];
                    j++;
                }
                if (s==n) p++;
                s=0;
            }
            cout<<p<<endl;
        }
        return 0;
    }
    


    4. POJ  2262 Goldbach's Conjecture

    链接:  http://poj.org/problem?id=2262

    哥德巴赫猜想的一个应用吧 将一个数拆分成两个素数之和(均大于2) 要求是两者之间的差最大

    筛选法求素数

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    int a[1000005];
    int main ()
    {
        int i,j,n,t=0;
        for (i=2; i<1000000; i++)
            if (!a[i])
                for (j=2*i; j<1000000; j+=i)
                    a[j]=1;
        while(cin>>n)
        {
            int b=0;
            if (n==0) break;
            for (i=3; i<=n/2; i++)//<=..一开始没有注意到 w了。因为6=3+3
                if (!a[i] && !a[n-i])
                {
                    b=1;
                    break;
                }
            if (b) printf("%d = %d + %d
    ",n,i,n-i);
            else printf("Goldbach's conjecture is wrong.
    ");
        }
        return 0;
    }
    


    POJ 1503 Integer Inquiry

    链接: http://poj.org/problem?id=1503

    高精度运算之大数加法

    要注意前导0的问题。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<ctype.h>
    #include<algorithm>
    using namespace std;
    char num[150][150];
    int sum[150]={0};
    int main ()
    {
        int t=0,i,j,max=0;
        memset(num,0,sizeof(num));
        memset(sum,0,sizeof(sum));
        while(gets(num[t]))
        {
            if (num[t][0]=='0' && strlen(num[t])==1 ) break;
            int len=strlen(num[t]);
            if (max<len) max=len;
            for (i=0, j=len-1; i<j; i++, j--)
            {
                char temp;
                temp=num[t][i];
                num[t][i]=num[t][j];
                num[t][j]=temp;
            }
            t++;
        }
        for (j=0; j<=max; j++)
        {
            for (i=0; i<t; i++)
                if (isdigit(num[i][j]))
                sum[j]+=num[i][j]-'0';
            if (sum[j]>=10)
            {
                sum[j+1]+=sum[j]/10;
                sum[j]=sum[j]%10;
            }
        }
        while(sum[j]==0)
            j--;
        for (i=j; i>=0; i--)
            cout<<sum[i];
        cout<<endl;
        return 0;
    }
    


    POJ 3006 Dirichlet's Theorem on Arithmetic Progressions 

    狄利克雷定理

    链接: http://poj.org/problem?id=3006

    a,b互质,在等差序列a,a+b,a+2b,a+3b……当中有很多个素数。。 给出a,b,n。求出等差数列当中第n个素数。

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int is[1000000];
    int main ()
    {
        is[0]=is[1]=1;
        int i,j,a,b,n;
        for (i=2; i<1000000; i++)
            if (!is[i])
                for (j=2*i; j<1000000; j+=i)
                    is[j]=1;
        while(cin>>a>>b>>n)
        {
            if (a==0) break;
            a-=b;
            while(n)
            {
                a+=b;
                if (!is[a]) n--;
            }
            cout<<a<<endl;
        }
        return 0;
    }
    


    POJ 3094 Quicksum

    http://poj.org/problem?id=3094

    A=1,B=2,……,Z=26,空格=0. 求出一串字符串的值

    例如 ACM:=1*1+2*3+3*13=46

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    using namespace std;
    int main ()
    {
        char str[260];
        while(gets(str))
        {
            int sum=0,i,len=strlen(str);
            if (str[0]=='#') break;
            for (i=0; i<len; i++)
                if (str[i]!=' ')
                    sum+=(str[i]-'A'+1)*(i+1);
            cout<<sum<<endl;
        }
        return 0;
    }
    


    POJ 2255 Tree Recovery

    链接: http://poj.org/problem?id=2255

    这道题是二叉树重建的裸题

    给你二叉树的前序和中序遍历。让你求出后序遍历。

    注意递归的利用

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    void build(char *in, char *pre, char *post, int n)
    {
        int i=0;
        if (n<=0) return;
        while(*(in+i)!=*(pre)) i++;
        build(in,pre+1,post,i);
        build(in+i+1,pre+i+1,post+i,n-i-1);
        post[n-1]=*pre;
    }
    int main ()
    {
        char in[30],pre[30],post[30];
        while(cin>>pre>>in)
        {
            int n;
            memset(post,0,sizeof(post));
            n=strlen(in);
            build(in,pre,post,n);
            cout<<post<<endl;
        }
        return 0;
    }
    



  • 相关阅读:
    LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
    LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
    LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
    LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
    LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
    LeetCode 617. 合并二叉树(Merge Two Binary Trees)
    LeetCode 206. 反转链表(Reverse Linked List) 16
    LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
    LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
    LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3253835.html
Copyright © 2020-2023  润新知