hdu oj最基础50题(2010-2019题)
题目地址:http://acm.hdu.edu.cn/listproblem.php?vol=11
本博文旨在帮助部分跨考生从初试踏入复试,大家加油hhhhh
本内容编写过程中仅使用最基本的c语言知识,不使用c++库
文章目录
2010.水仙花数
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
int l, r, x, cnt;
int a, b, c;
int ans[100];
memset(ans, 0, sizeof(ans));
while(~scanf("%d %d",&l, &r)){
cnt = 0;
for(int i=l;i<=r;i++){
a = i / 100;
b = (i / 10) % 10;
c = i % 10;
if(pow(a,3) + pow(b,3) + pow(c,3) == i){
ans[cnt++] = i;
}
}
if(cnt == 0) printf("no\n");
else{
for(int i=0;i<cnt-1;i++) printf("%d ", ans[i]);
printf("%d\n", ans[cnt-1]);
}
}
return 0;
}
2011.多项式求和
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
int n;
scanf("%d", &n);
int x;
double ans;
for(int i=0;i<n;i++){
scanf("%d", &x);
ans = 0;
for(int j=1;j<=x;j++){
if(j % 2 == 1){
ans = ans + 1.0 / (double)j;
}else{
ans = ans - 1.0 / (double)j;
}
}
printf("%.2lf\n", ans);
}
return 0;
}
2012.素数判定
#include<stdio.h>
#include<math.h>
#include<string.h>
// 埃拉托斯特尼筛法
const int N = 100005;
bool prime[N];
void init(){
for(int i=2;i<N;i++) prime[i]=true;//先全部初始化为素数
for(int i=2;i*i<N;i++){
if(prime[i]){//如果i是质数
for(int j=i*i;j<N;j+=i){//从i的两倍开始的所有的倍数(i*i也行)
prime[j] = false;
}
}
}
}
int main(){
int l, r, cnt, flag;
// 初始化素数表
init();
while(~scanf("%d %d", &l, &r), l || r){
flag = 0;
for(int i=l;i<=r;i++){
cnt = i * i + i + 41;
if(!prime[cnt]){
flag = 1;
break;
}
}
if(flag) printf("Sorry\n");
else printf("OK\n");
}
return 0;
}
2013.蟠桃记
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
int n, ans;
while(~scanf("%d", &n)){
ans = 1;
for(int i=1;i<n;i++){
ans = (ans + 1) * 2;
}
printf("%d\n", ans);
}
return 0;
}
2014.青年歌手大奖赛_评委会打分
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
int n;
double maxx, minn, x, ans;
while(~scanf("%d", &n)){
maxx = -1;
minn = 101;
ans = 0;
for(int i=1;i<=n;i++){
scanf("%lf", &x);
if(maxx < x) maxx = x;
if(minn > x) minn = x;
ans = ans + x;
}
ans = (ans - maxx - minn) / (n - 2);
printf("%.2lf\n", ans);
}
return 0;
}
2015.偶数求和
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
int n, m;
int arr[105], ind;
while(~scanf("%d %d", &n, &m)){
memset(arr, 0, sizeof(arr));
ind = 0;
int surplus = n % m;
int st = 2, k = 0, cnt = 0;
for(int i=1;i<=n-surplus;i++){
cnt = cnt + st;
st += 2;
k++;
if(k == m){
arr[ind++] = cnt / m;
cnt = 0;
k = 0;
}
}
for(int i=n-surplus+1;i<=n;i++){
cnt = cnt + st;
st += 2;
}
if(surplus) arr[ind++] = cnt / surplus;
for(int i=0;i<ind;i++){
if(i == ind - 1) printf("%d\n", arr[i]);
else printf("%d ", arr[i]);
}
}
return 0;
}
2016.数据的交换输出
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
int n, pos, arr[105];
while(~scanf("%d", &n), n){
memset(arr, 0 ,sizeof(arr));
pos = 0;
for(int i=0;i<n;i++){
scanf("%d", &arr[i]);
if(arr[i] < arr[pos]) pos = i;
}
int tmp = arr[0];
arr[0] = arr[pos];
arr[pos] = tmp;
for(int i=0;i<n;i++){
if(i == n - 1) printf("%d\n", arr[i]);
else printf("%d ", arr[i]);
}
}
return 0;
}
2017.字符串统计
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
int n, cnt;
scanf("%d", &n);
char s[105];
while(n--){
scanf("%s", s);
int len = strlen(s);
cnt = 0;
for(int i=0;i<len;i++){
if(s[i] >= '0' && s[i] <= '9') cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
2018.母牛的故事
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
int n;
int nums[55];
memset(nums, 0, sizeof(nums));
for(int i=1;i<=4;i++){
nums[i] = i;
}
for(int i=5;i<=55;i++){
nums[i] = nums[i - 1] + nums[i - 3];
}
while(~scanf("%d", &n), n){
printf("%d\n", nums[n]);
}
return 0;
}
2019.数列有序!
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
int n, m;
int arr[105], k, flag, x;
while(~scanf("%d %d", &n, &m), n || m){
k = flag = 0;
for(int i=0;i<n;i++){
scanf("%d", &x);
if(x > m && !flag){
flag = 1;
arr[k++] = m;
}
arr[k++] = x;
}
for(int i=0;i<=n;i++){
if(i == n) printf("%d\n", arr[i]);
else printf("%d ", arr[i]);
}
}
return 0;
}