HDU100题简要题解(2010~2019)

HDU2010 水仙花数

题目链接

Problem Description
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+3^3。
现在要求输出所有在m和n范围内的水仙花数。

Input
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。

Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。

Sample Input
100 120
300 380

Sample Output
no
370 371

水仙花数!很经典的入门题

把区间内的每个数的每一位的立方相加,与其本身相等即为水仙花数

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

int n, m;

int main() {
  while (scanf("%d%d", &m, &n) != EOF) {
    int flag = 0;
    for (int i = m; i <= n; i++) {
      int a = i % 10;
      int b = (i / 10) % 10;
      int c = i / 100;
      if (pow(a, 3) + pow(b, 3) + pow(c, 3) == i) {
        if (flag == 0) {
  	  printf("%d", i);
  	  flag = 1;
  	}
  	else printf(" %d", i);
      }
    }
    if (flag == 0) cout << "no" << endl;
    else cout << endl;
  }
  return 0;
} 

HDU2011 多项式求和

题目链接

Problem Description
多项式的描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...
现在请你求出该多项式的前n项的和。

Input
输入数据由2行组成,首先是一个正整数m(m<100),表示测试实例的个数,第二行包含m个正整数,对于每一个整数(不妨设为n,n<1000),求该多项式的前n项的和。

Output
对于每个测试实例n,要求输出多项式前n项的和。每个测试实例的输出占一行,结果保留2位小数。

Sample Input
2
1 2

Sample Output
1.00
0.50

多项式求和,奇数项就加,偶数项就减

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

int n, m;
double sum;

int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &m);
      sum = 0;
      for (int j = 1; j <= m; j++) {
  	 if (j % 2 == 1) sum += 1.0 / j;
  	 else sum -= 1.0 / j;
      }
      printf("%.2lf\n", sum);
    }
  }
  return 0;
}

HDU2012 素数判定

题目链接

Problem Description
对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。

Input
输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。

Output
对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。

Sample Input
0 1
0 0

Sample Output
OK

把区间内的数都带进表达式,看看表达式的值是否为素数,如果不是,就直接输出“Sorry”就好了,后面的没必要继续搜

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

int x, y;

bool check(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}

int main() {
  while (scanf("%d%d", &x, &y) != EOF) {
    int flag = 0;
    if (x == 0 && y == 0) break;
    for (int i = x; i <= y; i++) {
      int sum = pow(i, 2) + i + 41;
      if (!check(sum)) {
        cout << "Sorry" << endl;
  	 flag = 1;
  	 break;
      }
    }
    if (flag == 0) cout << "OK" << endl; 
  }
  return 0;
}

HDU2013 蟠桃记

题目链接

Problem Description
喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!
什么问题?他研究的问题是蟠桃一共有多少个!
不过,到最后,他还是没能解决这个难题,呵呵-
当时的情况是这样的:
第一天悟空吃掉桃子总数一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第n天准备吃的时候只剩下一个桃子。聪明的你,请帮悟空算一下,他第一天开始吃的时候桃子一共有多少个呢?

Input
输入数据有多组,每组占一行,包含一个正整数n(1<n<30),表示只剩下一个桃子的时候是在第n天发生的。

Output
对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。

Sample Input
2
4

Sample Output
4
22

从后倒着往前算,因为每天吃掉前一天剩下的桃子数的一半多一个,所以可以得到:

最后一天1个桃子,倒数第二天3个桃子,倒数第三天8个桃子......倒数第n天的桃子数 = 2 * (倒数第n-1天的桃子数 + 1)

从而得到第一天的桃子数

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

int n, sum = 1;

int main() {
  while (scanf("%d", &n) != EOF) {
    n--;
    sum = 1;
    while (n--) sum = 2 * (sum + 1);
      printf("%d\n", sum);
  }
  return 0;
}

HDU2014 青年歌手大奖赛_评委会打分

题目链接

Problem Description
青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。

Input
输入数据有多组,每组占一行,每行的第一个数是n(2<n<=100),表示评委的人数,然后是n个评委的打分。

Output
对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行。

Sample Input
3 99 98 97
4 100 99 98 97

Sample Output
98.00
98.50

按照题目要求做就可以

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

int n;
double a[101], sum, maxx, minn;

int main() {
  while (scanf("%d", &n) != EOF) {
    sum = 0;
    maxx = 0;
    minn = 1e7 + 1;
    for (int i = 1; i <= n; i++) {
      scanf("%lf", &a[i]);
      maxx = max(maxx, a[i]);
      minn = min(minn, a[i]);
      sum += a[i];
    }
    double ans = (sum - maxx - minn) / (n - 2);
    printf("%.2lf\n", ans);
  }
  return 0;
}

