HDUOJ 2000-2019(自用)

这篇文章展示了多个使用C++语言解决的基础数学问题,包括ASCII码排序、计算两点距离、球体体积计算、求绝对值、成绩等级转换等,涵盖了算法和数学应用的多个方面。
摘要由CSDN通过智能技术生成

2000 ASCII码排序

可直接使用swap函数

#include<iostream>
using namespace std;
int main() {
    char n[3];
    int i = 0;
    while (cin >> n) {
        if (n[0] > n[1]) swap(n[0], n[1]);
        if (n[1] > n[2]) swap(n[1], n[2]);
        if (n[0] > n[1]) swap(n[0], n[1]);
        cout << n[0] << " " << n[1] << " " << n[2] << endl;

    }

}

2001 计算两点距离

易错点:变量必须都为double型

#include<iostream>
#include<math.h>
using namespace std;
int main() {
	double x1, y1, x2, y2;
	double c;
	//也可以while(scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2)!=EOF)//输入
	while (cin>>x1>>y1>>x2>>y2) {
		double x = x1 - x2;
		double y = y1 - y2;
		c = sqrt(x*x + y * y);
		printf("%.2f\n", c);
	}
}

2002 球体体积

易错点:
1.要用浮点型做除法,否则整型相除还是整型
2.PI的值为3.1415927,最后一位不是6

#include<iostream>
#include<math.h>
#define PI 3.1415926
using namespace std;
int main() {
	double r,v;
	while (cin>>r) {
		v = 4.0/3.0*PI*r*r*r;
		printf("%.3f\n", v);
	}
}

2003 求绝对值

#include<iostream>
#include<math.h>
using namespace std;
int main() {
	double m;
	while (cin>>m) {
		if (m < 0) m = -m;
		printf("%.2f\n", m);
	}
}

2004 成绩转换

//oj又抽风了提交不上去,2004只是经过编译器编译

#include<iostream>
#include<math.h>
using namespace std;
int main() {
	int score;
	while (cin>>score) {
		if (score < 0 || score>100)
			printf("Score is error!");
		else if (score < 60)
			cout << 'E' << endl;
		else if (score < 70)
			cout << 'D' << endl;
		else if (score < 80)
			cout << 'C' << endl;
		else if (score < 90)
			cout << 'B' << endl;
		else if (score <= 100)
			cout << 'A' << endl;
	}
}

2005 第几天?

注意sum和flag重置

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main() {
	int y, m, d;
	int month[2][12] = {
		{31,28,31,30,31,30,31,31,30,31,30,31},
		{31,29,31,30,31,30,31,31,30,31,30,31}
	};
	
	while (scanf("%d/%d/%d", &y, &m, &d) != EOF) {
		//判断闰年 是4的倍数,400的倍数,但不是100的倍数
		int sum = 0, flag = 0;
		if (y % 4 == 0) {
			if (y % 100 == 0&&y%400!=0) 
				flag = 0;
			else flag = 1;
		}
		for (int i = 1; i < m; i++)
			sum += month[flag][i - 1];
		sum += d;
		printf("%d\n", sum);
	}
}

2006 求奇数的乘积

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main() {
	int n;
	int a[1000];
	while (cin >> n) {
		int m=1;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			if (a[i] % 2 != 0)
				m *= a[i];
		}
		cout << m<<endl;
	}
}

2007 平方和与立方和

注意:
1.要使用unsigned int型
2.考虑稳健性,若m<n,要交换m,n

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main() {
	unsigned int m,n,x,y,i;
	while (cin >> m>>n) {
		x=y=0;
		if (m > n)
			swap(m, n);
		for (i = m; i <=n; i++) {
			if (i % 2 == 0)
				x += i*i;
			else
				y += i * i*i;
		}
		cout << x<<' '<<y<<endl;
	}
}

2008 数值统计

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main() {
	int n;
	int a, b, c;
	double m;
	while (cin >> n) {
		a = b = c=0;
		if (n == 0) break;
		if (n < 100) {
			for(int i=0;i<n;i++){
				cin >> m;
				if (m < 0)	a++;
				else if (m == 0)	b++;
				else	c++;
			}
			cout << a << ' ' << b << ' ' << c << endl;
			continue;
		}
	}
}

2009 求数列的和

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main() {
	double n,sum;
	int m;
	while (cin >> n >> m) {
		sum = 0;
		for (int i = 0; i < m; i++) {
			sum += n;
			n = sqrt(n);
		}
		printf("%.2f\n", sum);
	}
	
}

2010 水仙花数

这题最麻烦的是处理输出空格的问题,不然会出现presentation error

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int v_sum(int t) {
	int sum = 0;
	while (t!= 0) {
		sum += (t % 10)*(t % 10)*(t % 10);
		t = t / 10;
	}
	return sum;
}
int main() {
	int m,n,flag=0;
	while (cin >> m >> n) {
		flag = 0;
		if (n < m) swap(m, n);
		for (int i = m; i <= n; i++) {
			if (v_sum(i) == i) {
				if (flag == 0)
					cout << i;
				else
					cout << " " << i;
				flag = 1;
			}	
		}
		if (flag == 1)
			cout << endl;
		else
			cout << "no" << endl;
	}
}

