# 北大慕课（郭炜）：程序设计与算法（一）编程题答案（全）

4 篇文章 6 订阅

OpenJudge链接：http://cxsjsxmooc.openjudge.cn/2021t1springall/

001:输出第二个整数

# include<iostream>
using namespace std;
int main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%d", b);
}


002:字符菱形

# include<iostream>
using namespace std;
int main()
{
char a;
scanf("%c", &a);
printf("  %c  \n", a);
printf(" %c%c%c \n", a,a,a);
printf("%c%c%c%c%c\n", a,a,a,a,a);
printf(" %c%c%c \n", a,a,a);
printf("  %c  \n", a);
}


003:打印ASCII码

# include<iostream>
using namespace std;
int main()
{
char a;
scanf("%c", &a);
printf("%d", a);
}


004:打印字符

# include<iostream>
using namespace std;
int main()
{
int a;
scanf("%d", &a);
printf("%c", a);
}


005:整型数据类型存储空间大小

# include<iostream>
using namespace std;
int main()
{
int a;
short b;
printf("%d %d", sizeof(a), sizeof(b));
}


006:浮点型数据类型存储空间大小

# include<iostream>
using namespace std;
int main()
{
float a;
double b;
printf("%d %d", sizeof(a), sizeof(b));
}


007:对齐输出

# include<iostream>
using namespace std;
int main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%8d %8d %8d", a, b, c);
}


008:输出保留12位小数的浮点数

# include<iostream>
using namespace std;
int main()
{
double a;
scanf("%lf", &a);
printf("%.12f", a);

}


009:空格分隔输出

# include<iostream>
using namespace std;
int main()
{
char a;
int b;
float c;
double d;
scanf("%c", &a);
scanf("%d", &b);
scanf("%f", &c);
scanf("%lf", &d);
printf("%c %d %f %lf", a, b, c, d);
}


010:计算球的体积

# include<iostream>
using namespace std;
int main()
{
double r;
double V;
scanf("%lf", &r);
V = 4 * 3.14 * r * r * r / 3;
printf("%.2lf", V);
}


011:大象喝水

# include<iostream>
using namespace std;
int main()
{
int r;
int h;
double V;
scanf("%d %d", &h, &r);
V = 3.14 * r * r *h;
int geshu;
geshu = int(20000/V)  + 1;
printf("%d", geshu);
}


012:奇偶数判断

# include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n % 2 == 1) cout << "odd";
else cout << "even";
}


013:求一元二次方程的根

#include <iostream>
#include <cstdio>
#include<cmath>
using namespace std;
double eps = 1e-10;
int main()
{
double a, b, c, delta,rel,img;
cin >> a >> b >> c;
delta = b * b - 4 * a * c;
if (delta < eps && -delta < eps)
printf("x1=x2=%.5f",-b / (2 * a));
else if (delta < 0) {
rel = -b / (2 * a);
img = sqrt(-delta) / (2 * a);
if (rel < eps && -rel < eps)
rel = 0;
printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi", rel, img, rel, img);
}
else {
printf("x1=%.5f;x2=%.5f", (-b + sqrt(delta)) / (2 * a), (-b - sqrt(delta)) / (2 * a));
}
return 0;

}


014:点和正方形的关系

# include<iostream>
using namespace std;
int main()
{
int x, y;
cin >> x >> y;
if (x >= -1 && x <= 1) {
if (y >= -1 && y <= 1) cout << "yes";
else cout << "no";
}
else cout << "no";
return 0;
}


015:苹果和虫子2

# include<iostream>
using namespace std;
int main()
{
int n, x, y, geshu;
cin >> n >> x >> y;
if (y % x == 0) geshu = n - y / x;
else geshu = n - y / x - 1;
if (geshu >= 0) cout << geshu;
else cout << 0;
return 0;
}


016:简单计算器

# include<iostream>
using namespace std;
int main()
{
int x, y;
char c;
cin >> x >> y >> c;
switch (c) {
case '+':
cout << x + y;
break;
case '-':
cout << x - y;
break;
case '*':
cout << x * y;
break;
case '/':
if (y == 0) cout << "Divided by zero!";
else cout << x / y;
break;
default:
cout << "Invalid operator!";
break;
}
return 0;

}


017:求整数的和与均值

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n,sum=0,a;
double average;
cin >> n;
for (int i = 0;i < n;i++) {
cin >> a;
sum += a;
}
printf("%d %.5f", sum, double(sum) / n);
}


018:整数序列的元素最大跨度值

