Ch1. Intro to Programming

1-1 Input three integers and output the average number. Keep three decimal places. 

#include<stdio.h>
//#include<math.h>
int main()
{
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
printf("%.3f\n", (a+b+c)/3.0);
}

1-2 Input  Fahrenheit temperature f, output the centigrade degree c. Keep three decimal places. Hint: c=5(f-21)/9.

#include<stdio.h>
#include<math.h>
int main()
{
float f,c;
scanf("%f",&f);
c=*(f-)/;
printf("%.3f\n", c);
}

1-3 Input a positive integer n, output the value of 1+2+3+...+n.

Hint: Solve the problem rather than exercise programming. 

#include<stdio.h>
//#include<math.h>
int main()
{
int n,sum;
sum=;
scanf("%d", &n);
/*if(n == 1)
printf("1\n");
else */
{
for(int i=; i<=n; i++)
sum=sum+i;
printf("%d\n", sum);
} }

1-4 Input positive integer n (n<360), output the sin and cos value of n.

Hint: Use math functions.

#include<stdio.h>
#include<math.h>
int main()
{
int n;
scanf("%d", &n);
printf("%f %f\n", sin(n), cos(n));
}

1-5 A piece of clothes costs 95 yuan, and if the amount adds up to 300 yuan, it will have a 85% discount. Input  the sales number of clothes and output the money one should pay (yuan). Keep two places of decimal.

#include<stdio.h>
//#include<math.h>
int main()
{
int n;
float total;
scanf("%d", &n);
total=n*;
if (total<)
printf("%.2f\n", total);
else
printf("%.2f\n", total*0.85);
}

1-6 Input three lengths of a triangle (all possitive integers), and determine whether they can be three sides of a triangle. If so, output yes; otherwise, output not a triangle.

#include<stdio.h>
//#include<math.h>
int main()
{
int a,b,c;
scanf("%d%d%d", &a, &b, &c);
if ((a+b>c) && (a+c>b) && (b+c>c))
printf("yes");
else
printf("not a triangle");
return ;
}

1-7 Input a year and determine if it is a leap year. If yes, output yes; otherwise, output no.

#include<stdio.h>
//#include<math.h>
int main()
{
int n;
scanf("%d", &n);
if ((n% == ) || ((n% == ) && (n% != )))
printf("yes");
else
printf("no");
return ;
}

Q1 What's the minimum and maximum number of int integers? (give accurate number)

#include<stdio.h>
//#include<math.h>
int int_min()
{
int n=, i=;
while(n>=i)
{
n=i;
i--;
}
return n;
}
int int_max()
{
int n=, i=;
while(n <=i)
{
n=i;
i++;
}
return n;
} int main()
{
printf("%d\n", int_min());
printf("%d\n", int_max());
}

Q2 How actuate decimal places can double floats can be? Or, is it a right question?

#include<stdio.h>
//#include<math.h>
int main()
{
double i =1.0, j = 3.0;
printf("%.500f\n", i/j);
return ;
}

Ref. https://www.zhihu.com/question/36662447/answer/68630339

It shows 0.333333333333333310000000000...    (sixteen 3s ) so, there are maximum 16 decimal places in a double float. And  a double float can not acturately describe decimals.

Q3 What's the minimum and maximum positive integer of double floats? (no need to give accurate numbers)

(I don't know...)

Ref. http://blog.csdn.net/architect19/article/details/8551145

http://www.faceye.net/search/87209.html

Not working through...

Q4 What's the precedence of these logical operators, "&&", "||", and "!" ? How do you interpret a&&b||c ?

"&&" and "||"at the same precedence level, and are operated from left to right.

#include<stdio.h>
//#include<math.h>
int main()
{
int a=, b=,c=;
printf("%d", b&&c||a);
return ;
}

It shows 1.

"!" has higher priority than "&&" and "||" .

#include<stdio.h>
//#include<math.h>
int main()
{
int a=, b=,c=;
printf("%d", !b&&c||a);
return ;
}

It shows 1.

#include<stdio.h>
//#include<math.h>
int main()
{
int a=, b=,c=;
printf("%d", !(b&&c||a));
return ;
}

It shows 0.

Q5 What's the accurate meaning of if(a) if(b) x++; else y++? The else matches which if? Any accturate way to express the match?

The esle matches the second if.

#include<stdio.h>
int main()
{
int a, b,x, y;
if(a){
if (b)
x++;
else
y++;
}
return ;
}

Use braces to enclose a series of codes following if(a).

上一篇:jar 文件


下一篇:计算器的单元测试dome