C++数据结构与算法(基础篇)-第三章.基础算法-枚举法

1.枚举法概述

2.步骤

3.找质数问题

#include<iostream>

#include<cmath>

using namespace std;

//质数:(除了1和本身之外没有其余因子)

bool isPrime(int num){

if(num<2) return false;

//枚举所有因子

//① for(int i = 2;i <= num-1;i++){

//②

for(int i = 2;i <= sqrt(num);i++){

if(num%i == 0){//如果在2~num-1中找到了因子

return false;//那么不是质数

}

}

return true;

}

// 17

// 1 2 4 | 4 8 16

int main(){

int n;

cin>>n;

//①时间复杂度-O(n*num)

//②时间复杂度-O(n*sqrt(num))

//1.枚举所有的可行解

for(int i = 2;i <= n;i++){

//2.筛选可行解

if(isPrime(i)){

cout<<i<<endl;

}

}

}

4.父亲的年龄

#include<iostream>

using namespace std;

int main(){

int ans = 0;

for(int f_age = 27; f_age <= 99; f_age++){

int s_age = f_age % 10 * 10 + f_age / 10 % 10;

if(f_age - s_age == 27){

cout<<f_age<<" "<<s_age<<endl;

ans++;

}

}

cout<<"方案数:"<<ans<<endl;

return 0;

}

5.数字组合

#include<iostream>

using namespace std;

int main(){

int n,ans = 0;

cin>>n;

//枚举个位

for(int i = 1; i <= n; i += 2){

//枚举十位

for(int j = 0; j <= n; j++){

if(i != j){

//枚举百位

for(int k = 1; k <= n ; k++){

if(k != j && k != i){

cout<<k*100+j*10+i<<endl;

ans++;

}

}

}

}

}

cout<<"方案:"<<ans<<endl;

return 0;

}

6.找合数

#include<iostream>
#include<cmath>
using namespace std;

bool isComposite(int num) {
    if (num < 4) return false;
    for (int i = 2; i <= sqrt(num); i++) {
        if (num % i == 0) {
            return true;
        }
    }
    return false;
}

int main() {
    int n; cin >> n;
    for (int i = 4; i <= n; i++) {
        if (isComposite(i)) {
            cout << i <<" ";
        }
    }
    return 0;
}

7.找完数

#include<iostream>
using namespace std;

int main (){
    int n,ans=0;
    cin >> n;
    for (int i = 2; i <= n; i++) {
        int sum = 0;//i这个数的因子之和
        for (int j = 1; j <= i - 1; j++) {
            if (i % j == 0){
                sum += j;
            }
        }
        if (sum == i) {
            cout <<i<< " " ;
            ans++;
        }
    }
    cout <<"方案:"<< ans << endl;
    return 0;
}

8.水仙花数

#include<iostream>
using namespace std;

int main (){
    for (int i = 100; i <= 999; i++) {
       int a = i % 10;
       int b = i / 10 % 10;
       int c = i / 100;
        if (a*a*a + b*b*b + c*c*c ==i) {
            cout <<i<< " " ;
        }
    }
    return 0;
}

9.分糖果

#include<iostream>
using namespace std;

int main (){
    int n,l,r;
    cin >> n >> l >>r;
    //时间复杂度:O(1)
    if(l/n == r/n){
        cout << r % n <<endl;
    }
    else{
        cout << n-1 <<endl;
    }
    return 0;
}

10.解密

#include<iostream>
#include<cmath>
using namespace std;

int main (){
    
    //n=p*q   p->[1,m]
    //e*d=(p-1)*(q-1)+1
    //e*d=n-p-q+2
    //p+q=n-e*d+2=m
    //p+q=m->p+n/p=m->p^2-m*p+n=0
    
    
    int k;
    cin >> k;
    long long n,d,e,q;
    while(k--){
        cin >> n >> d >> e;
        long long m = n - e*d + 2;
        long long delta = m*m - 4*n;
        if(delta<0){
            cout <<"NO"<< endl;
        }  
        else if(sqrt(delta) != int(sqrt(delta))){
            cout <<"NO"<< endl;
        }
        else if((m-int(sqrt(delta)))%2!=0){
            cout <<"NO"<< endl;
        }
        else{
            cout<<(m-int(sqrt(delta)))/2<<" "<<
                 (m+int(sqrt(delta)))/2<<endl;
        }
    }
    
    return 0;
}

11.优缺点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

�西瓜�

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

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

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

打赏作者

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

抵扣说明:

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

余额充值