1. 输入10个整数,计算其最大值和最小值,并交换它们所在数组中的位置
#include"stdio.h"
#define SIZE 10
void swap(int* a, int* b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void input(int num[]){
int i=0;
for(;i<SIZE;i++)
scanf("%d",num+i);
}
void max_min(int num[]){
int i,max,min,maxpos,minpos;
i = maxpos = minpos = 0;
max = min = num[0];
for(i=1;i<SIZE;i++){
if(num[i] > max){
max = num[i];
maxpos = i;
}
else if(num[i] < min){
min = num[i];
minpos = i;
}
}
swap(num+maxpos,num+minpos);
}
int main(){
int i = 0;
int num[SIZE];
input(num);
max_min(num);
printf("交换后: \n");
for(;i<SIZE;i++)
printf("%4d",num[i]);
return 0;
}
2. Fibonacci数列
#include"stdio.h"
int fib(int n){
if(n==1||n==2)
return 1;
else return fib(n-1)+fib(n-2);
}
int _fib(int n){
int num[n],i;
num[0] = num[1] = 1;
for(i=2;i<n;i++)
num[i] = num[i-1] + num[i-2];
return num[n-1];
}
int main(){
int i;
for(i=1;i<10;i++)
printf("%d! = %4d\t\t%d! = %4d\n",i,fib(i),i,_fib(i));
return 0;
}
3. 输入年月日,计算并输出它是这一年的哪一天
#include"stdio.h"
int isRun(int year){
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
return 0;
}
int Day(int arr[], int month, int day){
int i = 0,days = 0;
for(;i<month-1;i++)
days += arr[i];
days += day;
return days;
}
int YearToDay(int year, int month, int day){
int arr1[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int arr2[] = {31,29,31,30,31,30,31,31,30,31,30,31};
if(isRun(year))
return Day(arr2,month,day);
else return Day(arr1,month,day);
}
int main(){
int year,month,day;
printf("输入年月日: ");
scanf("%d %d %d",&year,&month,&day);
printf("第%d天\n",YearToDay(year,month,day));
return 0;
}
4. 输入某年的第几天,计算并输出它是这一年的第几月第几日
#include"stdio.h"
int isRun(int year){
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
return 0;
}
void Date(int arr[], int *month, int *day){
int i = 0;
while(*day > arr[i]){
*day -= arr[i];
*month += 1;
i++;
}
}
void YearToDay(int year, int* month, int* day){
int arr1[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int arr2[] = {31,29,31,30,31,30,31,31,30,31,30,31};
if(isRun(year))
Date(arr2,month,day);
else Date(arr1,month,day);
}
int main(){
int year,month,days;
month = 1;
printf("某年第几日: ");
scanf("%d %d",&year,&days);
YearToDay(year,&month,&days);
printf("%d月 %d日\n",month,days);
return 0;
}
5. 输入10个数,计算平均数,中位数,众数
#include"stdio.h"
#define MAXSIZE 100
#define N 40
void input(int num[]){
int i=0;
for(;i<N;i++)
scanf("%d",num+i);
}
int average(int num[]){
int sum = 0,i = 0;
while(i<N){
sum += num[i];
i++;
}
return sum/N;
}
int most(int num[]){
int count[MAXSIZE] = { 0 };
int i = 0,maxpos = 0,max = 0;
while(i<N){
count[num[i]]++;
i++;
}
i=0;
while(i<N){
if(count[num[i]] > max){
maxpos = i;
max = count[num[i]];
}
i++;
}
return num[maxpos];
}
void sort(int num[]){
int i,j;
for(i=0;i<N;i++){
for(j=i;j<N;j++){
if(num[i] > num[j]){
int temp;
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
}
int middle(int num[]){
sort(num);
if(N%2==0)
return (num[N/2] + num[N/2-1]) / 2;
else return num[N/2];
}
int main(){
int num[N];
input(num);
printf("平均数: %2d\t众数: %2d\t中位数:%2d\n",average(num),most(num),middle(num));
return 0;
}
|