题目1:求100之内的素数
#include"stdio.h"
#include"math.h"
#define N 100
void main()
{
int m, i, found;
float t;
for(m = 2; m <= N; m++){
t = sqrt(m);
found = 1;
for(i = 2; i <= t; i++){
if(m%i ==0){
found = 0;
break;
}
}
if(found){
printf("%d ", m);
}
}
}
#include"stdio.h"
#define N 10
void main()
{
int i, t, j, m[N];
printf("请输入十个数:");
for(i = 0; i < N; i++)
scanf("%d", &m[i]);
for(i = 0; i < N-1; i++){
for(j = 0; j <N-1-i; j++){
if(m[j] < m[j+1]){
t = m[j+1];
m[j+1] =m[j];
m[j] = t;
}
}
}
printf("排序后结果:");
for(i = 0; i < N; i++)
printf("%d ", m[i]);
}
题目3:求一个3*3矩阵对角线元素之和
#include"stdio.h"
#define N 3
void main()
{
int i, j;
float m[N][N], t, sum=0.;
printf("请输入3行3列:\n");
for(i = 0; i < N; i++){
for(j = 0; j < N; j++)
scanf("%f", &m[i][j]);
}
for(i = 0; i < N; i++){
t = m[i][i];
sum = sum + t;
}
printf("对角线元素之和为:%.f\n", sum);
getchar();
}
题目4:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
#include"stdio.h"
void main()
{
int a[11]={11,23,45,49,50,51,53,63,77,90};
int x, i, j;
printf("请输入一个数:");
scanf("%d", &x);
if(x >= a[9])
a[10] = x;
for(i = 0;;i++){
if(x >= a[i] && x <= a[i+1])
break;
}
for(j = 10; j >= i+1; j--){
a[j+1] = a[j];
}
a[i+1] = x;
printf("插入排序后为:");
for(i = 0; i < 11; i++)
printf("%3d", a[i]);
getchar();
getchar();
}
题目5:将一个数组逆序输出。
#include"stdio.h"
#define N 5
void main()
{
int x = N, i, a[N], n=0;
printf("请输入%d个数:", x);
for(i = 0; i< N; i++){
scanf("%d", &a[i]);
n++;
}
if(n != N){
printf("error");
return;
}
printf("逆序输出为:");
for(i = N-1; i >=0; i--)
printf("%2d", a[i]);
}