KY163 素数判定
描述
给定一个数n,要求判断其是否为素数(0,1,负数都是非素数)。
思路
测试到sqrt(x),如果所有数都不能被其整除,x就为素数
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
bool Judge(int x) {
if (x < 2)
return false;
for (int i = 2; i <= sqrt(x); i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
int main() {
int x;
while (cin >> x) {
if (Judge(x))
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
备注
将sqrt函数放在for循环中会让函数多次执行,sqrt函数的计算较复杂,用新变量记录sqrt的值会减少计算量,降低时间复杂度
KY183 素数
描述
输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数,如果没有则输出-1。
思路
代码
#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
vector<int> prime;
bool isPrime[10001];
void Initial() {
for (int i = 0; i < 10001; i++) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i < 10001; i++) {
if (!isPrime[i])
continue;
prime.push_back(i);
for (int j = i* i; j < 10001; j += i) {
isPrime[j] = false;
}
}
}
int main() {
Initial();
int n;
while (cin >> n) {
bool isOutPut = false;
for (int i = 0; i < prime.size() && prime[i] < n; i++) {
if (prime[i] % 10 == 1) {
isOutPut = true;
cout << prime[i];
}
}
if (!isOutPut)
cout << "-1";
}
return 0;
}
备注
continue 会跳过当前循环中的代码,强制开始下一次循环。
KY87 鸡兔同笼
描述
一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外)。已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物。
思路
用贪心算法来计算最大值与最小值
如果输入为奇数,直接返回0 0
代码
#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
int main() {
int a;
while (cin >> a) {
int temp = a;
if (a % 2 == 1){
cout << "0 0" << endl;
continue;
}
int min = 0, max = 0;
while (a != 0) {
a -= 2;
min++;
}
a = temp;
while (a >= 4) {
a -= 4;
max++;
}
if (a != 0)
max++;
cout << max << " " << min << endl;
}
return 0;
}
KY17 n的阶乘
描述
输入一个整数n,输出n的阶乘(每组测试用例可能包含多组数据,请注意处理)
代码
#include <iostream>
using namespace std;
long long F(int x){
if(x == 0)
return 1;
else
return x * F(x - 1);
}
int main() {
long long a;
while (cin >> a) {
cout << F(a);
}
return 0;
}
KY229 阶乘
描述
输入n, 求y1=1!+3!+…m!(m是小于等于n的最大奇数) y2=2!+4!+…p!(p是小于等于n的最大偶数)。
思路
用递归来求阶乘,用for循环来求阶乘的和
代码
#include <iostream>
using namespace std;
long long F(int x) {
if (x == 0)
return 1;
else
return x * F(x - 1);
}
int main() {
int a;
while (cin >> a) {
int n, m;
if (a % 2 == 0) {
n = a - 1;
m = a;
} else {
n = a;
m = a - 1;
}
int ans_n = 0, ans_m = 0;
for (int i = n; i > 0; i -= 2) {
ans_n += F(i);
}
for (int i = m; i > 0; i -= 2) {
ans_m += F(i);
}
cout << ans_n << " " << ans_m << endl;
}
return 0;
}
KY10 球的半径和体积
描述
输入球的中心点和球上某一点的坐标,计算球的半径和体积
输入描述:
球的中心点和球上某一点的坐标,以如下形式输入:x0 y0 z0 x1 y1 z1
输出描述:
输入可能有多组,对于每组输入,输出球的半径和体积,并且结果保留三位小数 为避免精度问题,PI值请使用arccos(-1)。
#include <iostream>
#include<cmath>
using namespace std;
int main() {
double x1, y1, z1, x2, y2, z2;
while (cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2) {
double r = pow(pow(x1 - x2, 2) + pow(y1 - y2, 2) + pow(z1 - z2, 2), 1.0 / 2 );
double pi = acos(-1);
double r3 = pow(r, 3);
double a = 4, b = 3;
double res = a / b;
double v = res *r3* pi;
printf("%0.3f %0.3f", r, v);
}
}
备注
c++中arccos函数是acos
4/3不能直接相除,而是要将它们都转为double类型再除,否则是取整除法
KY36 中位数
描述
中位数定义:一组数据按从小到大的顺序依次排列,处在中间位置的一个数(或最中间两个数据的平均数). 给出一组无序整数,求出中位数,如果求最中间两个数的平均数,向下取整即可(不需要使用浮点数)
输入描述:
该程序包含多组测试数据,每一组测试数据的第一行为N,代表该组测试数据包含的数据个数,1<=N<=10000. 接着N行为N个数据的输入,N=0时结束
代码
#include <asm-generic/errno.h>
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
int count;
while(cin >> count){
if(count == 0)
break;
vector<int> v;
while(count--){
int temp;
cin >> temp;
v.push_back(temp);
}
sort(v.begin(), v.end());
if(v.size() % 2 == 0){
cout << (v[v.size() / 2 - 1] + v[v.size() / 2]) / 2 << endl;
}
else {
cout << v[v.size() / 2] << endl;
}
}
return 0;
}