# include<iostream>
using namespace std;
int main()
{
int n, min = 1001, max = -1, num;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> num;
if (num < min) min = num;
if (num > max) max = num;
}
cout << max - min;
return 0;
}


019:奥运奖牌计数

# include<iostream>
using namespace std;
int main()
{
int n, jing, ying, tong;
int j=0, y=0, t=0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> jing >> ying >> tong;
j += jing;
y += ying;
t += tong;
}
cout << j << " " << y << " " << t << " " << j + y + t;
return 0;
}


020:乘方计算

# include<iostream>
using namespace std;
int main()
{
int a, n, out;
cin >> a >> n;
out = 1;
for (int i = 0; i < n; i++) {
out *= a;
}
cout << out;

return 0;
}


021:鸡尾酒疗法

# include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int ji_z, ji_y;
cin >> ji_z >> ji_y;
float ji_l = (float)ji_y / ji_z;
int qi_z, qi_y;
float qi_l;
for (int i = 0; i < n - 1; i++) {
cin >> qi_z >> qi_y;
qi_l = float(qi_y) / qi_z;
if ((qi_l - ji_l) < 0.05 && (qi_l - ji_l) > -0.05) cout << "same" << endl;
if ((qi_l - ji_l) > 0.05) cout << "better" << endl;
if ((ji_l - qi_l) > 0.05) cout << "worse" << endl;
}
return 0;
}


022:角谷猜想

# include<iostream>
using namespace std;
int main()
{
long long n;
cin >> n;
while (n != 1) {
if (n % 2 == 1) {
printf("%lld*3+1=%lld\n", n, n * 3 + 1);
n = n * 3 + 1;
}
else {
printf("%lld/2=%lld\n", n, n / 2);
n = n / 2;
}
}
cout << "End\n";
return 0;
}


023:正常血压

# include<iostream>
using namespace std;
int main()
{
int n,shou,shu,lianxu=0,max_lianxu=0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> shou >> shu;
if (shou>=90&&shou<=140) {
if (shu >= 60 && shu <= 90) {
lianxu += 1;
max_lianxu = max(lianxu, max_lianxu);
}
else {
max_lianxu = max(lianxu, max_lianxu);
lianxu = 0;
}
}
else {
max_lianxu = max(lianxu, max_lianxu);
lianxu = 0;
}
}
cout << max_lianxu;
return 0;
}


024:数字反转

# include<iostream>
using namespace std;
int main()
{
long long n;
int diyiwei = 0;
cin >> n;
if (n < 0) {
cout << "-";
n = -n;
}
while (n>0) {
int yushu = n % 10;
n = n / 10;
if (yushu!=0) diyiwei = 1;
if(yushu!=0||diyiwei==1)
cout << yushu;
}
return 0;
}


025:求特殊自然数

# include<iostream>
using namespace std;
//这个数一定在81-342范围里
int main()
{
for (int i = 81; i < 343; i++) {
//转为7进制
int j = i;
int yushu11 = j % 7;
j = j / 7;
int yushu12 = j % 7;
j = j / 7;
int yushu13 = j % 7;
j = j / 7;
//转为9进制
j = i;
int yushu21 = j % 9;
j = j / 9;
int yushu22 = j % 9;
j = j / 9;
int yushu23 = j % 9;
j = j / 9;
//看是不是相反的关系
if (yushu11 == yushu23 && yushu12 == yushu22 && yushu13 == yushu21) {
cout << i<<endl;
cout << yushu13 << yushu12 << yushu11<<endl;
cout << yushu23 << yushu22 << yushu21<<endl;
}
//是就输出，再找找后面的
}
return 0;
}


026:雇佣兵

# include<iostream>
using namespace std;
int main()
{
int M, N, X, m, n;
cin >> M >> N >> X;
//n是当前战斗力，m是当前体力
n = N;
m = 0;
while (n <= M && X > 0) {
//用能量棒恢复能量
if (M % n == 0) {
// 刚好可以用M/n个能量棒回满能量、
if (X >= M / n) {
X = X - M / n;
m = M;
}
else {
/*m = X * n;
X = 0;*/
break;
}
}
else {
//得多用一个哦
if (X >= M / n + 1) {
m = M;
X = X - M / n - 1;
}
else {
/*m = X * n;
X = 0;*/
break;
}
}
//开始战斗，提升战斗力
n += m / n;
//体力变成0
m = 0;
}
cout << n;
return 0;
}


027:数字统计

