统计素数个数 与 人民币支付

统计素数个数

与之前原理一样。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int main(){
    long long x,y,num = 0;
    cin >> x >> y;
    
    if(x > y){long t = x;x = y;y = t;}
    
    for(long long i = x;i <= y;i++){//遍历从x到y的所有数字
        bool is1 = true;//判断是否为素数
        if(i != 2){
            if(i % 2 == 0 || i == 1) is1 = false;
            else{
                for(int j = 3;j*j <= i;j += 2){
                    if(i%j == 0){
                        is1 = false;
                        break;
                    }
                }
            }
        }
        else is1 = true;
        if(is1) {num++;}
    }
    cout << num << endl;
    return 0;
}

人民币支付

我的思路是分离这个数的各个数位,依次处理。知道分离到百位,剩下的数即为100纸币的张数。

cpp 复制代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
    int x,t;
    cin >> x;
    int cost[6] = {0,0,0};
    for(int i = 1;i<=3;i++){//从个位起,逐个判断,到百位停下
            t = x % 10;
            if(i == 1){//计算个位人民币
                if(t >= 5) cost[4]++;
                cost[5] = t%5;
            }
            if(i == 2){//计算十位人民币
                if(t >= 5) cost[1]++;
                t = t%5;
                cost[2] = t/2;
                cost[3] = t%2;
            }
            if(i == 3){//现在为百位,直接将其赋值给cost【0】
                cost[0] = t;
            }
            x = x - t;
            x = x / 10;
    }
    
    for(int i=0;i<6;i++){
        cout << cost[i] << endl;
    }
    return 0;
}
相关推荐
西数科技CDRSA3 小时前
BGA封装NAND芯片不识别,Flash Extractor引脚直读还原原始镜像
数据结构·算法·硬件工程
是main不是漫8 小时前
【双指针 | LeetCode11 盛最多水的容器】O(n²) 超时,O(n) 封神:这道“装水题“只差一个思路
开发语言·数据结构·c++·算法
BK贝壳1 天前
布尔数组也能做按位取反、移位和乘法:bool-hybrid-array 的位运算在本机跑了一遍
开发语言·数据结构·python·性能优化·numpy
码完就睡1 天前
数据结构——图的基础概念
数据结构
Go-higher1 天前
C语言--数据结构-栈、队列、堆
c语言·数据结构
郝学胜-神的一滴2 天前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
Regulus-Moon2 天前
Quorum:用一个不等式换掉分布式锁
数据结构·论文阅读·分布式·算法·系统架构
老师我太想进步了20262 天前
【数据结构-16】链式二叉树在线OJ题
c语言·数据结构·算法
程序员大阳2 天前
副队长C语言数据结构教程(4)--双向链表
c语言·数据结构·链表·双向链表