东方博宜oj答案1221-1240
写在前面
想要看前面的答案可以去翻我以前的文章,就不发链接了,因为到时候文章多了链接可能会很多。大家一起加油,最近开始学数据结构了,全英,想死。
感谢"sad明"提出的一些建议。
1221
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
cin >> n;
int a[100];
double num = 0;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (a[j] < a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
num = (a[0] + a[1] + a[2] + a[3] + a[4]) / 5.0;
cout << fixed << setprecision(1) << num;
}
1222
很经典的递归,我觉得甚至都可以把这个代码给背下来。
#include <iostream>
using namespace std;
void hanoi(int n, char a, char b, char c)
{
if (n >= 1)
{
hanoi(n - 1, a, c, b);
cout << a << " To " << c << endl;
hanoi(n - 1, b, a, c);
}
}
int main()
{
int m;
cin >> m;
hanoi(m, 'A', 'B', 'C');
return 0;
}
1223
#include <iostream>
using namespace std;
int num = 0;
int hanoi(int n, char a, char b, char c)
{
if (n >= 1)
{
hanoi(n - 1, a, c, b);
num++;
hanoi(n - 1, b, a, c);
}
return num;
}
int main()
{
int m;
cin >> m;
cout << hanoi(m, 'A', 'B', 'C');
return 0;
}
1224
来自洛谷过河卒题解中Chiaro的答案。等我有空自己写一个。
#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ll long long
inline int read(){
int num = 0; char c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)) num = (num << 1) + (num << 3) + (c ^ '0'), c = getchar();
return num;
}
int bx, by, mx, my;
ll f[30];
inline bool check(int x, int y) {
if(x == mx && y == my) return 1;
return (std::abs(mx - x) + std::abs(my - y) == 3) && (std::max ((std::abs(mx - x)), std::abs(my - y)) == 2);
}
int main(){
bx = read() + 2, by = read() + 2, mx = read() + 2, my = read() + 2;
f[2] = 1;
for(int i = 2; i <= bx; i++){
for(int j = 2; j <= by; j++){
if(check(i, j)){
f[j] = 0;
continue;
}
f[j] += f[j - 1];
}
}
printf("%lld\n", f[by]);
return 0;
}
1225
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n - 1; i++)
{
for (int j = 1; j <= n - i; j++)
{
cout << " ";
}
cout << "*";
if (i > 1)
{
for (int j = 1; j <= 2 * i - 3; j++)
{
cout << " ";
}
cout << "*";
}
cout << endl;
}
for (int i = 1; i <= 2 * n - 1; i++)
{
cout << "*";
}
return 0;
}
1226
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= 2 * n - 1; i++)
{
if (i == 1 || i == 2 * n - 1)
{
for (int j = 1; j <= n - 1; j++)
{
cout << " ";
}
for (int j = 1; j <= n; j++)
{
cout << "*";
}
}
if (i > 1 && i <= n)
{
for (int j = 1; j <= n - i; j++)
{
cout << " ";
}
cout << "*";
for (int j = 1; j <= n + 2 * (i-2) ; j++)
{
cout << " ";
}
cout << "*";
}
if(i>n&&i<2*n-1)
{
for (int j = 1; j <= i - n; j++)
{
cout << " ";
}
cout << "*";
for (int j = 1; j <= -2*i+5*n-4; j++)
{
cout << " ";
}
cout << "*";
}
cout << endl;
}
return 0;
}
1227
#include <iostream>
using namespace std;
int main()
{
int money, big_bowl_price, small_bowl_price;
cin >> money >> big_bowl_price >> small_bowl_price;
for (int i = 1; i <= money / big_bowl_price; i++)
{
for (int j = 1; j <= money / small_bowl_price; j++)
{
if (i % 2 == 0 && j % 2 == 0 && big_bowl_price * i + small_bowl_price * j == money)
{
cout << i << " " << j << endl;
}
}
}
return 0;
}
1228
28、29以及35、36、37、38都是算法题,等学完了我再来补,实在是非常抱歉因为水平就到这里,我也绝对不能说故意写个错误答案什么的。
1229
1230
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
if (i >= 1 && i <= (n + 1) / 2)
{
for (int j = 1;j<=-i+0.5*n+1.5;j++)
{
cout << "*";
}
}
else
{
for (int j = 1; j <= i - 0.5 * n + 0.5; j++)
{
cout << "*";
}
}
cout << endl;
}
return 0;
}
1231
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
cin >> n;
int* p = new int[n];
double average = 0;
for (int i = 0; i < n; i++)
{
cin >> p[i];
average += p[i];
}
average /= n;
int num_higher = 0, num_lower = 0;
for (int i = 0; i < n; i++)
{
if (p[i] > average)
{
num_higher++;
}
if (p[i] < average)
{
num_lower++;
}
}
cout << fixed << setprecision(1) << average;
cout << " " << num_higher << " " << num_lower;
delete[]p;
return 0;
}
1232
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
cin >> n;
int* p = new int[n];
int max = 0, max_loc = 0;
int min = 0, min_loc = 0;
for (int i = 0; i < n; i++)
{
cin >> p[i];
if (i == 0)
{
max = p[i];
min = p[i];
max_loc = 0;
min_loc = 0;
}
if (p[i] > max)
{
max = p[i];
max_loc = i;
}
if (p[i] < min)
{
min = p[i];
min_loc = i;
}
}
int temp = p[max_loc];
p[max_loc] = p[min_loc];
p[min_loc] = temp;
for (int i = 0; i < n; i++)
{
cout << p[i] << " ";
}
delete[]p;
return 0;
}
1233
注意当个数为偶数的时候要除2的话得写成2.0
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
int n;
cin >> n;
int* p = new int[n];
for (int i = 0; i < n; i++)
{
cin >> p[i];
}
sort(p, p + n);
double num = 0;
if (n % 2 == 1)
{
num = p[(n - 1) / 2];
cout << fixed << setprecision(1) << num;
}
else
{
num = (p[(n - 1) / 2] + p[(n + 1) / 2]) / 2.0;
cout << fixed << setprecision(1) << num;
}
delete[]p;
return 0;
}
1234
#include <iostream>
#include <cmath>
using namespace std;
bool prime(int& n)
{
int prime = 1;
for (int i = 2; i <= sqrt(n); i++)
{
if (n % i == 0)
{
prime = 0;
break;
}
}
if (prime == 1)
{
return true;
}
else
{
return false;
}
}
void analyze(int &a, int &b)//传引用可以对实参进行修改
{
int i;
for (i = 2;; i++)
{
if (prime(i) == 1 && a % i == 0)
{
//每次读到质因数就输出该质因数
cout << i << " ";
break;
}
}
//让给定的整数去除以该质因数
a /= i;
b++;
if (a != 1)
{
//递归真nm好用
analyze(a,b);
}
else
{
//此时是a==1,说明没有更多的除了本身以外的因数了,所以就return,因为是void所以返回空
return;
}
}
int main()
{
int n;
cin >> n;
int num = 0;
analyze(n, num);
cout << endl << num;
return 0;
}
1235
1236
1237
1238
1239
高端的程序往往采用最朴素的办法(雾
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
if (i < (n + 1) / 2)
{
for (int j = 1; j < i; j++)
{
cout << " ";
}
cout << "X";
for (int j = 1; j <= -2 * i + n; j++)
{
cout << " ";
}
cout << "X";
}
if (i == (n + 1) / 2)
{
for (int j = 1; j <= (n + 1) / 2 - 1; j++)
{
cout << " ";
}
cout << "X";
}
if (i > (n + 1) / 2)
{
for (int j = -i+n; j > 0; j--)
{
cout << " ";
}
cout << "X";
for (int j = 1; j <= 2 * i - n - 2; j++)
{
cout << " ";
}
cout << "X";
}
cout << endl;
}
return 0;
}
1240
#include <iostream>
using namespace std;
void judge(int num)
{
if (num == 1)
{
cout << "yi ";
}
if (num == 2)
{
cout << "er ";
}
if (num == 3)
{
cout << "san ";
}
if (num == 4)
{
cout << "si ";
}
if (num == 5)
{
cout << "wu ";
}
if (num == 6)
{
cout << "liu ";
}
if (num == 7)
{
cout << "qi ";
}
if (num == 8)
{
cout << "ba ";
}
if (num == 9)
{
cout << "jiu ";
}
}
int main()
{
int n;
cin >> n;
if (n / 10 == 0)
{
judge(n);
}
else if (n / 10 == 1)
{
cout << "shi ";
judge(n % 10);
}
else
{
judge(n / 10);
cout << "shi ";
judge(n % 10);
}
return 0;
}