#include <iostream>
using namespace std;
int main()
{
int A,B;
int n=0;
cin >> A >> B;
for (;A<=B;A++)
{
int i=A;
while(i>0)
{
if(i%10==2) n++;
i/=10;
}
}
cout << n << endl;
return 0;
}


028:与指定数字相同的数的个数

# include<iostream>
using namespace std;

int main()
{
int N;
cin >> N;
int M[110];
for (int i = 0; i < N; ++i) {
cin >> M[i];
}
int m, geshu=0;
cin >> m;
for (int i = 0; i < N; ++i){
if (M[i] == m)  geshu++;
}
cout << geshu;

return 0;
}


029:陶陶摘苹果

# include<iostream>
using namespace std;

int main()
{
int height[10];
for (int i = 0; i < 10; i++) {
cin >> height[i];

}
int taotao, geshu=0;
cin >> taotao;
taotao += 30;
for (int i = 0; i < 10; i++) {
if (height[i] <= taotao) geshu++;
}
cout << geshu;
return 0;
}


030:年龄与疾病

# include<iostream>
using namespace std;

int main()
{
int n;
cin >> n;
int a[101];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int zu0_18 = 0, zu19_35=0, zu36_60=0, zu61_=0;
for (int i = 0; i < n; i++) {
if (a[i] >= 0 && a[i] <= 18) zu0_18++;
else if (a[i] >= 19 && a[i] <= 35) zu19_35++;
else if (a[i] >= 35 && a[i] <= 60) zu36_60++;
else if (a[i] >= 61) zu61_++;
}
float zu1, zu2, zu3, zu4;
zu1 = float(zu0_18) / n * 100;
zu2 = float(zu19_35) / n * 100;
zu3 = float(zu36_60) / n * 100;
zu4 = float(zu61_) / n * 100;
printf("%.2f", zu1);
printf("%%\n");
printf("%.2f",  zu2);
printf("%%\n");
printf("%.2f",  zu3);
printf("%%\n");
printf("%.2f",  zu4);
printf("%%\n");
}


031:校门外的树

# include<iostream>
using namespace std;
//搞一个数组，存放所有马路上的树，有树为1.移走为0
int main()
{
int L, M;
cin >> L >> M;
int s[10001];
for (int i = 0; i <= L; i++) {
s[i] = 1;
}
int low, high;
for (int i = 0; i < M; i++) {
cin >> low >> high;
for (int j = low; j <= high; j++) {
s[j] = 0;
}
}
int geshu = 0;
for (int i = 0; i <= L; i++) {
if (s[i]) geshu++;
}
cout << geshu;
return 0;
}


032:计算鞍点

# include<iostream>
using namespace std;
int s[5][5];
int main()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cin >> s[i][j];
}
}
int zuida, zuixiao, zhaodao = 0;
for (int i = 0; i < 5; i++) {
zuida = s[i][0];
int zuida_j = 0;
for (int j = 1; j < 5; j++) {
if (s[i][j] > zuida) {
zuida = s[i][j];
zuida_j = j;
}
}
zuixiao = s[0][zuida_j];
int zuixiao_i = 0;
for (int m = 1; m < 5; m++) {
if(s[m][zuida_j]<zuixiao){
zuixiao = s[m][zuida_j];
zuixiao_i = m;
}
}
if (zuida == zuixiao) {
cout << i+1 << " " << zuida_j+1 << " " << zuida;
zhaodao = 1;
}
}
return 0;
}


033:图像模糊处理

# include<iostream>
#include<math.h>
using namespace std;
int p[110][110], p1[110][110];
int main()
{
int n, m;
cin >> n >> m;
for (int i=0; i < n; i++) {
for (int j=0; j < m; j++) {
cin >> p[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 || j == 0||i==n-1||j==m-1) {
p1[i][j] = p[i][j];
}
else
p1[i][j] = round(float(p[i - 1][j] + p[i][j - 1] + p[i + 1][j] + p[i][j + 1]+p[i][j]) / 5);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << p1[i][j] << " ";
}
cout << endl;
}
return 0;
}


034:矩阵转置

#include <iostream>
using namespace std;
#define N 100
int n,m;
int a[N][N];
int main(){
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){	//矩阵转置后行数和例数也随之转置
printf("%d ",a[j][i]);
}
printf("\n");
}
return 0;
}


035:Pell数列

# include<iostream>
# include<math.h>
using namespace std;
int a[101], p[1000010];

int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
p[1] = 1;
p[2] = 2;
for (int i = 3; i < 1000010; i++) {
p[i] = (2 * p[i - 1] + p[i - 2]) % 32767;
}
for (int i = 0; i < n; i++) {
cout << p[a[i]] << endl;
}

