5.1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int a = 0, count = 0, x;
printf("Enter a number: ");
scanf("%d", &a);
x = a;
if (a == 0) {
printf("The number 0 has 1 digits.\n");
}
else {
while (a != 0) {
a /= 10;
printf("a==%d\n", a);
count++;
}
printf("The number %d has %d digits.\n", x, count);
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int a;
printf("Enter a number: ");
scanf("%d", &a);
if (a >= 0 && a <= 9) {
printf("The number %d has 1 digits.\n", a);
}
else if (a > 9 && a < 100) /*a>=10&&a<=99*/ {
printf("The number %d has 2 digits.\n", a);
}
else if (a < 99 & a < 1000) {
printf("The number %d has 3 digits.\n", a);
}
return 0;
}
5.2
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int hour, minute;
printf("Enter a 24-hour time: ");
scanf("%d : %d", &hour, &minute);
if (hour > 12) {
if (hour == 24) {
printf("Equivalent 12-hour time: 0:%d AM\n", minute);
}
else {
hour -= 12;
printf("Equivalent 12-hour time: %d:%d PM\n", hour, minute);
}
}
else {
printf("Equivalent 12-hour time: %d:%d AM\n", hour, minute);
}
return 0;
}
5.3
有点问题
#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;
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;
}
if (number < 2000) {
commission1 = 33.03*number;
}
else {
commission1 = number* 33.02;
}
printf("Commission: $%.2f\n", commission);
printf("Your opponent :$%.2f\n", commission1);
return 0;
}
5.4
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int wind_velocity;
printf("输入风速:");
scanf("%d", &wind_velocity);
if (wind_velocity < 1) { printf("Caim"); }
else if (wind_velocity < 3) { printf("Light air"); }
else if (wind_velocity < 28) { printf("Breeze"); }
else if (wind_velocity < 48) { printf("Gale"); }
else if (wind_velocity < 64) { printf("Storm"); }
else if(wind_velocity >63){ printf("Hurricane"); }
return 0;
}
5.5
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
float income, taxes;
printf("Enter your income: ");
scanf("%f", &income);
if (income < 750) { taxes = income * 0.01; }
else if(income<=2250){
taxes = 7.50 + (income - 750.0) * 0.02;
}
else if (income <= 3750) {
taxes = 37.50 + (income - 2250.0) * 0.03;
}
else if (income <= 5250) {
taxes = 82.50 + (income - 3750.0) * 0.04;
}
else if (income <= 7000) {
taxes = 142.50 + (income - 5250.0) * 0.05;
}
else {
taxes = 230.00 + (income - 7000.0) * 0.06;
}
printf("taxes==%.2f\n", taxes);
return 0;
}
5.6
#include <stdio.h>
int main (void)
{
int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, c;
int first_sum, second_sum, total;
int check;
printf ("Enter the 12 digits of a UPC: ");
scanf ("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",&d, &i1, &i2, &i3, &i4, &i5, &j1, &j2, &j3, &j4, &j5, &c);
first_sum = d + i2 + i4 + j1 + j3 + j5;
second_sum = i1 + i3 + i5 + j2 + j4;
total = 3 * first_sum + second_sum;
check = 9 - ((total - 1) % 10);
if (check == c)
printf ("VALID");
else
printf ("NOT VALID");
return 0;
}
5.7
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int a, b, c, d;
int max1, min1, max2, min2, max, min;
printf("Enter four integers: ");
scanf("%d%d%d%d", &a, &b, &c, &d);
if (a > b) {max1 = a; min1= b;}
else {max1 = b; min1 = a;}
if (c > d) {max2 = c; min2 = d;}
else {max2= d; min2 = c;}
if (max1 > max2) {max = max1; }
else { max = max2; }
if (min1 < min2) {min = min1; }
else { min = min2; }
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 4
void max_min(int a[], int n, int* max, int* min);
int main(void)
{
int b[N], i, big, small;
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++) {
scanf("%d", &b[i]);
}
max_min(b, N, &big, &small);
printf("Largest: %d\n", big);
printf("Smallest: %d\n", small);
return 0;
}
void max_min(int a[], int n, int* max, int* min)
{
int i;
*max = *min = a[0];
for (i = 1; i < n; i++) {
if (a[i] > * max) { *max = a[i]; }
else if (a[i] < *min) { *min = a[i]; }
}
}
5.8
#include <stdio.h>
int main (void)
{
int hours, minutes;
int time;
printf ("Enter a 24-hour time:");
scanf ("%d:%d", &hours, &minutes);
time = hours * 60 + minutes;
// 480 583 679 767 840 945 1140 1305 这是几个起飞时间换算为分钟的结果
if (time < 480){
printf ("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
} else if (time < 583) {
if ((time-480) < (583-time)) printf ("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
else printf ("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
} else if (time < 679) {
if ((time-583) < (679-time)) printf ("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
else printf ("Closest departure time is 11:19 a.m., arriving at 1:31 p.m");
} else if (time < 767) {
if ((time-679) < (767-time)) printf ("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
else printf ("Closest departure time is 12:47 a.m., arriving at 3:00 p.m");
} else if (time < 840) {
if ((time-767) < (840-time)) printf ("Closest departure time is 12:47 a.m., arriving at 3:00 p.m.");
else printf ("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
} else if (time < 945) {
if ((time-840) < (945-time)) printf ("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
else printf ("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
} else if (time < 1140) {
if ((time-945) < (1140-time)) printf ("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
else printf ("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
} else {
if ((time-1140) < (1305-time)) printf ("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
else printf ("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
}
return 0;
}
5.9
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int year1, year2, month1, month2, day1, day2;
printf("Enter first date (mm/dd/yy) : ");
scanf("%d/%d/%d", &month1, &day1,&year1 );
printf("Enter second date (mm/dd/yy) : ");
scanf("%d/%d/%d", &month2, &day2, &year2);
int max=0 ,min = 0;
if (year1 > year2) {
printf("%d/%d/%d is earlier than %d/%d/%d", month2, day2, year2, \
month1, day1, year1);
}
else if (year1 == year2) {
if (month1 > month2) {
printf("%d/%d/%d is earlier than %d/%d/%d", month2, day2, year2, \
month1, day1, year1);
}
else if (month1 == month2) {
if (day1 > day2) {
printf("%d/%d/%d is earlier than %d/%d/%d", month2, day2, year2, \
month1, day1, year1);
}
else if (day1 == day2) {
printf("666");
}
else {
printf("%d/%d/%d is earlier than %d/%d/%d", month1, day1, year1, \
month2, day2, year2);
}
}
else {
printf("%d/%d/%d is earlier than %d/%d/%d", month1, day1, year1, \
month2, day2, year2);
}
}
else {
printf("%d/%d/%d is earlier than %d/%d/%d", month1, day1, year1, \
month2, day2, year2);
}
return 0;
}