第一题
#include
#include
int main(void)
{
float a, b = 0;
do
{
printf("Enter a number:");
scanf_s("%f", &a);
b = (a > b)? a:b;
} while (a > 0);
printf("The lagest number entered was %f\n", b);
system("pause");
return 0;
}
第二题
方法一
辗转相除法(又名欧几里德法)
设两数为a,b设其中a 做被除数,b做除数,temp为余数
1、大数放a中、小数放b中;
2、求a/b的余数;
3、若temp=0则b为最大公约数;
4、如果temp!=0则把b的值给a、temp的值给a;
#include
#include
int main(void)
{
int a, b, temp = 1,c;
printf("Enter two numbers:");
scanf_s("%d %d", &a, &b);
if (b > a)
{
c = b;
b = a;
a = c;
}
while (temp != 0)
{
temp = a%b;
if (temp == 0)
printf("GREATEST COMMON DIVISOR:%d\n", b);
else
{
a = b;
b = temp;
}
}
system("pause");
return 0;
}
方法2 穷举法
从1到两数中的较小值,找到所有两数的约数,每次计算较大约数会覆盖上次较小的约数,这样最后输出的就是最大公约数
#include
#include
int main(void)
{
int a, b, i,c;
printf("Enter two numbers:");
scanf_s("%d %d", &a, &b);
if (b > a)
{
c = b;
b = a;
a = c;
}
for ( i = 1; i <= b; i++)
{
if (a%i == 0)
{
if (b%i == 0)
c = i;
}
}
printf("Greatesy common divisor:%d", c);
system("pause");
return 0;
}
还有好多方法可以参考这个https://blog.csdn.net/Brilliance_panpan/article/details/88372432
第三题
#include
#include
int main(void)
{
int a, b, i,c,aa,bb;
printf("Enter a franction:");
scanf_s("%d/%d", &a, &b);
aa = a, bb = b;
if (b > a)
{
c = b;
b = a;
a = c;
}
for (i = 1; i <= b; i++)
{
if (a%i == 0)
{
if (b%i == 0)
c = i;
}
}
printf("The lowest terms: %d/%d",(aa/c),(bb/c));
system("pause");
return 0;
}
第四题(在5…3的基础上做的)
#include
#include)
int main(void)
{
float commission, value = 1, number, price, commissionA;
while (value != 0)
{
printf("Enter number of trade: ");
scanf_s("%f", &number);
printf("Enter price of trade: ");
scanf_s("%f", &price);
value = price * number;
if (number < 2000.00f)
commissionA = (number * 33 + 3) / 100;
else
commissionA = (number * 33 + 2) / 100;
if (value < 2500.f)
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)
commission = 39.00f;
printf("Commission: $%.2f\n", commission);
printf("CommissionA: $%.2f\n", commissionA);
}
system("pause");
return 0;
}
第五题
#include
#include)
int main(void)
{
int a,b,c;
printf("Enre the number:");
scanf_s("%d",&a);
do
{
b = a % 10;
a = a / 10;
printf("%d", b);
} while (a > 0);
printf("\n");
system("pause");
return 0;
}
第六题
#include
#include
#include
int main(void)
{
int a,b;
printf("Enter a number:");
scanf_s("%d",&a);
b = sqrt(a);
for (int i = 2; i <= b;)
{
printf("%d\n", i*i);
i += 2;
}
system("pause");
return 0;
}
第七题
好像是改下for循环的控制命令就可以了
第八题
#include
#include
#include
int main(void)
{
int a, b, i,j;
printf("Enter the number of days in mounth:");
scanf_s("%d", &a);
printf("Enter the staring day of the week:");
scanf_s("%d", &b);
for (i = 0; i < ((b % 7)-1); i++)
printf("\t");
for (j = 1; j <= a; j++)
{
if ((i + j) % 7 != 0)
printf("%d\t", j);
else
printf("%d\n", j);
}
printf("\n");
system("pause");
return 0;
}
第九题
第二章的没做。所以。。。
第十题
#include
#include
int main(void)
{
int y, m, d ;
int y1 = 100, m1 = 100, d1 = 100, i = 1;
printf("Enter a data(mm/dd/yy):");
scanf_s("%d/%d/%d", &m, &d, &y);
while (y + m + d != 0)
{
if ((y * 365 + m * 30 + d) < (y1 * 365 + m1 * 30 + d1))
{
y1 = y;
m1 = m;
d1 = d;
}
printf("Enter a data(mm/dd/yy):");
scanf_s("%d/%d/%d", &m, &d, &y);
}
printf("%d/%d/%d is the earlest date\n",m1,d1,y1);
system("pause");
return 0;
}
第十一题
#include
#include
int main(void)
{
int n,i;
float e=1, j = 1;
printf("Enter a number:");
scanf_s("%d", &n);
for (i = 1; i <= n; i++)
{
j = j * i;
e = e + 1 / j;
}
printf("The E is %7.6f", e);
system("pause");
return 0;
}
第十二题
#include
#include
#include
int main(void)
{
int i;
float e,n,num=1, j = 1;
printf("Enter the number:");
scanf_s("%d", &n);
e = exp(1);
for (i = 1; abs(num-e)>n; i++)
{
j = j * i;
num = num + 1 / j;
}
printf("The E is %.10f\n", num);
system("pause");
return 0;
}