return 0;
}


036:求最大公约数问题

# include<iostream>
# include<math.h>
using namespace std;

int main()
{
int m, n, temp;
cin >> m >> n;
if (m > n) {
temp = m;
m = n;
n = temp;
}
//现在确保m是较小的那个数了
int yueshu=0;
for (int i = 1; i <= m; i++) {
if (m % i == 0) {
if (n % i == 0) {
yueshu = i;
}
}
}
cout << yueshu;
return 0;

}


037:编程填空：第i位替换

#include <iostream>
using namespace std;

int bitManipulation1(int n, int m, int i) {
return ((1 << (i)) & m) + ((~(1 << (i))) & n);
}

int main() {
int n, m, i, t;
cin >> t;
while (t--) {
cin >> n >> m >> i;
cout << bitManipulation1(n, m, i) << endl;
}
return 0;
}


038:编程填空：第i位取反

#include <iostream>
using namespace std;

int bitManipulation2(int n, int i) {
return (1 << i) ^ n;
}

int main() {
int t, n, i;
cin >> t;
while (t--) {
cin >> n >> i;
cout << bitManipulation2(n, i) << endl;
}
return 0;
}


039:编程填空：左边i位取反

#include <iostream>
using namespace std;

int bitManipulation3(int n, int i) {
return (-1 << (32-i)) ^ n;
}

int main() {
int t, n, i;
cin >> t;
while (t--) {
cin >> n >> i;
cout << bitManipulation3(n, i) << endl;
}
return 0;
}


040:统计数字字符个数

#include <iostream>
using namespace std;

char line[260];
int geshu=0;
int main() {
cin.getline(line, 260);
for (int i = 0; line[i]; ++i) {
if (line[i] >= '0' && line[i] <= '9')
geshu++;
}
cout << geshu;
return 0;
}


041:找第一个只出现一次的字符

#include <iostream>
using namespace std;
char shuru[100000];
int zimu[26];
int cunzai = 0, chongfu = 0;
int main() {
cin >> shuru;
for (int i = 0; shuru[i]; ++i) {
chongfu = 0;
if (shuru[i] != 'H') {
for (int j = i+1; shuru[j]; ++j) {
if (shuru[j] == shuru[i]) {
shuru[j] = 'H';
chongfu = 1;
}
}
if (chongfu == 0) {
cout << shuru[i];
cunzai = 1;
break;
}
}
}
if (cunzai == 0) {
cout << "no";
}
return 0;

}


042:石头剪子布

#include <iostream>
#include <cstring>
using namespace std;
int N;
char p1[10], p2[10];
int n[110];
int main() {
cin >> N;
for (int i=0;i<N;++i){
cin >> p1 >> p2;
if (strcmp(p1, "Rock")==0) {
if (strcmp(p2, "Rock") == 0) n[i]=0;
if (strcmp(p2, "Scissors") == 0) n[i]=1;
if (strcmp(p2, "Paper") == 0) n[i] = 2;
}
if (strcmp(p1, "Scissors")==0) {
if (strcmp(p2, "Rock") == 0) n[i] = 2;
if (strcmp(p2, "Scissors") == 0) n[i] = 0;
if (strcmp(p2, "Paper")==0) n[i] = 1;
}
if (strcmp(p1, "Paper")==0) {
if (strcmp(p2, "Rock") == 0) n[i] = 1;
if (strcmp(p2, "Scissors") == 0) n[i] = 2;
if (strcmp(p2, "Paper")==0) n[i] = 0;
}
}
for (int i = 0; i < N; i++) {
if (n[i] == 0) cout << "Tie\n";
if (n[i] == 1) cout << "Player1\n";
if (n[i] == 2) cout << "Player2\n";
}
return 0;

}


043:最长最短单词

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
char s[10000];
int len;
int sum = 0, max_len = 0, min_len = 9999;
int max_i=0, min_i=0;
int i;
cin.getline(s,10000);
len = strlen(s);//字符长度
s[len] = ' ';//在字符串后加以空格，方便判定

for (i = 0; i < len+1; i++)
{
if (s[i] != ' ' && s[i] != ',')	sum++;//若不是空格或逗号，累加单词长度
else if (sum > 0)//若是空格或逗号
{
if (sum > max_len)
{
max_len = sum;//存储目前最长的单词长度
max_i = i - sum;//记录最长单词的位置
}
if (sum < min_len)
{
min_len = sum;//存储目前最短的单词长度
min_i = i - sum;//记录最短单词的位置
}
sum = 0;//计数器归零
}
}
for (i = max_i; i <= max_i + max_len - 1; i++)
cout << s[i];//输出最长单词
cout << endl;
for (i = min_i; i <= min_i + min_len - 1; i++)
cout << s[i];//输出最短单词
return 0;
}


