第七题
square3.c代码
#include<stdio.h>
int main()
{
int i, n, odd, square;
printf("This program prints a table of squares.\n");
printf("Enter number of entries in table: ");
scanf_s("%d", &n);
i = 1;
odd = 3;
for (square = 1; i <= n; odd+=2)
{
printf("%10d%10d\n", i, square);
++i;
square += odd;
}
return 0;
}
示例代码
#include<stdio.h>
int main()
{
int i, n;
int currentSquare = 1; // 当前平方数
int oddIncrement = 3; // 每次增加的奇数
printf("This program prints a table of squares.\n");
printf("Enter number of entries in table: ");
scanf_s("%d", &n);
for (i = 1; i <= n; ++i)
{
printf("%10d%10d\n", i, currentSquare);
currentSquare += oddIncrement;
oddIncrement += 2; // 下一个奇数
}
return 0;
}
输出
第八题
代码示例
#include <stdio.h>
int main() {
int daysInMonth, startDay;
int i, day = 1;
// 获取用户输入
printf("Enter the number of days in month: ");
scanf_s("%d", &daysInMonth);
printf("Enter starting day of the week: ");
scanf_s("%d", &startDay);
// 输出日历标题
printf("Sun Mon Tue Wed Thu Fri Sat\n");
// 输出日历前的空格
for (i = 1; i < startDay; i++) {
printf(" ");
}
// 输出日历
while (day <= daysInMonth) {
printf("%3d ", day);
if ((day + startDay - 1) % 7 == 0) {
printf("\n");
}
day++;
}
// 如果最后一行没有填满,则换行
if ((day + startDay - 2) % 7 != 0) {
printf("\n");
}
return 0;
}
输出
第九题
代码示例
#include <stdio.h>
float calculate_remaining_balance(float balance, float rate, float payment);
int main()
{
float loan = 0.0f;
float payment = 0.0f;
float rate = 0.0f;
int numberOfPayments = 0;
// 获取用户输入
printf("Enter amount of loan: ");
scanf_s("%f", &loan);
printf("Enter interest rate (as a percentage): ");
scanf_s("%f", &rate);
printf("Enter monthly payment: ");
scanf_s("%f", &payment);
printf("Enter number of payments: ");
scanf_s("%d", &numberOfPayments);
// 初始化当前余额为贷款金额
float currentBalance = loan;
// 输出标题
printf("\nPayment\tRemaining Balance\n");
printf("---------------------------\n");
// 循环计算每次还贷后的剩余金额
for (int i = 1; i <= numberOfPayments; i++)
{
currentBalance = calculate_remaining_balance(currentBalance, rate, payment);
printf("%d\t\t%.2f\n", i, currentBalance);
}
return 0;
}
// 计算剩余金额的函数
float calculate_remaining_balance(float balance, float rate, float payment)
{
// 先计算一个月的利息,然后从余额中减去支付金额
float interestForMonth = balance * ((rate / 100.0f) / 12.0f);
float newBalance = balance - payment + interestForMonth;
return newBalance;
}
输出
第十题
示例代码
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> // 用于INT_MAX
#define MAX_DATES 100 // 定义最大日期数量
// 日期结构体
typedef struct {
int month;
int day;
int year;
} Date;
// 函数声明
Date findEarliestDate(Date dates[], int count);
int main() {
Date dates[MAX_DATES];
int count = 0;
while (1) {
int month, day, year;
printf("Enter date (mm/dd/yy) (0/0/0 to end): ");
if (scanf_s("%d/%d/%d", &month, &day, &year) != 3) {
printf("输入错误,请重新输入。\n");
continue;
}
// 检查是否为结束输入的信号
if (month == 0 && day == 0 && year == 0) {
break;
}
// 存储日期
dates[count].month = month;
dates[count].day = day;
dates[count].year = year;
count++;
// 检查是否达到最大日期数量
if (count >= MAX_DATES) {
printf("已达到最大日期数量限制。\n");
break;
}
}
// 如果没有输入任何日期,则不执行后续操作
if (count == 0) {
printf("没有输入任何日期。\n");
return 0;
}
// 找出并打印最早的日期
Date earliestDate = findEarliestDate(dates, count);
printf("最早的日期是:%02d/%02d/%02d\n", earliestDate.month, earliestDate.day, earliestDate.year);
return 0;
}
// 找出最早日期的函数
Date findEarliestDate(Date dates[], int count) {
Date earliest = { INT_MAX, INT_MAX, INT_MAX }; // 初始化为一个非常大的日期
for (int i = 0; i < count; i++) {
if (dates[i].year < earliest.year ||
(dates[i].year == earliest.year && dates[i].month < earliest.month) ||
(dates[i].year == earliest.year && dates[i].month == earliest.month && dates[i].day < earliest.day)) {
earliest = dates[i];
}
}
return earliest;
}
输出
第十一题
示例代码
#include <stdio.h>
int main() {
int N;
printf("请输入N的值:");
scanf_s("%d", &N);
// 初始化e的近似值和当前项的倒数
double e_approx = 1.0; // 第一项是1/0! = 1
double term = 1.0; // 当前项的倒数,初始化为1/1!
// 计算e的近似值
for (int n = 1; n <= N; n++) {
term /= n; // 计算下一项的倒数
e_approx += term; // 累加到e的近似值中
}
printf("e的近似值(使用%d项)是:%.15f\n", N, e_approx);
return 0;
}
输出
第十二题
示例代码
#include <stdio.h>
int main() {
int n = 0; // 初始化项数计数器
double epsilon; // 定义epsilon变量
double e_approx = 1.0; // e的近似值初始化为1.0(即第一项1/0!)
double term = 1.0; // 当前项的倒数,从1/1!开始
// 询问用户输入epsilon值
printf("请输入一个较小的浮点数epsilon:");
scanf_s("%lf", &epsilon);
// 验证输入的epsilon是否大于0
if (epsilon <= 0) {
printf("输入的epsilon必须大于0。\n");
return 1;
}
// 持续执行加法运算,直到当前项小于epsilon
while (term >= epsilon) {
n++; // 项数计数器加1
term /= n; // 计算下一项的倒数(即1/n!)
e_approx += term; // 将当前项累加到e的近似值中
}
// 输出e的近似值和达到该近似值所需的项数
printf("为达到小于%.15f的精度,使用了%d项。\n", epsilon, n);
printf("e的近似值是:%.15f\n", e_approx);
return 0;
}
输出