ACM-ICPC基本算法之枚举法

image-20200718102741944

image-20200718102833146

#include <iostream>
using namespace std;
int main()
{
    int i,j,k;
    for(i=0;i<20;i++)
        for(j = 0;j<34;j++)
            for(k=0;k<300;k++)
                if((15*i+9*j+k)==300 && (i+j+k)==100)
                    printf("cook=%d,hen=%d,chicken=%d\n",i,j,k);
    return 0;
}

image-20200718102929543

#include <iostream>
using namespace std;
int main()
{
    int n;
    int i,j;
    int sum = 0;
    cin >> n;
    for(i=2;i<=n;i++)
    {
        sum = 0;
        for(j=1;j<i;j++)
        {
            if(i%j == 0)
                sum += j;
        }
        if(sum == i)
        {
            printf("%d its factors are ",i);
            for(j=1;j<i;j++)
                if(i%j == 0)
                    cout << j << " ";
            cout << endl;
        }
    }
    return 0;
}

image-20200718103128145

#include <iostream>
#include <math.h>
using namespace std;
int isPrime(int n)
{
    double j;
    int i;
    j = sqrt(n);
    for(i=2;i<=j;i++)
        if(n%i == 0)
            return 0;
    return 1;
}
int turn(int n)
{
    int a,b,c;
    a=n/100;
    b=n%100/10;
    c=n%10;
    return c*100+b*10+a;
}
int main()
{
    int i,m,n;
    cin >> m >> n;
    for(i=m;i<=n;i++)
    {
        if((i/100)%2==0)
        {
            i=i+100;
            continue;
        }
        if(isPrime(i)==0)
            continue;
        else if (isPrime(turn(i))&&i<turn(i))
            cout << i << endl;
    }
    return 0;
}

image-20200718103202276

#include <iostream>
#include <string.h>
using namespace std;
//求模式串t的next函数值并存入数组next
int getnext(char *t,int *next,int tlength)
{
    int i = 1,j = 0;
    next[1] = 0;
    while(i<tlength)
    {
        if(j==0||t[i]==t[j])
        {
            ++i;
            ++j;
            next[i]=j;
        }
        else
        {
            j = next[j];
        }  
    }
    return 0;
}
int indexkmp(char *s,char *t,int pos,int tlength,int slength,int *next)
//利用模式串t的next函数求t在主串s中第pos个字符之后的位置
{
    int i = pos,j = 1;
    while(i<=slength && j <= tlength){
        if(j==0||s[i]==t[j])
        {
            ++i;
            ++j;
        }
        else
        {
            j=next[j];
        }
    }
    if(j>tlength)
        return i-tlength;
    else
        return -1;
}
int main()
{
    int locate,tlength,slength,next[256];
    char s[256],t[256];
    slength = strlen(gets(s+1));
    tlength = strlen(gets(t+1));
    getnext(t,next,tlength);
    locate = indexkmp(s,t,0,tlength,slength,next);
    cout << locate << endl;
    return 0;
}

image-20200718103239825

#include <iostream>
using namespace std;
int max(int x,int y,int z)
{
    if(x>y&&x>z)    return x;
    else if(y>x&&y>z)   return y;
    else return z;
}
int main()
{
    int x1,x2,x3,t=1,i,flag,x0;
    cin >> x1 >> x2 >> x3;
    x0 = max(x1,x2,x3);
    for(i=2;i<=x0;i++)
    {
        flag = 1;
        while(flag == 1)
        {
            flag=0;
            if(x1%i==0){
                x1=x1/i;
                flag = 1;
            }
            if(x2%i==0){
                x2=x2/i;
                flag = 1;
            }
            if(x3%i==0){
                x3=x3/i;
                flag=1;
            }
            if(flag==1)
                t*=i;
        }
        x0=max(x1,x2,x3);
    }
    cout << t << endl;
    return 0;
}

image-20200718103302139

#include <iostream>
#include <math.h>
using namespace std;
int warder(int n)
{
    int a[10000];
    int i,j=0,temp;
    for(i=1;i<=n;i++)
    {
        temp=(int)sqrt(i);
        if(temp*temp==i)
            a[j++]=i;
    }
    for(i=0;i<j;i++)
        cout << a[i] << " ";
    return 0;
}
int main()
{
    int n;
    cin >> n;
    warder(n);
    cout << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不能say的秘密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值