044:密码翻译

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char line[90];
int main()
{
cin.getline(line, 85);
int len = strlen(line);
for (int i = 0; i < len; i++) {
if (line[i] == 'z') line[i] = 'a';
else if (line[i] == 'Z') line[i] = 'A';
else if ((line[i] >= 'a' && line[i] < 'z') ||
(line[i] >= 'A' && line[i] < 'Z'))
line[i] += 1;

}
for (int i = 0; i < len; i++) {
cout << line[i];
}
return 0;
}


045:指针练习：输出Hello

#include <iostream>
using namespace std;
int main() {
char s[] = "Hello";
char * p;
for(
p=s;*p;p++
)
cout << * p ;
return 0;
}


046:指针练习:输出Tesla

#include <iostream>
using namespace std;
void Print(const char * p1, const char * p2)
{
for(
;p1<p2;p1++
)
cout << * p1;
}
int main()
{
const char * s = "Tesla123";
Print(s,s+5);
cout << endl;
Print(s,s+3);
cout << endl;

return 0;
}


047:指针练习：ForEach

#include <iostream>
using namespace std;

void ForEach(void * a, int width, int num,
void (*f)(void *p)
)

{
for(int i = 0;i < num; ++i)
f((char*)a+width*i);
}

void PrintSquare(void * p)
{
int * q = (int*)p;
int n = *q;
cout << n * n << ",";
}
void PrintChar(void * p) {
char * q = (char*)p;
cout << *q << ",";
}
int main()
{
int a[5] = {1,2,3,4,5};
char s[] = "hello!";
ForEach(a,sizeof(int),5,PrintSquare);
cout << endl;
ForEach(s,sizeof(char),6,PrintChar);
return 0;
}


048:指针练习：Memcpy之一

#include <iostream>
using namespace std;
void Memcpy(char * src,char * dest,int n)
{
for (int i = 0; i < n; i++) {
*(dest + i) = *(src + i);
}
}
int Strlen(char * s)
{
int i;
for( i = 0; s[i]; ++i);
return i;
}
int main()
{
int a;
char s1[30];
char s2[30];
int t;
cin >> t;
for(int i = 0;i < t; ++i) {
cin >> a;
int b = 99999999;
Memcpy((char*)&a,(char *) &b,sizeof(int));
cout << b << endl;
}
for(int i = 0;i < t; ++i) {
cin >> s1;
Memcpy(s1,s2,Strlen(s1)+1);
cout << s2 << endl;
}
return 0;
}


049:指针练习：double

#include <iostream>
using namespace std;

void Double(int * p, int n)
{
for(int i = 0;i < n; ++i)
p[i] *= 2;
}

int main()
{
int a[3][4] = { { 1,2,3,4},{5,6,7,8},
{ 9,10,11,12 } };

Double(
a[1], 6
);
for(int i = 0;i < 3; ++i) {
for(int j = 0; j < 4; ++j)
cout << a[i][j] << ",";
cout << endl;
}

return 0;
}


050:指针练习：Memcpy之二

#include <iostream>
using namespace std;
void Memcpy( void * src, void * dest, int size)
{
char temp[100];
for (int i = 0; i < size; i++) {
temp[i] = *((char*)src + i);
}
for (int i = 0; i < size; i++) {
*((char *)dest+i) = temp[i];
}
}

void Print(int * p,int size)
{
for(int i = 0;i < size; ++i)
cout << p[i] << ",";
cout << endl;
}

int main()
{
int a[10];
int n;
cin >> n;
for(int i = 0;i < n; ++i)
cin >> a[i];
int b[10] = {0};
Memcpy(a,b,sizeof(a));
Print(b,n);

int c[10] = {1,2,3,4,5,6,7,8,9,10};
Memcpy(c,c+5,5*sizeof(int)); //将c的前一半拷贝到后一半
Print(c,10);

char s[10] = "123456789";
Memcpy(s+2,s+4,5); //将s[2]开始的5个字符拷贝到s[4]开始的地方
cout << s << endl;

char s1[10] = "123456789";
Memcpy(s1+5,s1+1,4); //将s1[5]开始的4个字符拷贝到s1[1]开始的地方
cout << s1 << endl;

return 0;
}


051:指针练习：MyMax

