hdu oj最基础50题(2000-2009题)
题目地址:http://acm.hdu.edu.cn/listproblem.php?vol=11
本博文旨在帮助部分跨考生从初试踏入复试,大家加油hhhhh
本内容编写过程中仅使用最基本的c语言知识,不使用c++库
文章目录
2000.ASCII码排序
#include<stdio.h>
#include<string.h>
// 交换
void swap(int &x, int &y){
int tmp = x;
x = y;
y = tmp;
}
// 数组分割为2部分
int patition(char *a, int low, int high){
char tmp = a[low]; //取当前区域最左边为基准元素
while (low < high) {
// 当队尾的元素大于等于基准数据时,向前挪动high指针
while (low < high && a[high] >= tmp) {
high--;
}
// 如果队尾元素小于tmp了,需要将其赋值给low
a[low] = a[high];
// 当队首元素小于等于tmp时,向前挪动low指针
while (low < high && a[low] <= tmp) {
low++;
}
// 当队首元素大于tmp时,需要将其赋值给high
a[high] = a[low];
}
// 跳出循环时low和high相等,此时的low或high就是tmp的正确索引位置
// 由原理部分可以很清楚的知道low位置的值并不是tmp,所以需要将tmp赋值给a[low]
a[low] = tmp;
return low; // 返回tmp的正确位置
}
// 快速排序
void quickSort(char *a, int left, int right){
if(left >= right) return;
int mid = patition(a, left, right);
quickSort(a, left, mid - 1);
quickSort(a, mid + 1, right);
}
int main(){
char s[100];
while(~scanf("%s", s)){
int len = strlen(s);
// sort(s, s+len);
// 本题sort禁用
int left = 0, right = len - 1;
quickSort(s, left, right);
for(int i=0;i<len;i++){
if(i != len - 1) printf("%c ", s[i]);
else printf("%c", s[i]);
}
printf("\n");
}
return 0;
}
2001.计算两点间的距离
#include<stdio.h>
#include<math.h>
int main(){
double x1,y1,x2,y2;
while(~scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2)){
printf("%.2lf\n",sqrt(pow((y2 - y1),2) + pow((x2 - x1),2)));
}
return 0;
}
2002.计算球体积
#include<stdio.h>
#include<math.h>
#define PI 3.1415927
int main(){
double r;
// 这个方法也可以求出PI
// double PI = atan(1.0) * 4;
while(~scanf("%lf",&r)){
printf("%.3lf\n",4 * PI * pow(r,3) / 3);
}
return 0;
}
2003.求绝对值
#include<stdio.h>
#include<math.h>
int main(){
double a;
while(~scanf("%lf", &a)){
if(a < 0) a = -a;
printf("%.2lf\n", a);
}
return 0;
}
2004.成绩转换
#include<stdio.h>
int main(){
int a;
while(~scanf("%d", &a)){
switch(a / 10){
case 6:
printf("D\n");
break;
case 7:
printf("C\n");
break;
case 8:
printf("B\n");
break;
case 9:
printf("A\n");
break;
default:
if(a == 100) printf("A\n");
else if(a < 60 && a>=0) printf("E\n");
else printf("Score is error!\n");
break;
}
}
return 0;
}
2005.第几天?
#include<stdio.h>
int main(){
int year, month, day, ans;
int month_days[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
while(~scanf("%d/%d/%d",&year, &month, &day)){
ans =0;
// 首先判断是否是闰年
if((year % 4 == 0 && year % 100 != 0) || (year%400==0)){
month_days[2]=29;
}
else{
month_days[2]=28;
}
for(int i=1;i<month;i++){
ans = ans + month_days[i];
}
ans = ans + day;
printf("%d\n",ans);
}
return 0;
}
}
2006.求奇数的乘积
#include<stdio.h>
int main(){
int n, x, ans;
while(~scanf("%d",&n)){
ans = 1;
for(int i=0;i<n;i++){
scanf("%d", &x);
if(x % 2 == 1){
ans = ans * x;
}
}
printf("%d\n", ans);
}
return 0;
}
2007.平方和与立方和
#include<stdio.h>
#include<math.h>
int main(){
int l, r, ans1, ans2;
while(~scanf("%d %d",&l,&r)){
ans1 = 0;
ans2 = 0;
if(l > r){
int tmp = l;
l = r;
r = tmp;
}
for(int i=l;i<=r;i++){
if(i % 2 == 1){
ans2 = ans2 + pow(i,3);
}else{
ans1 = ans1 + pow(i,2);
}
}
printf("%d %d\n", ans1, ans2);
}
return 0;
}
2008.数值统计
#include<stdio.h>
#include<math.h>
int main(){
int n, ans1, ans2, ans3;
double x;
while(~scanf("%d",&n),n){
ans1 = ans2 = ans3 = 0;
while(n--){
scanf("%lf", &x);
if(x > 0){
ans3++;
}else if(x == 0){
ans2++;
}else{
ans1++;
}
}
printf("%d %d %d\n", ans1, ans2, ans3);
}
return 0;
}
2009.求数列的和
#include<stdio.h>
#include<math.h>
int main(){
int m;
double n, ans;
while(~scanf("%lf %d",&n,&m)){
ans = n;
m--;
while(m--){
n = sqrt(n);
ans = ans + n;
}
printf("%.2lf\n", ans);
}
return 0;
}