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

1. 角谷猜想

#include <iostream>
using namespace std;
int main()
{
  long n;
  cin >> n;
  while(n>1)
  {
    if(n%2==0) {
      cout << n << "/2=";
      n /= 2;
    } else {
      cout << n << "*3+1=";
      n = n*3+1;
    }
    cout << n << endl;
  }
  cout << "End" << endl;
  return 0;
}

考点很奇怪!n 为 long 类型

2. 正常血压

#include <iostream>
using namespace std;
int main()
{
  int n, a, b;
  int cnt = 0;
  int max = 0;
  cin >> n;
  for(int i=0; i<n; i++)
  {
    cin >> a >> b;
    if (90 <=a && a <= 140 && 60 <=b && b <= 90) {
      cnt++;
      max = (cnt>max)?cnt:max;
    } else {
      cnt = 0;
    }
  }
  cout << max << endl;
  return 0;
}
4
100 80
90 50
120 60
140 90
2

4
0 0
0 0
0 0
0 0
0

4
100 80
0 0
100 80
0 0
1

4
100 80
100 80
100 80
0 0
3

3. 数字反转

#include <iostream>
using namespace std;
int main()
{
  long long n;
  int zero = 0;
  cin >> n;
  if (n<0)
  {
    n = -n;
    cout << '-';
  }
  while(n>=10)
  {
    int m = n%10;
    if (m != 0)
      zero = 1;
    if (zero != 0 || m != 0)
      cout << m;
    n /= 10;
  }
  cout << n << endl;
  return 0;
}
123
321

-380
-83

0
0

1
1

1000
1

-10
-1

4. 求特殊自然数

#include <iostream>
using namespace std;
int main()
{
  int n;
  for (int i=0;i<=6;i++) {
    for (int j=0;j<=6;j++) {
      for (int k=0;k<=6;k++) {
          n = i*7*7+j*7+k;
          if (n != 0 && n == k*9*9+j*9+i) {
            cout << n << endl;
            cout << i << j << k << endl;
            cout << k << j << i << endl;
          }
      }
    }
  }
  return 0;
}

5. 雇佣兵

#include <iostream>
using namespace std;
int main()
{
  int M,N,X,a;
  cin >> M >> N >> X;
  while (X>0 && M>=N)
  {
    a = M/N;
    X -= (M % N != 0)?(a+1):a;
    if (X<0) break;
    N += a;
  }
  cout << N << endl;
  return 0;
}
5 2 10
6

2 5 10
5

2 2 10
3

3 2 1
2

3 2 2
3

6. 数字统计

#include <iostream>
using namespace std;
int main()
{
  int A,B;
  int n=0;
  cin >> A >> B;
  for (;A<=B;A++)
  {
    int i=A;
    while(i>0)
    {
      if(i%10==2) n++;
      i/=10;
    }
  }
  cout << n << endl;
  return 0;
}
2 22
6

2 100
20

1 1
0

1 2
1

-eof-

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值