#include <iostream>
using namespace std;
char* MyMax(void* a, int s, int n, int (*f)(void* n1, void* n2)) {
char* zuida = (char*)a;
for (int i = 0; i < n; ++i) {
if ((f(zuida, (char*)a + s*i))<0) {
zuida = (char*)a + s*i;
}
}
return zuida;
}
int Compare1(void * n1,void * n2)
{
int * p1 = (int * )n1;
int * p2 = (int * )n2;
return ((*p1)%10) - ((*p2)%10);
}
int Compare2(void * n1,void * n2)
{
int * p1 = (int * )n1;
int * p2 = (int * )n2;
return *p1 - *p2;
}
#define eps 1e-6
int	Compare3(void * n1,void * n2)
{
float * p1 = (float * )n1;
float * p2 = (float * )n2;
if( * p1 - * p2 > eps)
return 1;
else if(* p2 - * p1 > eps)
return -1;
else
return 0;
}

int main()
{
int t;
int a[10];
float d[10];
cin >> t;
while(t--) {
int n;
cin >> n;
for(int i = 0;i < n; ++i)
cin >> a[i];
for(int i = 0;i < n; ++i)
cin >> d[i];
int * p = (int *) MyMax(a,sizeof(int),n,Compare1);
cout << * p << endl;
p = (int *) MyMax(a,sizeof(int),n,Compare2);
cout << * p << endl;
float * pd = (float * )MyMax(d,sizeof(float),n,Compare3);
cout << * pd << endl;
}
return 0;
}


052:指针练习：指向指针的指针

#include <iostream>
using namespace std;
int main()
{
int x,y,z;
x = 10;
y = 20;
z = 30;

int * a[3]  = { &x, &y,&z};
for(
int** p = a;
p < a + 3; ++p)
cout<< * (*p) << endl;
return 0;

}


053:指针练习：SwapMemory

#include <iostream>
using namespace std;
void SwapMemory(void * m1,void * m2, int size)
{

// 交换m1和m2，内存大小是size
char temp;
for (int i = 0; i < size; ++i) {
temp = *((char*)m1 + i);
*((char*)m1 + i) = *((char*)m2 + i);
*((char*)m2 + i) = temp;
}

}

void PrintIntArray(int * a,int n)
{
for(int i = 0;i < n; ++i)
cout << a[i] << ",";
cout << endl;
}

int main()
{
int a[5] = {1,2,3,4,5};
int b[5] = {10,20,30,40,50};
SwapMemory(a,b,5 * sizeof(int));
PrintIntArray(a,5);
PrintIntArray(b,5);
char s1[] = "12345";
char s2[] = "abcde";
SwapMemory(s1,s2,5);
cout << s1 << endl;
cout << s2 << endl;
return 0;
}


054:成绩排序

#include <iostream>
using namespace std;
struct student {
string name;
};
student s[20];
int main() {
int n;
cin >> n;
for(int i=0;i<n;++i){
// 将输入的n组数据存在一个数组中
cin >> s[i].name;
}
// 排序
for (int i = 0; i < n - 1; ++i) {
int tmpMax = i;
for (int j = i + 1; j < n; ++j) {
tmpMax = j;
if (s[j].name < s[tmpMax].name) {
tmpMax = j;
}
}
}
student tmp = s[i];
s[i] = s[tmpMax];
s[tmpMax] = tmp;

}
// 排好了，输出
for (int i = 0; i < n; ++i) {
cout << s[i].name << " " << s[i].grade << endl;
}

}


055:分数线划定

#include <iostream>
using namespace std;
struct xuanshou {
string xuhao;
};
xuanshou x[5005];
int main() {
int n, m;
cin >> n >> m;
// 输入选手的笔试分数
for (int i = 0; i < n; ++i) {
cin >> x[i].xuhao;
}

// 排序，选择排序
for (int i = 0; i < n - 1; ++i) {
int tmpMax = i;
for (int j = i + 1; j < n; ++j) {
tmpMax = j;
if (x[j].xuhao < x[tmpMax].xuhao)
tmpMax = j;
}

}
xuanshou tmp = x[i];
x[i] = x[tmpMax];
x[tmpMax] = tmp;
}
// 计算进入笔试的人数
int geshu = m * 1.5;
for (int i = geshu; i < n; ++i) {
geshu++;
}
// 输出
cout << x[geshu - 1].grade << " ";
cout << geshu << endl;
for (int i = 0; i < geshu; ++i) {
cout << x[i].xuhao << " ";
}

}


056:病人排队

