7.6递归求两个数的最大公因数
题目内容:
编写递归函数,求两个整数的最大公因数(辗转相除法)。编写主函数,输入两个整数,调用函数求最大公因数,在主函数中输出。
输入:两个正整数。
输出:一个整数。
输入输出样例:
输入:
24 42
输出:
6
#include <stdio.h>
int fun01(int x,int y);
int main(void){
int a,b;
scanf("%d %d",&a,&b);
int c = fun01(a,b);
printf("%d",c);
}
int fun01(int x,int y){
int m ;
if(x>y) m=y;
if(x<y) m=x;
for(m; m > 1 ;m--){
if(x%m == 0 && y%m == 0)
return m;
}
return 0;
}
- 4.5 插入一个学生信息
假定程序中已经有三个学生信息了,现在需要插入一个新的学生信息。具体要求如下:
1)必须用数组来存储学生信息,包括学号,班级,姓名,三门课程的成绩;
2)新输入的学生信息插入后,按照学生三门课程总成绩从高到低输出排序后的学生信息。
3)已有的三个学生的信息可以在数组初始化时静态赋值,要求这三个学生的信息必须是:
1001,11,zhang,99.5,88.5,89.5,277.5
1002,22,li,77.9,56.5,87.5,221.9
1003,11,wang,92.5,99.0,60.5,252.0
程序输入:
1004
12
zhao
95.8
85.6
74.9
输出如下:
1001,11,zhang,99.5,88.5,89.5,277.5
1004,12,zhao,95.8,85.6,74.9,256.3
1003,11,wang,92.5,99.0,60.5,252.0
1002,22,li,77.9,56.5,87.5,221.9
(注意:下划线标注部分为输入。输出的学生信息每个学生一行,行与行之间无空行;学生的各项信息之间用逗号分隔,采用英文标点,每行最后一列是三门课程的总成绩)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int i,j;
struct student{
int id;
int classnum;
char name[10];
float score1,score2,score3;
float sum;
}stu[4]=
{{1001,11,"zhang",99.5,88.5,89.5,277.5}
,{1002,22,"li",77.9,56.5,87.5,221.9}
,{1003,11,"wang",92.5,99.0,60.5,252.0}
};
scanf("%d",&stu[3].id);
scanf("%d",&stu[3].classnum);
scanf("%s",&stu[3].name);
scanf("%f",&stu[3].score1);
scanf("%f",&stu[3].score2);
scanf("%f",&stu[3].score3);
stu[3].sum=stu[3].score1+stu[3].score2+stu[3].score3;
for(i=0;i<4;i++){
for(j=i;j<4;j++){
if(stu[i].sum<stu[j].sum)
{
struct student t=stu[i];
stu[i]=stu[j];
stu[j]=t;
}
}
}
for(i=0;i<4;i++){
printf("%d,%d,%s,%.1f,%.1f,%.1f,%.1f\n",
stu[i].id,stu[i].classnum,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].sum);
}
return 0;
}
- 6.16旋转矩阵
题目内容:
编写函数,将一个矩阵逆时针旋转90度,如原矩阵为
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
旋转后为:
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
----------------------
函数原形为 :
void rotate(int A[][10], int m, int n, int B[][10]);
这里, A -- 原矩阵, B -- 结果矩阵
矩阵的输入、输出也用函数实现。
编写主函数,先输入矩阵的行数和列数,然后调用函数按行输入矩阵的元素(设均为整数),调用函数转动矩阵,调用函数显示结果。矩阵的行数、列数均不超过 10。
输入格式:
第1行,两个整数。表示矩阵的行数m和列数n。
以后m行,每行n个整数,用空格隔开。
输出格式:
n行,每行m个数,行中数据用一个空格隔开,末尾无空格。
输入样例:
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
输出样例:
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void rotate(int A[][10], int m, int n, int B[][10])
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
B[n-1-j][i]=A[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d",B[i][j]);
if(j==m-1) printf("\n");
else printf(" ");
}
}
}
int main(int argc, char *argv[]) {
int m=2,n=2;
int a[10][10],b[10][10];
scanf("%d %d",&m,&n);
rotate(a,m,n,b);
return 0;
}
- 6.3 写一个函数,实现两个字符串的比较。
写一个函数int strcmp(char *p1, char *p2)用来实现两个字符串的比较。在主函数中输入两个字符串,输出对应的结果。
输入:
hello
world
则输出:-1
输入:
world
hello
则输出1
输入:
hello
hello
则输出:0
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int strcmp (char *p1, char *p2)
{
int i=0;
while(p1[i]!='\0'&&p2[i]!='\0')
{
if(p1[i]>p2[i]){
return 1;
}
else if(p1[i]<p2[i]){
return -1;
}
i++;
}
return 0;
}
int main(int argc, char *argv[]) {
char a[10],b[10];
int n;
scanf("%s",a);
scanf("%s",b);
n=strcmp (a,b);
printf("%d",n);
return 0;
}