编程笔记三

作者在 2015-10-14 22:31:11 发布以下内容

题一:简单几何题

数学课上,老师出了一道简单的关于三角形几何题,题目内容是:给出三个数字分别代表三条边,首先请判断利用这三条边是否可以组成一个三角形;如果可以,请计算三角形的周长和面积;否则,输出提示信息说明不能组成三角形。你可以用编程实现这道题的求解吗?

Input:

输入三个整数,a, b, c。

Output:

如果3边可以组成三角形,则输出为两行。

第一行为:”the perimeter of this triangle is: 周长!”

第二行为:”the area of this triangle is: 面积!”

否则,输出一行:

this is not a triangle!

答案:

#include<stdio.h>
#include<math.h>


int main(void) {
  int a, b, c;
  float m, p, n;
  scanf("%d %d %d", &a, &b, &c);
  if (a + b > c && a + c > b && b + c > a) {
    m = a+b+c;
    printf("the perimeter of this triangle is: %f!\n", m);       注:%f是float的输出字符号
    p = m/2;
    n = sqrt(p*(p-a)*(p-b)*(p-c));
    printf("the area of this triangle is: %f!\n", n);
  } else {
    printf("this is not a triangle!\n");
  }
  return 0;
}

MY CODE:

#include<stdio.h>
#include<math.h>


int main(void) {
  int a, b, c;
  float m, p, n;
  scanf("%d %d %d", &a, &b, &c);


  if (a < b)                                                         注意:适当换行,注意代码的可读性

    a ^= b ^= a ^= b;                                                 尽量用简洁的代码,简洁的思路
  if (a < c)
    a^=c^=a^=c;
  if (b < c)
    b^=c^=b^=c;


  if (a < b+c) {
  m = a+b+c;
    printf("the perimeter of this triangle is: %.2f!\n", m);
  p = m/2;
  n = sqrt(p*(p-a)*(p-b)*(p-c));
    printf("the area of this triangle is: %.2f!\n", n);
  } else {
    printf("this is not a triangle!\n");
  }
  return 0;
}

题二:square or circle

    Now you have a rope with a length of C (may not be an integer).You can form a circle or a square with the rope.How much is the circle's area larger than the square's?

 答案:


					
  1. #include <stdio.h>
  2.  
  3. #define PI 3.1415927
  4.  
  5. int main() {
  6. double c;
  7. scanf("%lf", &c);
  8. printf("%.2lf\n", c * c * (4 - PI) / 16 / PI);
  9. return 0;
  10. }
MYCODE:

#include<stdio.h>
int main() {
  float a;
  float b, c, m, n, s;
  scanf("%f", &a);                                 注意: scanf函数不用换行
  b = a/2/3.1415927;
  m = 3.1415927*b*b;
  c = a/4;
  n = c*c;
  s = m-n;
  printf("%.2f\n", s);
  return 0;
}

题三:Manhattan distance(for lab) 

     Input four integer x1, y1, x2, y2, which is mean that the coordinates of two points A(x1, y1), B(x2, y2). [0 <= x1,y1,x2,y2 <= 10000]Please output manhatton distance between the two points.if you want to use the abs() function to calculate the absolute value, you should include the stdlib.h head file.You also can compare X1 and X2.And then use the big value to minus the small value.

提示: d(i,j)=|X1-X2|+|Y1-Y2|.
ANSWER:


							
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main() {
  5. int x1, y1, x2, y2;
  6. int sum = 0;
  7.  
  8. scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  9. sum = abs(x1 - x2) + abs(y1 - y2);
  10. printf("%d\n", sum);
  11.  
  12. return 0;
  13. }
MYCODE:

#include<stdio.h>
#include<stdlib.h>


int main() {
  int a, b, c, d, m, n, s;
  scanf("%d %d %d %d", &a, &b, &c, &d);
  m = abs(a-c);
  n = abs(b-d);
  s = m+n;
  printf("%d\n", s);
  return 0;
}


默认分类 | 阅读 1680 次
文章评论,共0条
游客请输入验证码
浏览15674次
文章分类