#include <stdio.h>
#include <conio.h>
/*
1、编程实现:有二维数组a[3][3]={{5.4,3.2,8},{6,4,3.3},{7,3,1.3}},
将数组a的每一行元素均除以该行上的主对角元素(第1行同除以a[0][0],
第2行同除以a[1][1],...),按行输出新数组。(20分)
*/
int main()
{
double a[3][3] = { { 5.4, 3.2, 8},
{ 6, 4, 3.3 },
{ 7, 3, 1.3 }
};
int i, j;
double tmp;
//---------------输出原数组------------------------------
puts("原数组内容:");
for (i = 0; i < 3; i ++)
{
for (j = 0; j < 3; j++)
{
printf("%lf ", a[i][j]);
}
putchar(10);
}
for (i = 0; i < 3; i++)
{
tmp = a[i][i];
for (j = 0; j < 3; j++)
{
a[i][j] /= tmp;
}
}
//---------------输出新数组------------------------------
puts("新数组内容:");
for (i = 0; i < 3; i ++)
{
for (j = 0; j < 3; j++)
{
printf("%lf ", a[i][j]);
}
putchar(10);
}
getch();
return 0;
}
#include <stdio.h>
#include <conio.h>
#include <math.h>/*
3、 编程:设x取值为区间[1,20]的整数,求函数f(x)=x-sin(x)- cos(x)
的最大值,要求使用自定义函数实现f(x)功能。(20分)
*/
double f(int x);int main()
{
int i, j;
double maxFx = 0.0, tmp = 0.0; for (i = 1; i <= 20; i++)
{
tmp = f(i);
if (maxFx - tmp < 1e-7)
maxFx = tmp;
} printf("maxF(x) = %lf\n", maxFx);
getch(); return 0;
}double f(int x)
{
return (double)(x - sin(x) - cos(x));
}
#include <stdio.h>
#include <conio.h>
#include <string.h>/*
4.编写函数fun,通过指针实现将一个字符串反向。要求主函数输入
字符串,通过调用函数fun实现输入字符串反向。(20分)
*/
void fun(char str[])
{
int len = strlen(str);
int i, j;
char tmp; for (i = 0, j = len-1; i < j; i++, j--)
{
tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
}int main()
{
char str[100]; gets(str); //输入字符串
puts("字符串反向前:");
puts(str); fun(str); puts("字符串反向后:");
puts(str); getch();
return 0;
}
#include <stdio.h>
#include <conio.h>
#include <string.h>/*
5、已知学生三门课程基本信息如下。请使用结构体编程,计算学生三门
课程平均成绩后,列表输出学生的姓名、数学、英语、计算机、平均分信
息,并按平均分排序。(20分)
姓名 数学 英语 计算机
Mary 93 100 88
Jone 82 90 90
Peter 91 76 71
Rose 100 80 92
*/
/*------------声明结构-------------------*/
typedef struct Student
{
char name[20];
float math;
float english;
float computer;
float avg;
}STU;int cmp(const void *lhs, const void *rhs)
{
STU *f = (STU *)lhs;
STU *s = (STU *)rhs;
return f->avg - s->avg > 1e-7;
}int main()
{
STU s[4] = {{"Mary", 93, 100, 88, 0.0}, {"Jone", 82, 90, 90, 0.0},
{"Peter", 91, 76, 71, 0.0}, {"Rose", 100, 80, 92, 0.0}};
float average = 0.0f;
int i, j; for (i = 0; i < 4; i++)
{
s[i].avg += s[i].math + s[i].english + s[i].computer;
s[i].avg /= 3;
}
//从小到大排序
qsort(s, 4, sizeof(s[0]), cmp); for (i = 0; i < 4; i++)
{
printf("%-8s%-5.0f%-5.0f%-5.0f%-5.3f\n", s[i].name,
s[i].math, s[i].english, s[i].computer, s[i].avg);
}
return 0;
}
#include <stdio.h>
#include <conio.h>
#include <string.h>
typedef struct ARTICLE
{
char word[15];
int counter;
}Article;
//判断是否已经存在
int isExist(Article *art, int size, char *tmp);
int main()
{
Article article[200];
char tmp[15];
int i = 0, size, index;
for (i = 0; i < 200; i++)//初始化
{
article[i].counter = 1;
}
i = 0;//不能少这一步
scanf("%s", article[i].word);
size = 1;
while ( scanf("%s", tmp) && strcmp(tmp, "000") != 0)
{
index = isExist(article, size , tmp);
//printf("index = %d\n", index);
if (index != -1)
{
article[index].counter++;
}
else
{
strcpy(article[++i].word, tmp);
size += 1;
}
}
for (i = 0; i < size; i++)
{
printf("%s\t", article[i].word);
}
putchar(10);
for (i = 0; i < size; i++)
{
printf("%d\t", article[i].counter);
}
putchar(10);
return 0;
}
int isExist(Article *art, int size, char *tmp)
{
int i;
for (i = 0; i < size; i++)
{
if (strcmp(tmp, art[i].word) == 0)
return i;//返回下标
}
return -1;
}