1.
#include
#include
int main()
{ int max_multiple(int,int);
int min_divisor(int,int);
int maxResult,minResult,a,b;
printf("请输入两个整数\n");
scanf("%d %d",&a,&b);
maxResult=max_multiple(a,b);
minResult=min_divisor(a,b);
printf("最大公因数为: %d\n",maxResult);
printf("最小公倍数为: %d\n",minResult);
return 0;
}
int max_multiple(int a,int b)
{
if(a>=b)
{
int i;
for(i=b;i>=1;i--)
{
if(a%i==0&&b%i==0){
return i;
}
}
}
else{
int j;
for(j=a;j>=1;j--)
{
if(a%j==0&&b%j==0){
return j;
}
}
}
}
int min_divisor(int a,int b)
{
if(a>=b){
int i;
for(i=b;i<=a*b;i++){
if(i%a==0&&i%b==0){
return i;
}
}
}
else{
int i;
for(i=a;i<=a*b;i++){
if(i%a==0&&i%b==0){
return i;
}
}
}
}
2.
#include
#include
#include
int main()
{
double delta_greater(double a,double b,double c);
double delta_equal(double a,double b,double c);
double delta_less(double a,double b,double c);
double a,b,c;
printf("请输入a,b,c的值\n");
scanf("%lf %lf %lf",&a,&b,&c);
if((b*b-4*a*c)>0){
delta_greater(a,b,c);
}
else if((b*b-4*a*c)==0){
delta_equal(a,b,c);
}
else if((b*b-4*a*c)<0)
{
delta_less(a,b,c);
}
return 0;
}
double delta_greater(double a,double b,double c)
{
double x1,x2;
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("此时有两个实根,分别为:x1=%f,x1=%f",x1,x2);
return 0;
}
double delta_equal(double a,double b,double c)
{
double x;
x=-b/(2*a);
printf("此时有两个相等的实根:x1=x2=%f",x);
return 0;
}
double delta_less(double a,double b,double c)
{
double p,q;
p=-b/(2*a);
q=sqrt(-(b*b-4*a*c))/(2*a);
printf("此时有两个共轭复根:x1=%f+%fi,x2=%f-%fi",p,q,p,q);
return 0;
}
3.
#include
#include
#include
int main()
{
int judge_prime(int);
int x,result;
printf("请输入一个正整数\n");
scanf("%d",&x);
if(judge_prime(x)==1)
{
printf("这个数不是素数");
}
else
{
printf("这个数是素数");
}
return 0;
}
int judge_prime(int x)
{
int i;
for(i=2;i<=sqrt(x);i++){
if(x%i==0){
return 1;
}
}
return 0;
}
4.
#include
#include
#define N 3
int main()
{
int trans_array(int array[][3]);
int i,j,array[3][3];
printf("请输入一个二维数组:\n");
for(i=0;i<N;i++){
for(j=0;j<N;j++){
scanf("%d",&array[i][j]);
}
}
printf("当前数组为:\n");
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf(" %d ",array[i][j]);
}
printf("\n");
}
trans_array(array);
return 0;
}
int trans_array(int a[][3])
{
int i,j,temp;
for(i=0;i<N;i++){
for(j=0;j<i;j++)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
}
printf("转置后的数组为:\n");
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf(" %d ",a[i][j]);
}
printf("\n");
}
return 0;
}
5.
#include
#include
#include
int main()
{
void reverse_str(char a[]);
printf("请输入一个字符串\n");
char a[10];
gets(a);
reverse_str(a);
printf("反序存放结果为:\n");
puts(a);
return 0;
}
void reverse_str(char a[])
{
int i,j;
j=strlen(a);
char temp;
for(i=0;i<j/2;i++){
temp=a[i];
a[i]=a[j-i-1];
a[j-i-1]=temp;
}
return a;
}
6.
#include
#include
#include
int main()
{
void catenate_str(char a[],char b[]);
char a[50],b[20];
printf("请输入第一个字符串\n");
gets(a);
printf("请输入第二个字符串\n");
gets(b);
printf("连接结果为:\n");
catenate_str(a,b);
puts(a);
return 0;
}
void catenate_str(char a[],char b[])
{
strcat(a,b);
}
7.
#include
#include
#include
int main()
{
void copy_str(char a[],char b[]);
char a[50],b[50];
printf("请输入一个字符串\n");
gets(a);
printf("其中的元音字母有:\n");
copy_str(a,b);
puts(b);
return 0;
}
void copy_str(char a[],char b[])
{
int i,j;
for(i=0,j=0;i<strlen(a);i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')
{
b[j]=a[i];
j++;
}
}
b[j]='\0';
}
8.
#include
#include
#include
int main()
{
void print_char(char a[]);
char a[20];
printf("请输入一个四位数字\n");
gets(a);
printf("结果为\n");
printf_char(a);
puts(a);
return 0;
}
void printf_char(char a[])
{
int i;
for(i=strlen(a);i>0;i--)
{
a[2*i]=a[i];
a[2*i-1]=' ';
}
}
9.
#include
#include
#include
int main()
{
void count_each(char [],int []);
char ch[100];
int count[4];
int i;
for(i=0;i<4;i++)
{
count[i]=0;
}
printf("请输入一个字符串\n");
gets(ch);
count_each(ch,count);
printf("有%d个数字\n",count[0]);
printf("有%d个字母\n",count[1]);
printf("有%d个空格\n",count[2]);
printf("有%d个其他字符\n",count[3]);
return 0;
}
void count_each(char ch[],int count[])
{
int i;
for(i=0;i<strlen(ch);i++){
if(ch[i]>='0'&&ch[i]<='9')
{
count[0]++;
}
else if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))
{
count[1]++;
}
else if(ch[i]==32)
{
count[2]++;
}
else
count[3]++;
}
}
10.
#include
#include
int main()
{
void find_longest(char []);
char ch[1000];
printf("请输入一行字符\n");
gets(ch);
find_longest(ch);
return 0;
}
void find_longest(char ch[])
{
int i,count,start,end,max,max_start,max_end,last;
start=end=max=count=last=max_start=max_end=0;
for(i=0;i<strlen(ch);i++)
{
if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))
{
if(last==0){
start=end=i;
count=1;
last=1;
}
else
{
count++;
end++;
}
if(i==(strlen(ch)-1))
{
if(count>max)
{
max_start=start;
max_end=end;
max=count;
}
}
}
else
{
if(count>max)
{
max_start=start;
max_end=end;
max=count;
}
start=end=count=0;
last=0;
}
}
int j=max_start;
for(;j<=max_end;j++)
printf("%c",ch[j]);
}
11.
#include
#include
int main()
{
void bubbleSort(char []);
char ch[100];
printf("请输入一组字符\n");
gets(ch);
printf("排序结果为:\n");
bubbleSort(ch);
puts(ch);
return 0;
}
void bubbleSort(char a[])
{
char temp;
int i,j;
for(i=0;i<strlen(a)-1;i++)
{
for(j=0;j<strlen(a)-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
12.
#include
#include
#include
int main()
{
double x1,x2,fx, fdx;
x1 = 1.5;
do{
x2 = x1;
fx = 2*pow(x1,3)-4*pow(x1,2)+3*x1-6;
fdx = 6*pow(x1,2)-8*x1+3;
x1 = x2 -fx/fdx;
}while(fabs(x2-x1)>=1e-5);
printf("¸ùΪ%f", x2);
return 0;
}
13.
#include
#include
int main()
{
double lrd_polynomial(int,int);
int n,x;
double fx;
printf("请输入多项式的阶数和x的值:\n");
scanf("%d %d",&n,&x);
fx=lrd_polynomial(n,x);
printf("计算结果为:%f\n",fx);
return 0;
}
double lrd_polynomial(int n,int x)
{
double fx;
if(n==0)
{
fx=0;
}
else if(n==1)
{
fx=x;
}
else
{
fx=((2*n-1)*x-lrd_polynomial(n-1,x)-(n-1)*lrd_polynomial(n-2,x))/n;
}
return fx;
}
14.
#include
#include
int main()
{
void compute_fun(double [][5]);
printf("请输入每个学生的成绩\n");
double a[10][5];
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<5;j++)
{
scanf("%lf",&a[i][j]);
}
}
compute_fun(a);
return 0;
}
void compute_fun(double a[][5])
{
void get_stu_ave(double a[][5]);
void get_sub_ave(double a[][5]);
void get_high_score(double a[][5]);
void get_delta(double a[][5]);
get_stu_ave(a);
get_sub_ave(a);
get_high_score(a);
get_delta(a);
}
void get_stu_ave(double a[][5])
{
int i,j;
double stu_ave=0;
printf("每个学生的平均分依次为:\n");
for(i=0;i<10;i++)
{
for(j=0;j<5;j++)
{
stu_ave+=a[i][j];
}
printf("%.2f ",stu_ave/5);
stu_ave=0;
}
printf("\n");
}
void get_sub_ave(double a[][5])
{
double sub_ave=0;
int i,j;
printf("每个科目的平均分依次为:\n");
for(i=0;i<5;i++)
{
for(j=0;j<10;j++)
{
sub_ave+=a[j][i];
}
printf("%.2f ",sub_ave/10);
sub_ave=0;
}
printf("\n");
}
void get_high_score(double a[][5])
{
double high_score=a[0][0];
int i,j,highi,highj;
for(i=0;i<10;i++)
{
for(j=0;j<5;j++)
{
if(a[i][j]>high_score)
{
high_score=a[i][j];
highi=i;
highj=j;
}
}
}
printf("最高的分数为%.2f,是第%d个学生的第%d们课程 \n",high_score,highi+1,highj+1);
}
void get_delta(double a[][5])
{
double delta1,sub_ave;
delta1=sub_ave=0;
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<5;j++)
{
sub_ave+=a[i][j];
}
sub_ave/=10;
delta1+= (sub_ave*sub_ave/10-(sub_ave/10)*(sub_ave/10));
}
printf("方差为%f",delta1);
}
15.
#include
#include
#include
int main()
{
void adjust_queue(char [][50],int []);
void find_worker(char [][50],int [],int);
char name[10][50];
int number[10],findItem;
printf("请输入10个职工的姓名和职工号\n");
int i;
for(i=0;i<10;i++){
gets(name[i]);
scanf("%d",&number[i]);
getchar();
}
printf("排序前的结果为:\n");
for(i=0;i<10;i++)
{
printf("编号%d,姓名%s\n",number[i],name[i]);
}
adjust_queue(name,number);
printf("请输入您要查找的职工号:\n");
scanf("%d",&findItem);
find_worker(name,number,findItem);
return 0;
}
void adjust_queue(char a[10][50],int b[10])
{
printf("排序后的结果为:\n");
char temp1[50];
int i,j,temp;
for(i=0;i<10-1;i++)
{
for(j=0;j<10-i-1;j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
strcpy(temp1,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],temp1);
}
}
}
for(i=0;i<10;i++)
{
printf("编号%d,姓名%s\n",b[i],a[i]);
}
}
void find_worker(char a[10][50],int b[10],int findItem)
{
int aim,low,high,mid;
low=0;high=10;
while(low<=high)
{
mid=(low+high)/2;
if(b[mid]==findItem)
{
aim=mid;
break;
}
else if(b[mid]>findItem)
high=mid-1;
else
low=mid+1;
}
printf("此职工是%s",a[aim]);
}
16.
#include
#include
#include
int main()
{
double system_transfer(char []);
char x[50];
double result;
printf("请输入一个十六进制数\n");
gets(x);
result=system_transfer(x);
printf("转换结果为\n%.0f",result);
return 0;
}
double system_transfer(char x[50])
{
int getIndexOfSigns(char ch);
int i,len;
double sum=0;
double t=1;
len=strlen(x);
for(i=len-1;i>=0;i--)
{
sum+= t*getIndexOfSigns(x[i]);
t*=16;
}
return sum;
}
int getIndexOfSigns(char ch)
{
if(ch >= '0' && ch <= '9')
{
return ch - '0';
}
if(ch >= 'A' && ch <='F')
{
return ch - 'A' + 10;
}
if(ch >= 'a' && ch <= 'f')
{
return ch - 'a' + 10;
}
return -1;
}
17.
#include
#include
int main()
{
void trans_string(int);
int x;
printf("请输入一个整数\n");
scanf("%d",&x);
if(x>=0)
{
trans_string(x);
}
else
{
printf("- ");
x+=2*(0-x);
trans_string(x);
}
return 0;
}
void trans_string(int a)
{
int i;
if((i=a/10)!=0)
trans_string(i);
putchar(a%10+'0');
printf(" ");
}
18.
#include
#include
int main()
{
int judge_day(int,int,int);
int year,month,day,rank;
printf("请输入年,月,日\n");
scanf("%d,%d,%d",&year,&month,&day);
rank=judge_day(year,month,day);
printf("这一天是第%d天",rank);
return 0;
}
int judge_day(int year,int month,int day)
{
int is_leap(int);
int rank=0;
int m_d[]={31,28,31,30,31,30,31,31,30,31,30,31};
int i;
for(i=1;i<month;i++)
{
rank+=m_d[i];
}
rank+=day;
if(is_leap(year)&&month>2)
rank+=1;
return rank;
}
int is_leap(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
return 0;
}