算法一些例子

求字典序在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 maxDis

if __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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值