2011 多项式求和

主要问题是1除以double型,所以要另设一个double型变量x,把n赋给x

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

int main() {
	int m,n;
	double sum,x=1;
	cin >> m;
	for (int i = 0; i < m; i++) {
		sum = 0;
		cin >> n;
		x = n;
		while (n) {
			if (n % 2 == 0)
				sum -= 1 / x;
			else
				sum += 1 / x;
			n--;
			x--;
		}
		printf("%.2f\n", sum);
	}
}

2012 素数判定

flag记得重置!!!

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int su(int s) {
	for (int i = 2; i <= sqrt(s); i++) {
		if (s%i == 0)
			return 0;
	}
	return 1;
}
int main() {
	int x, y,s;
	int flag;
	while (cin >> x >> y) {
		flag = 1;
		if (x == 0 && y == 0)
			break;
		for (int n = x; n <= y; n++) {
			s = n * n + n + 41;
			if (su(s) == 0) flag = 0;
		}
		if (flag == 1)
			cout << "OK" << endl;
		else
			cout << "Sorry" << endl;
	}
}

2013 蟠桃记

sum重置

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

int main() {
	int n,sum;
	while (cin >> n) {
		sum = 1;
		while (n--&&n>=1) {
			sum = (sum + 1) * 2;
		}	
		cout << sum << endl;
	}
}

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

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

int main() {
    int n,a[100];
    int max, min;
    double sum,avg;
    while (cin >> n) {
        max = 0, min = 100,sum=0;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            if (a[i] >= max) max = a[i];
            if (a[i] <= min) min = a[i];
            sum += a[i];
        }
        sum = sum - max - min;
        avg = sum / (n - 2);
        printf("%.2f\n", avg);
    }
}

2015 偶数求和

与数学结合,注意数学思想的运用
从x开始,连续m个偶数的和为 x+m-1;所以只需要一个循环

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

int main() {
	int n, m;
	int sum,avg;
	int x;
	while (cin >> n >> m) {
		int flag = 0;
		x = 2;
		for (int i = 0; i < n/m; i++) {
			avg = x + m - 1;
			x += m * 2;
			if (flag == 0) {
				cout << avg;
				flag = 1;
			}
			else
				cout << " " << avg;
		}
		if (n%m != 0) {
			avg = x + n % m - 1;
			cout << " " << avg << endl;
		}
		else
			cout << endl;
	}
}

参考源码

#include <stdio.h>

int main(void)
{
    int i, n, m, b, c;

    while (scanf("%d%d", &n, &m) != EOF)
    {
        b = 2;
        c = 0;
        for (i = 0 ; i < n / m ; i++)
        {
            printf(c++ ? " %d" : "%d", b + m - 1);
            b += m * 2;
        }
        printf(n % m ? " %d\n" : "\n", b + n % m - 1);
    }

    return 0;
}

2016 数据的交换输出

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

int main() {
	int n;
	int a[100];
	while (cin >> n) {
		int i, min_i = 0;
		if (n == 0)	break;
		for (i = 0; i < n; i++) {
			cin >> a[i];
			if (a[i] < a[min_i])	min_i = i;
		}
		swap(a[0], a[min_i]);
		for (i = 0; i < n; i++) {
			if (i == 0)	cout << a[0];
			else	cout<<" "<<a[i];
		}
		cout << endl;
	}
}

2017 字符串统计

1.处理字符串的输入常使用getchar()和gets()函数
2.getchar()函数与scanf()、cin不同,会把空格和\n都读入字符流
3.所以在输入第一个数字后的回车需用一个getchar()接收,否则会直接被循环里的getchar接收从而无法进入循环
4.对数字的统计可以直接使用isdigit()函数,属于<stdio.h>库

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

int main() {
	int n,d;
	char c;
	cin >> n;
	getchar();
	while (n--) {
		d = 0;
		while ((c = getchar()) != '\n') {
			if (isdigit(c))
				d++;
		}
		cout << d << endl;
	}
}

2018 母牛的故事

在这里插入图片描述
类似的还有人或树,主要思想是分成多个年龄,总数即为各个年龄的数量相加。

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

int main() {
	int a, b, c, d, n, sum;
	a = 1;
	while (cin >> n&&n!=0) {
		a = 1; b = c = d = 0;
		for (int i = 1; i < n; i++) {
			a = b + a;
			b = c;
			c = d;
			d = a;
		}
		sum = a + b + c + d;
		cout << sum << endl;
	}
}

2019 数列有序!

插入排序,从后往前查找位置!

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

int main() {
	int n, x, i;
	int a[101];
	while (cin >> n >> x&&(n!=0&&x!=0)) {
		for (i = 0; i < n; i++) 
			cin >> a[i];
		for (i = n; i > 0 && a[i-1] > x; i--) {
			a[i] = a[i - 1];
		}
		a[i] = x;
		for (i = 0; i < n + 1; i++) {
			printf("%d%c", a[i], (n - i ? ' ' : '\n'));
		}
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值