#include <iostream>
using namespace std;
struct bingren {
string id;
int age;
int xuhao;
};
bingren x[5005];
int main() {
int n;
cin >> n;
// 输入病人id和年龄
for (int i = 0; i < n; ++i) {
cin >> x[i].id;
cin >> x[i].age;
x[i].xuhao = i;
}

// 排序，选择排序
for (int i = 0; i < n - 1; ++i) {
int tmpMin = i;
for (int j = i + 1; j < n; ++j) {
// 新病人是老年人
if(x[j].age>=60){
// 在位病人是老年人
if (x[tmpMin].age >= 60) {
// 新病人的年纪比较大
if (x[j].age > x[tmpMin].age) {
tmpMin = j;
}
// 年纪一样大
else if (x[j].age == x[tmpMin].age) {
// 新病人的排号靠前
if (x[j].xuhao < x[tmpMin].xuhao) {
tmpMin = j;
}
}
}
// 在位病人是年轻人
else {
tmpMin = j;
}
}
// 新病人是年轻人
else {
//在位病人也是年轻人
if (x[tmpMin].age < 60) {
// 新病人序号靠前
if (x[j].xuhao < x[tmpMin].xuhao) {
tmpMin = j;
}
}
}
}
bingren tmp = x[i];
x[i] = x[tmpMin];
x[tmpMin] = tmp;
}

// 输出
for (int i = 0; i < n; ++i) {
cout << x[i].id << endl;
}
return 0;
}


057:mysort

#include <iostream>
using namespace std;
struct A {
int nouse1;
int nouse2;
int n;
};

void mysort(void* a, int n, int w, int(*f)(const void* e1, const void* e2))
{
char* s = (char*)a;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
char* p1 = (char*)a + i * w;
char* p2 = (char*)a + j * w;
if (f(p1, p2) > 0) {
for (int k = 0; k < w; k++) {
char tem = p1[k];
p1[k] = p2[k];
p2[k] = tem;
}
}
}
}
}
int MyCompare1( const void * e1,const void * e2)
{
int * p1 = (int * ) e1;
int * p2 = (int * ) e2;
return * p1 - * p2;
}
int MyCompare2( const void * e1,const void * e2)
{
int * p1 = (int * ) e1;
int * p2 = (int * ) e2;
if( (* p1 %10) - (* p2 % 10))
return (* p1 %10) - (* p2 % 10);
else
return * p1 - * p2;
}
int MyCompare3( const void * e1,const void * e2)
{
A * p1 = (A*) e1;
A * p2 = (A*) e2;
return p1->n - p2->n;
}
int a[20];
A b[20];
int main ()
{
int n;
while(cin >> n) {
for(int i = 0;i < n; ++i) {
cin >> a[i];
b[i].n = a[i];
}
mysort(a,n,sizeof(int),MyCompare1);
for(int i = 0;i < n; ++i)
cout << a[i] << "," ;
cout << endl;
mysort(a,n,sizeof(int),MyCompare2);
for(int i = 0;i < n; ++i)
cout << a[i] << "," ;
cout << endl;
mysort(b,n,sizeof(A),MyCompare3);
for(int i = 0;i < n; ++i)
cout << b[i].n << "," ;
cout << endl;
}
return 0;
}


058:从字符串中取数

#include <iostream>
#include <iomanip>
using namespace std;
double GetDoubleFromString(char * str)
{

// 在此处补充你的代码
static char* p;
if (str)
p = str;
double num = 0;
while (*p && !(*p >= '0' && *p <= '9'))
++p;   // 跳过不是数字的字符
if (*p == 0)   // 如果跳完了，就代表结束了
return NULL;
// 把连续的数字字符变成浮点数的整数部分
while (*p >= '0' && *p <= '9') {
num = num * 10 + *p - '0';
++p;
}
// 遇到小数点
if (*p == '.') {
++p;
double i = 10;
// 把小数点后面的数字字符变成浮点数的小数部分
while (*p >= '0' && *p <= '9') {
num += (*p - '0') / i;
++p;
i *= 10;
}
}
return num;

}

int main()
{
char line[300];
while(cin.getline(line,280)) {
double n;
n = GetDoubleFromString(line);
while( n > 0) {
cout << fixed << setprecision(6) << n << endl;
n = GetDoubleFromString(NULL);
}
}
return 0;
}


059:sort简单题

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
int a[8] = {6,5,14,23,2,9,87,10 };
sort(
a+1, a+7, greater<int>()
);
for(int i = 0;i < 8; ++i)
cout << a[i] << "," ;
return 0;
}


