哈工大sse做题网站
中等题第9~12章只有12道题
以前做了改错题有30道, 没有保存.
1.
#include<stdio.h>
float (f1)(float x)
{
return 1 + x * x;
}
float (f2)(float x)
{
return x / (1 + x * x);
}
float Integral(float (*f)(float), float a, float b)
{
int i, n = 100;
float d = (b-a)/100;
float res = 0;
for(i = 0; i < n; i ++)
{
res += ((*f)(a+i*d) + (*f)(a+(i+1)*d)) / 2* d;
}
return res;
}
int main()
{
printf("y1=%f\ny2=%f\n", Integral(f1, 0, 1), Integral(f2, 0, 3));
return 0;
}
2.
#include <stdio.h>
void MonthDay(int year, int yearDay, int *pMonth, int *pDay)
{
int leap = year % 4 == 0 && year % 100 !=0 || year % 400 == 0;
int monthday[13] = {0, 31, 28 + leap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i;
for(i = 1; i <= 12; i ++)
{
if (yearDay <= monthday[i])
{
*pMonth = i;
*pDay = year;
break;
}
yearDay -= monthday[i];
}
}
int main()
{
int year, yearDay, month, day;
printf("Please enter year, yearDay:");
scanf("%d, %d", &year, &yearDay);
MonthDay(year, yearDay, &month, &day);
printf("month = %d, day = %d\n", month, day);
return 0;
}
3.
#include <stdio.h>
#define N 30
void FindMax(int score[],long num[],int n,int *pMaxScore,long *pMaxNum);
main()
{
int n;
int maxScore;
long maxNum;
int score[N];
long num[N];
printf("How many students?");
scanf("%d", &n);
printf("Input student’s ID and score:\n");
int i;
for(i = 0; i < n; i ++)
{
scanf("%ld%d", &num[i], &score[i]);
}
FindMax(score, num, n, &maxScore, &maxNum);
printf("maxScore = %d, maxNum = %ld\n", maxScore, maxNum);
}
/* 函数功能:计算最高分及其相应学生的学号 */
void FindMax(int score[],long num[],int n,int *pMaxScore,long *pMaxNum)
{
int i;
*pMaxScore = score[0];
*pMaxNum = num[0];
for(i = 1; i < n; i ++)
{
if (score[i] > *pMaxScore)
{
*pMaxScore = score[i];
*pMaxNum = num[i];
}
}
}
4.
#include <stdio.h>
#define N 10
void ReadData(int a[], int n);
void PrintData(int a[], int n);
void Swap(int *x, int *y);
main()
{
int a[N], b[N], i, n;
printf("Input array size(n<=10):");
scanf("%d", &n);
printf("Input array a:");
ReadData(a, n);
printf("Input array b:");
ReadData(b, n);
for(i = 0; i < n; i ++)
Swap(&a[i], &b[i]);
printf("Output array a:");
PrintData(a, n);
printf("Output array b:");
PrintData(b, n);
}
/* 函数功能:输入数组a的n个元素值 */
void ReadData(int a[], int n)
{
int i;
for (i = 0; i < n; i ++)
{
scanf("%d", &a[i]);
}
}
/* 函数功能:输出数组a的n个元素值 */
void PrintData(int a[], int n)
{
int i;
for (i = 0; i < n; i ++)
{
printf("%5d", a[i]);
}
puts("");
}
/* 函数功能:两整数值互换 */
void Swap(int *x, int *y)
{
int t;
t = *x; *x = *y; *y = t;
}
中等题第12章
5.
#include<stdio.h>
#include<string.h>
//const int N = 100; 为什么不能这样用呢? 细枝末节
#define N 100
struct candidate {
int vote;
char name[N];
}C[4];
int main()
{
char cname[3][N] = {"li", "zhang", "wang"};
char name[N];
int i;
for(i = 0; i < 3; i ++)
{
strcpy(C[i].name, cname[i]);
}
for(i = 0; i < 10; i ++)
{
printf("Input vote %d:", i+1);
scanf("%s", name);
int t;
for(t = 0; name[t]; t ++)
if(name[t] >= 'A' && name[t] <= 'Z') name[t] += ('a' - 'A');
int j;
for(j = 0; j < 3; j ++)
{
if(strcmp(name, C[j].name) == 0)
{
C[j].vote ++;
break;
}
else if(j == 2) C[3].vote ++; // 废票处理
}
}
printf("Election results:\n");
for(i = 0; i < 3; i ++)
{
printf("%8s:%d\n", C[i].name, C[i].vote);
}
printf("Wrong election:%d\n", C[3].vote);
return 0;
}
6.
中等题11章
#include<stdio.h>
#define M 10
#define N 10
void Transpose(int (*a)[N], int (*at)[M], int m, int n)
{
int i, j;
for(i = 0; i < m; i ++)
{
for(j = 0; j < n; j ++)
{
at[j][i] = a[i][j];
// *(*(at+j)+i) = *(*(a+i)+j)
}
}
}
void InputMatrix(int (*a)[N], int m, int n)
{
int i, j;
for(i = 0; i < m; i ++)
{
for(j = 0; j < n; j ++)
{
scanf("%d", &a[i][j]); // *(a+i)+j
// *(a+i)[N] 这样写可以吗?
// a是指向一维数组的指针变量!! 即二维数组的行指针. 为什么不直接写成 **a 呢?
}
}
}
void PrintMatrix(int (*at)[M], int n, int m)
{
int i, j;
for(i = 0; i < n; i ++)
{
for(j = 0; j < m; j ++)
{
printf("%d\t", at[i][j]); // *(*(at+i)+j)
}
printf("\n") ;
}
}
// 先行数后列数!!! 大多是先m再n, 转置后先n再m.
// m不是与行数绑定的!!!
// 函数定义与函数调用的m与n的左右位置是一致的.
int main()
{
int n, m;
int a[M][N], at[N][M];
printf("Input m, n:");
scanf("%d, %d", &m, &n);
printf("Input %d*%d matrix:\n", m, n);
InputMatrix(a, m, n);
Transpose(a, at, m, n);
printf("The transposed matrix is:\n");
PrintMatrix(at, n, m);
return 0;
}
7.
#include<stdio.h>
//用动态数组编程
#include<stdlib.h>
#include<malloc.h>
int main()
{
int m, n;
int *pScore = NULL, maxScore, row, col;
printf("Input array size m,n:");
scanf("%d, %d", &m, &n);
pScore = (int*)malloc(sizeof(int) * m * n);
printf("Input %d*%d array:\n", m, n);
int i, j;
for(i = 0; i < m; i ++)
{
for(j = 0; j < n; j ++)
{
scanf("%d", (pScore + i * n + j));
}
}
row = col = 0;
maxScore = *pScore;
for(i = 0; i < m; i ++)
{
for(j = 0; j < n; j ++)
{
if(*(pScore + i * n + j) > maxScore)
{
maxScore = *(pScore + i * n + j);
row = i;
col = j;
}
}
}
printf("maxScore = %d, class = %d, number = %d\n", maxScore, row+1, col+1);
free(pScore);
return 0;
}
8.
按如下函数原型编程计算并输出n×n阶矩阵的转置矩阵。其中,n由用户从键盘输入。已知n值不超过10。
void Transpose(int *a, int n);
void Swap(int *x, int *y);
void InputMatrix(int *a, int n);
void PrintMatrix(int a, int n);
输入提示信息:“Input n:”
输入格式:“%d”
输入提示信息:"Input %d%d matrix:\n"
输出提示信息:“The transposed matrix is:\n”
输出格式:“%d\t”
#include<stdio.h>
#define N 12
void Swap(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
void Transpose(int *a, int n)
{
int i, j;
for (i = 0; i < n; i ++)
{
for (j = i; j < n; j ++)
{
Swap(a+i*n+j, a+j*n+i);
}
}
}
void InputMatrix(int *a, int n)
{
int i, j;
for (i = 0; i < n; i ++)
{
for (j = 0; j < n; j ++)
{
scanf("%d", a + i * n + j);
}
}
}
void PrintMatrix(int *a, int n)
{
int i, j;
for (i = 0; i < n; i ++)
{
for (j = 0; j < n; j ++)
{
printf("%d\t", *(a + i * n + j));
}
puts(""); //printf("\n");
}
}
int main()
{
int a[N][N];
printf("Input n:");
int n;
scanf("%d", &n);
printf("Input %d*%d matrix:\n", n, n);
InputMatrix(&a[0][0], n); //注意这儿不能写a, 因为参是数int*a, 而不是int*a[N];
Transpose(&a[0][0], n);
printf("The transposed matrix is:\n");
PrintMatrix(&a[0][0], n);
return 0;
}
9.
#include <stdio.h>
int main()
{
char *bColor[] = {"RED", "YELLOW", "BLUE", "WHITE", "BLACK"};
int i, j, k;
int cnt = 0;
for (i = 0; i < 5; i ++)
{
for (j = i + 1; j < 5; j ++)
{
for (k = j + 1; k < 5; k ++)
{
printf("%d:%s,%s,%s\n", ++cnt, *(bColor+i), *(bColor+j), *(bColor+k));
}
}
}
return 0;
}
10.
重复了, 又是一个转置题, 大多时间用于处理输入输出.
11.
#include<stdio.h>
#include<string.h>
#define N 1000
int main()
{
char a[N], b[N], c[N];
scanf("%s%s", a, b);
int len1 = strlen(a), len2 = strlen(b);
int len = len1 < len2 ? len1 : len2;
//printf("%d\n", len);
int i, j;
for(i = 0; a[i]; i ++)
{
j = 0;
int ti = i, flag = 0;
while(a[ti] == b[j])
{
ti ++; j ++;
if(a[ti] == '\0' || b[j] == '\0')
{
flag = 1; // 没注意到这里是双重循环, 浪费了不少时间
break;
}
}
if(flag) break;
}
for(i = 0; i < j; i ++) printf("%c", b[i]);
puts("");
return 0;
}
12.
经典判别单词的方法, 但我不知道我的方法有什么问题, 我自己运行都是正确的啊.
#include<stdio.h>
#include<string.h>
#define N 88
int my_CountWords(char str[])
{
int res = 0;
int i;
while(1)
{
while(str[i] == ' ') i ++;
if(str[i]) res ++;
while(str[i] != ' ' && str[i]) i ++;
if(str[i] == '\0') break;
}
return res;
}
int CountWords(char str[])
{
int i, num;
num = (str[0] != ' ') ? 1 : 0;//1
for (i = 1; str[i] != '\0'; i++)//1
{
if (str[i] != ' ' && str[i - 1] == ' ')//1
{
num++;//1
}
}
return num;
}
int main()
{
char str[N];
printf("Input a string:");
gets(str);
int n = CountWords(str);
printf("Numbers of words = %d\n", n);
return 0;
}
13.
过于无聊就不写了.