算法一些例子

来源:互联网 发布:淘宝店铺类目怎么修改 编辑:程序博客网 时间:2024/05/29 15:03

求字典序在s1和s2之间的,长度在len1到len2的字符串的个数,结果mod 1000007

#include<iostream>#include<string>#include<vector>#include<math.h>using namespace std;int main(){    string s1,s2;    int len1,len2;    while(cin>>s1>>s2>>len1>>len2){        //只包含小写字母的字符串可以看成26进制的数制        //将s1和s2补长到len2长度        s1.append(len2-s1.size(),'a');        s2.append(len2-s2.size(),(char)('z'+1));        vector<int> array;        for(int i=0;i<len2;i++){            array.push_back(s2[i]-s1[i]);        }        int result = 0;        for(int i=len1;i<=len2;i++){            for(int k=0;k<i;k++){                result += array[k]*pow(26,i-1-k);            }        }        //所有字符串最后都不包含是s2自身,所以最后要减1;        cout<<result-1<<endl;    }    return 0;}

已知某公司总人数为W,平均年龄为Y岁(每年3月末计算,同时每年3月初入职新人),假设每年离职率为x,x>0&&x<1,每年保持所有员工总数不变进行招聘,新员工平均年龄21岁。
从今年3月末开始,请实现一个算法,可以计算出第N年后公司员工的平均年龄。(结果向上取整)。

#include <iostream>#include <cmath>using namespace std;void AverageAge(){    // 总人数为W,平均年龄为Y岁    // 每年离职率为x,x>0&&x<1    double W, Y, x, N;    while(cin >> W >> Y >> x >> N)    {        while(N--)        {            Y = 21 * x + (1 - x) * (Y + 1);        }        cout << ceil(Y) << endl;    }}int main(){    AverageAge();    return 0;}

有一个长为n的数组A,求满足0≤a≤b

def maxdis(A,N):    maxDis=0;    minIndex=0;    ite=0;    for i in A:        if(i-A[minIndex]>maxDis):            maxDis=A[i]-A[minIndex]        if(i<A[minIndex]):            minIndex=ite        ite+=1    return maxDisif __name__ == '__main__':    A=[10,5]    LONG=maxdis(A,len(A))    print LONG
#include<iostream>#include<string>#include<stdlib.h>using namespace std;unsigned int max(unsigned int *a){    unsigned int temp;    temp=a[0];    for (int i=1;i<10;i++)    {        if(a[i]>temp)            temp=a[i];    }    return temp;}int main(){    unsigned int n,m;    unsigned int tag=0;    while(cin>>n>>m)    {         unsigned int *d,*h;        d=new  unsigned int[n];        h=new  unsigned int[m];        for(unsigned int i=0;i<m;++i)        {            cin>>d[i];            cin>>h[i];        }        for(unsigned int i=0;i<m;i++)        {            for( unsigned int j=i+1;j<m;j++)            {                if(d[i]>n || ((h[j]-h[i])/(d[j]-d[i])>1))                    tag=-1;//imposible                else                    tag=max(h);            }        }        if(tag==-1)            cout<<"IMPOSIBLE"<<endl;        else            cout<<tag+2<<endl;    }}
0 0
原创粉丝点击