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

1:Pell数列

#include <iostream>
using namespace std;
int main()
{
  int n;
  long k;
  long long P[1000000+1]={0};
  P[1]=1;
  P[2]=2;
  for (long i=3;i<=1000000; i++)
    P[i] = (2 * P[i-1] + P[i-2])%32767;
  cin >> n;
  while (n--)
  {
    cin >> k;
    cout << P[k] << endl;
  }
  return 0;
}

2:求最大公约数问题

#include <iostream>
using namespace std;
long long gy(long long a, long long b)
{
  if (a%b==0)
    return b;
  return gy(b, a%b);
}
int main()
{
  long long a,b;
  cin >> a >> b;
  cout << gy(a, b) << endl;
  return 0;
}

3:编程填空:第i位替换

#include <iostream>
using namespace std;

int bitManipulation1(int n, int m, int i) {
// 在此处补充你的代码
  return (((m>>i)&1) == 0) ? (n & (~(1<<i))) : (n | (1<<i));
}

int main() {
    int n, m, i, t;
    cin >> t;
    while (t--) { 
        cin >> n >> m >> i;
        cout << bitManipulation1(n, m, i) << endl;
    }
    return 0;
}

4:编程填空:第i位取反

#include <iostream>
using namespace std;

int bitManipulation2(int n, int i) {
// 在此处补充你的代码
  return (((n>>i)&1)==0) ? (n | 1<<i) : (n & (~(1<<i)));
}

int main() {
    int t, n, i;
    cin >> t;
    while (t--) {
        cin >> n >> i;
        cout << bitManipulation2(n, i) << endl;
    }
    return 0;
}

5:编程填空:左边i位取反

#include <iostream>
#include <bitset>
using namespace std;

int bitManipulation3(int n, int i) {
// 在此处补充你的代码
  cout << bitset<sizeof(int)*8>(n) << endl;
  cout << bitset<sizeof(int)*8>((unsigned int)n>>(sizeof(n)*8-i)) << endl;
  cout << bitset<sizeof(int)*8>(~(n>>(sizeof(n)*8-i))) << endl;
  cout << bitset<sizeof(int)*8>((~(n>>(sizeof(n)*8-i)))<<(sizeof(n)*8-i)) << endl;
  cout << endl;
  unsigned long m = (unsigned int)n;
  int o = (int)(((m<<i)&(0x0ffffffff))>>i);
  cout << n << " " << i << " " << m << endl;
  cout << bitset<sizeof(int)*8>(n<<i) << endl;
  cout << bitset<sizeof(int)*8>(m<<i) << endl;
  cout << bitset<sizeof(int)*8>(o) << endl;
  cout << bitset<sizeof(int)*8>( ((~(n>>(sizeof(n)*8-i)))<<(sizeof(n)*8-i)) + (int)((((unsigned long)n<<i)&(0x0ffffffff))>>i) )<< endl;
  return ((~(n>>(sizeof(n)*8-i)))<<(sizeof(n)*8-i)) + (int)((((unsigned long)n<<i)&(0x0ffffffff))>>i);
}

int main() {
    int t, n, i;
    cin >> t;
    while (t--) {
        cin >> n >> i;
        cout << bitManipulation3(n, i) << endl;
    }
    return 0;
}

6:编程填空:计算整数k

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    unsigned int t, n, i, j, m, mi, mj;
    cin >> t;
    while (t--) {
        cin >> n >> i >> j;
        mi = (n>>i)&1;
        mj = (((n>>j)&1)==0)?1:0;
        m = (mi<<i) + (mj<<j);
        for (unsigned int k=i+1; k<j; k++)
          m += 1<<k;
        cout << setbase(16) << m << endl;
    }
    return 0;
}

-eof-

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值