上机练习10
p704.c
#include <stdio.h>
#define N 100
int main(void)
{
int data;
int i, j;
int fact[N];
int sum, count;
printf("Please input an integer: ");
scanf("%d", &data);
for (i = 2; i < data; i++)
{
sum = 0;
count = 0;
for (j = 1; j <= i / 2; j++)
{
if (i % j == 0)
{
fact[count++] = j;
sum += j;
}
}
if (i == sum)
{
printf("%6d is a wanshu", i);
for (j = 0; j < count; j++)
{
printf(" %d", fact[j]);
}
printf("\n");
}
}
return 0;
}
p706.c
#include <stdio.h>
#define N 10
int main(void)
{
int data[N], tmp;
int i, j;
printf("Please input 10 number:\n");
for (i = 0; i < N; i++)
{
scanf("%d", &data[i]);
}
for (i = 0; i < N - 1; i++)
{
for (j = 0; j < N - 1; j++)
{
if (data[j] > data[j + 1])
{
tmp = data[j];
data[j] = data[j + 1];
data[j + 1] = tmp;
}
}
}
for (i = 0; i < N; i++)
{
printf("%6d", data[i]);
}
return 0;
}
p713.c
#include <stdio.h>
int main(void)
{
int day, month, year, sum;
printf("Please input year-month-day: ");
scanf("%d-%d-%d", &year, &month, &day);
switch (month)
{
case 1:
sum = 0;
break;
case 2:
sum = 31;
break;
case 3:
sum = 59;
break;
case 4:
sum = 90;
break;
case 5:
sum = 120;
break;
case 6:
sum = 151;
break;
case 7:
sum = 181;
break;
case 8:
sum = 212;
break;
case 9:
sum = 243;
break;
case 10:
sum = 273;
break;
case 11:
sum = 304;
break;
case 12:
sum = 334;
break;
default:
printf("data error");
}
sum += day;
if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month > 2)
sum++;
printf("\nIt is the %dth day.\n", sum);
return 0;
}
p714.c
#include <stdio.h>
#define N 10
int main(void)
{
int a[N];
int zhengshu = 0, fushu = 0, all = 0;
int i;
printf("Input 10 integers: ");
for (i = 0; i < N; i++)
{
scanf("%d", &a[i]);
if (a[i] > 0)
{
zhengshu += a[i];
all += a[i];
}
else if (a[i] < 0)
{
fushu += a[i];
all += a[i];
}
else
{
all += a[i];
}
}
printf("\nzhengshu=%d,fushu=%d,all=%d", zhengshu, fushu, all);
return 0;
}
p717.c
#include <stdio.h>
#define N 10
int main(void)
{
int a[N], zhi[N], xb[N];
int i;
int count = 0;
printf("Input 10 integers: ");
for (i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < N; i++)
{
if (a[i] % 2 != 0 && i % 2 != 0)
{
zhi[count] = a[i];
xb[count] = i;
count++;
}
}
printf("\ncount=%d\n", count);
for (i = 0; i < count; i++)
{
printf("a[%d]=%d\n", xb[i], zhi[i]);
}
return 0;
}
p718.c
#include <stdio.h>
#define N 15
int main(void)
{
int i, n;
double fn[N] = {0, 1, 2};
printf("Input n (13>=n>=2): ");
scanf("%d", &n);
for (i = 2; i < n; i++)
{
fn[i + 1] = 2 * fn[i] + fn[i - 1] * fn[i - 2];
}
printf("\nf(%d)=%.0lf\n", n, fn[n]);
return 0;
}
p725.c
#include <stdio.h>
#define N 21
int main(void)
{
int i, n;
double fn[N] = {1, 1};
printf("input a data(3--20): ");
scanf("%d", &n);
for (i = 2; i < n; i++)
{
fn[i] = fn[i - 1] + fn[i - 2];
}
for (i = 0; i < n; i++)
{
printf("%.0lf\t", fn[i]);
}
return 0;
}
p739.c
#include <stdio.h>
int main(void)
{
int year, month, day;
int i;
int sum = 0;
int monthday[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("input a data(year-month-day):");
scanf("%d-%d-%d", &year, &month, &day);
for (i = 1; i < year; i++)
{
sum += 365;
if (i % 400 == 0 || i % 4 == 0 && i % 100 != 0)
{
sum++;
}
}
for (i = 1; i < month; i++)
{
sum += monthday[i];
}
if ((year % 400 == 0 || year % 4 == 0 && year % 100 != 0) && month > 2)
{
sum++;
}
sum += day;
printf("\nThe result is %d\n", sum);
return 0;
}
p742.c
#include <stdio.h>
#define N 5
int main(void)
{
int a[N], b[N];
int i;
int sum = 0;
printf("Input A: ");
for (i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
printf("Input B: ");
for (i = 0; i < N; i++)
{
scanf("%d", &b[i]);
}
for (i = 0; i < N; i++)
{
sum += a[i] * b[N - i - 1];
}
printf("\nsum=%d\n", sum);
return 0;
}
p746.c
#include <stdio.h>
#define N 10
int main(void)
{
int a[N];
int i, j, min, tmp;
printf("please input 10 integer numbers: ");
for (i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
printf("\nthe array before sorted: ");
for (i = 0; i < N; i++)
{
printf("%d ", a[i]);
}
for (i = 0; i < N - 1; i++)
{
min = i;
for (j = i + 1; j < N; j++)
{
if (a[j] < a[min])
{
min = j;
}
}
tmp = a[i];
a[i] = a[min];
a[min] = tmp;
}
printf("\nthe array after sorted: ");
for (i = 0; i < N; i++)
{
printf("%d ", a[i]);
}
return 0;
}
|