HDU2015 偶数求和

题目链接

Problem Description
有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。

Input
输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。

Output
对于每组输入数据,输出一个平均值序列,每组输出占一行。

Sample Input
3 2
4 2

Sample Output
3 6
3 7

就是有一个长度为n的每一项都为偶数的数列,然后让你每m个数求一次平均值(不足m个时就以其实际数量求),最后输出平均值的序列

用一个cnt记录一下区间内数的个数,用sum记录区间内的和,cnt每到m就输出一次平均值,然后两者再从0开始记录

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

int n, m;

int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    int sum = 0;
    int cnt = 0;
    for (int i = 2; i <= 2 * n; i += 2) {
      cnt++;
      sum += i;
      if (cnt == m && i != 2 * n) {
  	 cnt = 0;
  	 printf("%d ", sum / m);
  	 sum = 0;
      }
    }
    if (sum != 0) printf("%d\n", sum /cnt);
  }
  return 0;
}

HDU2016 数据的交换输出

题目链接

Problem Description
输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。

Input
输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。

Output
对于每组输入数据,输出交换后的数列,每组输出占一行。

Sample Input
4 2 1 3 4
5 5 4 3 2 1
0

Sample Output
1 2 3 4
1 4 3 2 5

找到最小的和第一个交换一下位置然后输出就好了

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

int n, a[101];

int main() {
  while (scanf("%d", &n) != EOF) {
    int cnt = 1;
    int minn = 1e7 + 1;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      if (a[i] < minn) {
  	 minn = a[i];
  	 cnt = i;
      }
    }
    int num = a[1];
    a[1] = a[cnt];
    a[cnt] = num;
    for (int i = 1; i <= n; i++) {
      if (i != n) printf("%d ", a[i]);
      else printf("%d\n", a[i]);
    }
  }
  return 0;
}

HDU2017 字符串统计

题目链接

Problem Description
对于给定的一个字符串,统计其中数字字符出现的次数。

Input
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Output
对于每个测试实例,输出该串中数值的个数,每个输出占一行。

Sample Input
2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

Sample Output
6
9

统计一个制胡窜里有多少个数字,这里用到了isdigit()去判断数字

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

int n;
char c[10001];

int main() {
  while (scanf("%d", &n) != EOF) {
    while (n--) {
      int cnt = 0;
      scanf("%s", c);
      for (int i = 0; i < strlen(c); i++) 
  	 if (isdigit(c[i])) cnt++;
      printf("%d\n", cnt);
    }
  }
  return 0;
}

HDU2018 母牛的故事

题目链接

Problem Description
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。

Output
对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。

Sample Input
2
4
5
0

Sample Output
2
4
6

小学奥数,当时就烦这类题目

这里用了递推,多手撸几个数据就会发现规律:第n年的母牛数 = 第n-1年的母牛数 + 第n-3年的母牛数

即a[i] = a[i - 1] + a[i - 3];

理解也很好理解,因为每头小牛从第四个年头开始也会生牛,所以第n个年头就是第n-1个年头的牛数 + 第n年新生的(也就是第n-3年的母牛数)

注意要初始化a[1] = 1; a[2] = 2; a[3] = 3; a[4] = 4

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

int n, a[56];

int main() {
  while (scanf("%d", &n) != EOF) {
    a[1] = 1;
    a[2] = 2;
    a[3] = 3;
    a[4] = 4;
    if (n == 0) break;
    for (int i = 5; i <= n; i++) {
      a[i] = a[i - 1] + a[i - 3];
    }
    printf("%d\n", a[n]);
  }
  return 0;
}

HDU2019 数列有序!

题目链接

Problem Description
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。

Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。

Output
对于每个测试实例,输出插入新的元素后的数列。

Sample Input
3 3
1 2 4
0 0

Sample Output
1 2 3 4

先把要加的数放在最后,然后sort一下再输出就好了

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

int n, x, a[101];

int main() {
  while (scanf("%d", &n) != EOF) {
    scanf("%d", &x);
    if (n == 0 && x == 0) break;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
      a[n + 1] = x;
      sort(a + 1, a + 2 + n);
      for (int i = 1; i <= n + 1; i++) {
  	if (i != n + 1) printf("%d ", a[i]);
  	else printf("%d\n", a[i]);
      }
  }
  return 0;
}
posted @ 2020-11-19 13:25  Moominn  阅读(492)  评论(0编辑  收藏  举报