程序设计与算法 第三周测验

1 奇偶数判断

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
  int n;
  scanf("%d", &n);
  if (n%2==1)
    printf("odd\n");
  else
    printf("even\n");
  return 0;
}

2 求一元二次方程的根

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
  double a, b, c;
  double x1, x2;
  double eps = 0.000001;
  scanf("%lf %lf %lf", &a, &b, &c);
  double d = b*b - 4*a*c;
  if (d > eps)
  {
    x1 = (-b + sqrt(d))/(2*a);
    x2 = (-b - sqrt(d))/(2*a);
    if ( -eps < x1 && x1 < eps ) x1 = 0;
    if ( -eps < x2 && x2 < eps ) x2 = 0;
    printf("x1=%.5f;x2=%.5f\n", x1, x2);
  }
  else
  {
    x1 = -b / (2*a); // real part
    if ( -eps < x1 && x1 < eps ) x1 = 0;
    if (-eps < d && d < eps)
    {
      printf("x1=x2=%.5f\n", x1);
    }
    else
    {
      x2 = sqrt(-d) / (2*a); // imaginary part
      printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi\n", x1, x2, x1, x2);
    }
  }
  return 0;
}

3 点和正方形的关系

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
  int x, y;
  scanf("%d %d", &x, &y);
  if (-1<=x && x<=1 && -1<=y && y<=1)
    printf("yes\n");
  else
    printf("no\n");
  return 0;
}

4 苹果和虫子2

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
  int n, x, y, m;
  scanf("%d %d %d", &n, &x, &y);
  m = y/x;
  if (y%x > 0)
    m++;
  printf("%d\n", (n-m>0)?(n-m):0);
  return 0;
}

考虑苹果都吃光的情况

5 简单计算器

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
  int x, y, o;
  scanf("%d %d %c", &x, &y, &o);
  switch(o)
  {
    case '+':
      printf("%d\n", x+y);
      break;
    case '-':
      printf("%d\n", x-y);
      break;
    case '*':
      printf("%d\n", x*y);
      break;
    case '/':
      if(y==0)
        printf("Divided by zero!\n");
      else
        printf("%d\n", x/y);
      break;
    default:
      printf("Invalid operator!\n");
      break;
  }
  return 0;
}

6 求整数的和与均值

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
  int n, i, m;
  int t = 0;
  scanf("%d", &n);
  for(i=1;i<=n;i++)
  {
    scanf("%d", &m);
    t += m;
  }
  printf("%d %.5f\n", t, (double)(t)/n);
  return 0;
}

7 整数序列的元素最大跨度值

#include <iostream>
using namespace std;
int main()
{
  int n, m, min=1000, max=0;
  cin >> n;
  for(int i=1;i<=n;i++)
  {
    cin >> m;
    if(m<min) min=m;
    if(m>max) max=m;
  }
  cout << max-min << endl;
  return 0;
}

8 奥运奖牌计数

#include <iostream>
using namespace std;
int main()
{
  int n, a, b, c;
  int A=0, B=0, C=0;
  cin >> n;
  for(int i=1;i<=n;i++)
  {
    cin >> a >> b >> c;
    A+=a;
    B+=b;
    C+=c;
  }
  cout << A << " " << B << " " << C << " " << A+B+C << endl;
  return 0;
}

9 乘方计算

#include <iostream>
using namespace std;
int main()
{
  int a, n, A = 1;
  cin >> a >> n;
  for(int i=1;i<=n;i++)
    A *= a;
  cout << A << endl;
  return 0;
}

10 鸡尾酒疗法

#include <iostream>
using namespace std;
int main()
{
  int n, a, b;
  double x, y;
  cin >> n;
  cin >> a >> b;
  x = (double)100 * b / a;
  for(int i=2;i<=n;i++)
  {
    cin >> a >> b;
    y = (double)100 * b / a;
    if (y-x > 5)
      cout << "better" << endl;
    else if (x-y > 5)
      cout << "worse" << endl;
    else
      cout << "same" << endl;
  }
  return 0;
}

这题太无聊了,凭什么 x、y(有效率)不能用 int?

-eof-

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值