要知道什么是‘递归’.|

2c237c6

园龄:1年4个月粉丝:2关注:0

ACM题解Day7 | 质数素数模块 | 完数难题

学习目标:

博主介绍: 27dCnc
专题 : 数据结构帮助小白快速入门算法
👍👍👍👍👍👍👍👍👍👍👍👍
☆*: .。. o(≧▽≦)o .。.:*☆

Github今日打卡
123

  • ACM题解

学习时间:

  • 周一至周五晚上 7 点—晚上9点
  • 周六上午 9 点-上午 11 点
  • 周日下午 3 点-下午 6 点

学习内容:

  1. 超级楼梯
  2. Octal Fractions(C)
  3. 年号字串
  4. x 进制转 10 进制

内容详细:

完数难题

题目考点: 完数 所有公倍数
1123

思路
将所有倍数统计出然后与其数比较 , 如果相同则统计

#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
#define N 100005
using unl = __int128_t;
using ll = long long;
using namespace  std;
class Solution {
public: //就是一个数的所有公倍数
    void slove() {
        int ans = 0;
        int n,m; cin >> n >> m;
        for (int i = n; i <= m; i++) {
            if (isGood(i)) ans++;
        }
        cout << ans << endl;
    }
    bool isGood(int x) {
        int sum = 0;
        for (int i = 1; i < x; i++) {
            if (x % i == 0)  sum += i;
        }
        if (sum == x) return true;
        else return false;
    }

};


signed main() {

    cin.tie(0) -> ios::sync_with_stdio(0);
    cout.tie(0) -> ios::sync_with_stdio(0);

    #if Run
        int _;cin>>_;while(_--) Solution().slove();
    #else
        Solution().slove();
    #endif

    return 0;
}

饮料换购

题目考试: 统计数 迭代

55555
思路

将 n 不断 除以 3 只要 n < 3 就将最后的数相加到 result

#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
#define N 100005
using unl = __int128_t;
using ll = long long;
using namespace  std;

class Solution {
public: //就是一个数的所有公倍数
    void slove() {
        double n;
        while(cin >> n) {
            double tmp = 0;
            int sum = n;
            while(n >= 3) {
                tmp = tmp + (n / 3.0);
                n /= 3.0;
            }
            cout << round(tmp)  + sum << endl;
        }
    }
};

signed main() {

    cin.tie(0) -> ios::sync_with_stdio(0);
    cout.tie(0) -> ios::sync_with_stdio(0);

    #if Run
        int _;cin>>_;while(_--) Solution().slove();
    #else
        Solution().slove();
    #endif

    return 0;
}

思路
只要当前数仍然为3的倍数就加加表示当前还要一个饮料

#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
#define N 100005
using unl = __int128_t;
using ll = long long;
using namespace  std;

class Solution {
public: //就是一个数的所有公倍数
    void slove() {
        int n; 
        cin>>n;
        
        for(int i=1;i<=n;i++)
        {
            if(i%3==0)
            {
                n++;
            }
        }
        
        cout << n << endl;
    }
};

signed main() {

    cin.tie(0) -> ios::sync_with_stdio(0);
    cout.tie(0) -> ios::sync_with_stdio(0);

    #if Run
        int _;cin>>_;while(_--) Solution().slove();
    #else
        Solution().slove();
    #endif

    return 0;
}

困难的作业

题目考点: 哈希表 字母统计

在这里插入图片描述
思路

用哈希表统计所有字母,然后只要从a到z的顺序输出统计次数

#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
#define N 100005
using unl = __int128_t;
using ll = long long;
using namespace  std;

class Solution {
public: 
    void slove() {
        int hash[256];fill(hash, hash + 256, 0); // 初始化数组
        string s; getline(cin,s);
        for (int i = 0; i < s.size(); i++) {
            hash[s[i]]++;
        }
        for (char i = 'a'; i <= 'z'; i++) {
            cout << hash[i] << " ";
        }
    }
};

signed main() {

    cin.tie(0) -> ios::sync_with_stdio(0);
    cout.tie(0) -> ios::sync_with_stdio(0);

    #if Run
        int _;cin>>_;while(_--) Solution().slove();
    #else
        Solution().slove();
    #endif

    return 0;
}

大头记单词

题目考点: 统计次数 质数判断
在这里插入图片描述
思路
统计次数,判断数是否为质数

