第六章 C语言程序设计
程序员文章站
2022-05-28 15:18:52
...
6.1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
float a, max=0;
printf("Enter a number: ");
scanf("%f", &a);
while (a > 0) {
printf("Enter a number: ");
scanf("%f", &a);
if (max < a) {
max = a;
}
}
printf("The largest number entered was %.2f\n", max);
return 0;
}
6.2
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int a, b;
printf("Enter two integers: ");
scanf("%d%d", &a, &b);
while (b != 0) {
int t = a%b;
a = b;
b = t;
}
printf("Greatest common divisor: %d\n", a);
return 0;
}
6.3
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int a, b;
printf("Enter two integers: ");
scanf("%d/%d", &a, &b);
int molecule = a, denominator = b;
while (b != 0) {
int t = a%b;
a = b;
b = t;
}
printf("Greatest common divisor: %d/%d\n", molecule/a, denominator/a);
return 0;
}
6.4
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
float number, per_share, value, commission, commission1;
printf("Number of stocks entered and price per share: ");
scanf("%f%f", &number, &per_share);
value = number * per_share;
do {
if (value < 2500.00f) {
commission = 30.00f + .017f * value;
}
else if (value < 6250.00f) {
commission = 56.00f + .0066f * value;
}
else if (value < 20000.00f) {
commission = 76.00f + .0034f * value;
}
else if (value < 50000.00f) {
commission = 100.00f + .0022f * value;
}
else if (value < 500000.00f) {
commission = 155.00f + .0011f * value;
}
else {
commission = 255.00f + .0009f * value;
}
if (commission < 39.00f) {
commission = 39.00f;
}
printf("Commission: $%.2f\n", commission);
printf("Number of stocks entered and price per share: ");
scanf("%f%f", &number, &per_share);
value = number * per_share;
}while (number > 0);
return 0;
}
这是符合题目的答案
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
float value, commission;
printf("Number of stocks entered and price per share: ");
scanf("%f", &value);
do {
if (value < 2500.00f) {
commission = 30.00f + .017f * value;
}
else if (value < 6250.00f) {
commission = 56.00f + .0066f * value;
}
else if (value < 20000.00f) {
commission = 76.00f + .0034f * value;
}
else if (value < 50000.00f) {
commission = 100.00f + .0022f * value;
}
else if (value < 500000.00f) {
commission = 155.00f + .0011f * value;
}
else {
commission = 255.00f + .0009f * value;
}
if (commission < 39.00f) {
commission = 39.00f;
}
printf("Commission: $%.2f\n\n", commission);
printf("Number of stocks entered and price per share: ");
scanf("%f", &value);
}while (value > 0);
return 0;
}
6.5
逆序排列多位数
6.6
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int n;
printf("输入一个正整数:");
scanf("%d", &n);
for (int i = 2; i * i <= n; i += 2) {
printf("%d\n", i * i);
}
return 0;
}
6.7
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int i, n, odd, square;
printf("This progream prints a table of squares.\n");
printf("Enter number of entries in tables: ");
scanf("%d", &n);
odd = 3;
for (square = 1,i=1; i <= n; odd += 2,i++) {
printf("%10d%10d\n", i, square);
square += odd;
}
return 0;
}
6.8
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int days, start;
int count = 0;
printf("Enter number of days in month: ");
scanf("%d", &days);
printf("Enter starting day of the week (i=sun, 7=sat): ");
scanf("%d", &start);
for (int i = 1; i <= days; i++) {
for (start; start > 1; start--) {
printf(" \t");
count++;
}
printf("%d\t", i);
count++;
if (count%7==0) {
printf("\n");
}
}
return 0;
}
#include <stdio.h>
int main (void)
{
int day, week;
printf ("Enter number of days in month: ");
scanf ("%d", &day);
printf ("Enter starting day of the week (1=Sun, 7=Sat): ");
scanf ("%d", &week);
for (int i = week; i > 1; i--) printf (" ");
for (int i = 1; i <= day; i++, week++) {
printf ("%3d", i);
if (week == 7) {
week = 0;
printf ("\n");
}
}
return 0;
}
[另外一个版本](https://blog.csdn.net/qq_28012069/article/details/80569352)
6.9
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main (void)
{
float money, rate, payment;
printf ("Enter amount of loan:");
scanf ("%f", &money);
printf ("Enter interest rate:");
scanf ("%f", &rate);
printf ("Enter monthly payment:");
scanf ("%f", &payment);
int num;
printf ("Enter the number of repayments:(1~10) ");
scanf ("%d", &num);
for (int i = 1; i <= num; i++){
printf ("Balance remaining after ");
switch (i) {
case 1: printf ("1st"); break;
case 2: printf ("2nd"); break;
case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
printf ("%dth", i);
break;
}
printf (" payment:%.2f\n", money = ((money - payment) + (money * rate * 0.01/ 12)));
}
return 0;
}
6.10
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int month,day,year,year1,, month1, day1,;
int min_year = 0, min_month = 0, min_day = 0;
printf("Enter a date (mm/dd/yy) : ");
scanf("%d/%d/%d", &month, &day, &year);
while (month != 0) {
printf("Enter a date (mm/dd/yy) : ");
scanf("%d/%d/%d", &month1, &day1, &year1);
if (year1 > year && year1!=0) {
min_year = year; min_month = month; min_day = day;
}
else if (year1 == year && year1 != 0) {
if (month1 > month) {
min_year = year; min_month = month; min_day = day;
}
else if (month1 == month) {
if (day1 > day) {
min_year = year; min_month = month; min_day = day;
}
else if (day1 == day) {
printf("666");
}
else {
min_year = year1; min_month = month1; min_day = day1;
}
}
else {
min_year = year1; min_month = month1; min_day = day1;
}
}
else if(year1 < year && year1 != 0){
min_year = year1; min_month = month1; min_day = day1;
}
month = month1; year = year1; day = day1;
}
printf("%d/%d/%d is the earlier date\n", min_month, min_day, min_year);
return 0;
}
6.11
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
float b = 0;
int n;long long a = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
a = 1;
for (int j = i; j > 0; j--) {
a *= j;
}
b += 1.0/a;
}
printf("e == %f\n", b);
return 0;
}
6.12
#include <stdio.h>
int main (void)
{
float m;
float sum = 1.0f;
int temp;
printf ("Enter the number: ");
scanf ("%f", &m);
for (int i = 1; ; i++) {
temp = 1;
for (int j = i ; j >= 1; j--) {
temp *= j;
}
if (1.0f / temp <= m) break;
sum += 1.0f / temp;
}
printf ("The value of e is: %f", sum);
return 0;
}