060:还是sort简单题

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

struct Point{
int x;
int y;
};
// 在此处补充你的代码
struct Rule1
{
bool operator()(const int& a1, const int& a2) const {
if (a1 % 10 != a2 % 10) {
return a1 % 10 < a2 % 10;
}
else {
return a1 > a2;
}
}
};
struct Rule2
{
bool operator()(const Point& a1, const Point& a2) const {
int l1 = a1.x * a1.x + a1.y * a1.y;
int l2 = a2.x * a2.x + a2.y * a2.y;
if (l1 != l2) {
return l1 < l2;
}
else {
if (a1.x != a2.x)
return a1.x < a2.x;
else
return a1.y < a2.y;
}
}
};
int main()
{
int a[8] = {6,5,55,23,3,9,87,10 };
sort(a,a+8,Rule1());
for(int i = 0;i < 8; ++i)
cout << a[i] << "," ;
cout << endl;
Point ps[8] = {{1,0},{0,1},{0,-1},{-1,0},{1,-1},{1,1},{2,0},{-2,0} } ;
sort(ps,ps+8,Rule2());
for(int i = 0;i < 8; ++i)
cout << "(" << ps[i].x << "," << ps[i].y << ")";
return 0;
}


061:Set

#include <iostream>
#include <set>
#include <string>
#include <iterator>

using namespace std;

int main()
{
multiset<int> mset;
set<int> mm;
char commend[5];
int i, n, num, amount;
multiset<int>::iterator it;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> commend >> num;
switch (commend[1])
{
case 'd':
mset.insert(num);
mm.insert(num);
cout << mset.count(num) << endl;
break;
case 'e':
cout << mset.count(num) << endl;
mset.erase(num);
break;
case 's':
if (mm.find(num) == mm.end())
cout << "0 0" << endl;
else
{
cout << "1 ";
cout << mset.count(num) << endl;
}
break;
}
}
return 0;
}


062:热血格斗场

#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;
// 保证输入没有战斗力相同的，那么就可以用map保存
struct Ren {
int zhanli;
int id;
};
typedef map<int, int> MP;
int main() {
int n;
MP mp;
scanf("%d", &n);
// 把老板加进去
Ren ren;
ren.id = 1;
ren.zhanli = 1000000000;
mp.insert(make_pair(ren.zhanli, ren.id));
while (n--) {
scanf("%d%d", &ren.id, &ren.zhanli);
MP::iterator di = mp.lower_bound(ren.zhanli);
MP::iterator gao;
if (di != mp.begin()) {
// 新加入的人不是最弱的
if (di != mp.end()) {
// 新加入的人是中间的
gao = di;
di--;

if (ren.zhanli - di->first == gao->first - ren.zhanli) {
//上下差距一样
printf("%d %d\n", ren.id, di->second);
}
else if (ren.zhanli - di->first < gao->first - ren.zhanli) {
// 下面差距小
printf("%d %d\n", ren.id, di->second);
}
else {
// 上面差距小
printf("%d %d\n", ren.id, gao->second);
}
}
else {
// 新加入的人是最强的
// 和目前最强的人打
di--;
printf("%d %d\n", ren.id, di->second);
}

}
else {
// 新加入的人是最弱的
// 和目前最弱的人打
printf("%d %d\n", ren.id, di->second);
}
// 整完之后把这个人加进去
mp.insert(make_pair(ren.zhanli, ren.id));
}
return 0;
}


063:冷血格斗场

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main()
{
int n, value, id;
scanf("%d", &n);
map<int, int> member;
member[1000000000] = 1;
for (int i = 1; i <= n; i++) {
scanf("%d %d", &id, &value);
auto it = member.lower_bound(value);      // 返回最小的大于等于value的迭代器
if (it == member.end())
--it;
if (it == member.begin()) {
printf("%d %d\n", id, it->second);
}
else {
auto jt = it;
jt--;
if (value - jt->first < it->first - value)
printf("%d %d\n", id, jt->second);
else if (value - jt->first > it->first - value)
printf("%d %d\n", id, it->second);
else {
if (jt->second < it->second)
printf("%d %d\n", id, jt->second);
else
printf("%d %d\n", id, it->second);
}
}
it = member.find(value);
if (it == member.end() || it->second > id)
member[value] = id;
}

}

• 26
点赞
• 148
收藏
觉得还不错? 一键收藏
• 2
评论
01-29
05-03 404
04-11 541
05-31 348
01-02
08-16
07-16 1189
03-24 2188
03-17 1004

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。