统计素数个数

与之前原理一样。
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;
}