在这里插入代码片#include<stdio.h>    
#include<string.h>    
int fun1(int n)  //判断一个数是否为素数    
{    
    int i,j,k;    
    int flag=1; //先假设是素数    
    if(n<2){    
        flag=0;      
    }else{    
        for(i=2;i<n;i++){     
            if(n%i==0){       
                flag=0;  //能被整除,不是素数    
                break;    
            }    
        }    
    }    
    return flag;  //返回判断结果    
}    
void  fun2(char a[],int n,int *max,int *min)     
{     
/****************Begin***************/             
//求字符串字母出现次数最多与最少,用指针返回结果    
    int hash[256]; memset(hash,0,sizeof hash);
//初始化时,max尽可能小,min尽可能大    
      *max = -1000000;
      *min = 1000000;
            
             
 //统计每个字母出现次数    
    for (int i = 0; i < n; i++) {
        hash[a[i]]++;
    }
         
//进行比较,替换最大值与最小值    
    for (int i = 0; i < n; i++) {
        if (hash[a[i]] > *max) {
            *max = hash[a[i]];
        }
        if (hash[a[i]] < *min) { // 修正比较条件
            *min = hash[a[i]];
        }
    }
  //指针返回结果    
/*****************End****************/  
}    
int main(void)    
{    
  char a[100];    
  int i,j,k,n;    
  scanf("%s",&a);  //输入字符串    
  n=strlen(a);  //求出字符串的长度    
  int max,min;    
  fun2(a,n,&max,&min); //调用函数,得出最大值与最小值    
  k=fun1(max-min);   //判断是否满足条件    
  if(k==1){    
      printf("Lucky\n");    
      printf("%d\n",max-min);  //输出结果    
  }else{    
      printf("No Answer\n");    
      printf("0");    
  }    
}  

还剩几盏灯

题目考点: 经典考点

12344
还要思考

#include<stdio.h>                        //1表示灯亮,0表示灯灭  
int main()  
{  
    int n, a[100] = {0},i,j,count = 0;//初始灯数组a[100]值为0,表示灯灭  
    scanf("%d", &n);  
    /**********Begin*************/
    for(j=2;j<2+n;j++){
        for(i=0;i<100;i=i+j){
            a[i]++;
        }
    }
    for(i=0;i<100;i++){
        if(a[i]%2==1){
            count++;
        }
    }
    /**********End*************/
    printf("%d", count);    //输出还有多少灯亮着  
    return 0;  
}  

寻找质数

在这里插入图片描述
统计质数

 #include<bits/stdc++.h>
#define Run 0
#define endl "\n"
#define N 100005
using unl = __int128_t;
using ll = long long;
using namespace  std;

class Solution {
public: //就是一个数的所有公倍数
     void solve() {
        int a, b;
        cin >> a >> b;
        int cnt = 0;
        while (cnt != b) {
            if (IsPrime(a++)) {
                cout << a - 1 << " ";
                cnt++;
            }
        }
    }
    int IsPrime(int n)
    {
        int i;
        if(n<2||(n!=2&&n%2==0))//n小于2或者n是不等于2的偶数,必然非素数
            return 0;
        else//这里n都是奇数
        {//这里使用上面刚提到的写法,用i代替开根号的过程
            for(i=3;i*i<=n;i+=2)//这里注意循环条件
            {//2必然不是因子,从3开始,每次递增2,直到sqrt(n)为止
                if(n%i==0)
                    return 0;
            }
            return 1;
        }
    }

};

signed main() {

    cin.tie(0) -> ios::sync_with_stdio(0);
    cout.tie(0) -> ios::sync_with_stdio(0);

    #if Run
        int _;cin>>_;while(_--) Solution().solve();
    #else
        Solution().solve();
    #endif

    return 0;
}

学习产出:

  • 技术笔记 2 遍
  • CSDN 技术博客 3 篇
  • 习的 vlog 视频 1 个

在这里插入图片描述

重磅消息:

GTP - 4 最新版接入服务他来了 点击链接即可查看详细

GTP - 4 搭建教程

🔥如果此文对你有帮助的话,欢迎💗关注、👍点赞、⭐收藏、✍️评论,支持一下博主~

本文作者:2c237c6

本文链接:https://www.cnblogs.com/27dCnc/p/18568630

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   2c237c6  阅读(5)  评论(0编辑  收藏  举报  
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起