进制转换题

n转十

cs 复制代码
#include <stdio.h>
#include <math.h>

int main(int argc, char *argv[])
{
  int ans=0,n=2022;
  int k=0;
  while(n)
  {
    ans+=pow(9,k)*(n%10);
    n/=10;
    k++;
  }
  printf("%d",ans);
  return 0;
}

十转n

cs 复制代码
#include <stdio.h>
#include <stdlib.h>

int f2(int x)
{
  int a=0;
  while(x)
  {
    a+=x%2;
    x/=2;
  }
  return a;
}

int f4(int x)
{
  int a=0;
  while(x)
  {
    a+=x%4;
    x/=4;
  }
  return a;
}

int main(int argc, char *argv[])
{
  int cnt=0;
  for(int i=1;i<=2024;i++)
  {
    if(f2(i)==f4(i)) cnt++;
  }
  printf("%d",cnt);
  return 0;
}
cs 复制代码
#include <stdio.h>

typedef long long LL;

const int p=1000000007;
const int MAX=100000;

int max3(int a,int b,int c)
{
  int t=a>b?a:b;
  return t>c?t:c;
}

int max2(int a,int b)
{
  return a>b?a:b;
}

int main(int argc, char *argv[])
{
  int n,m1,m2;
  int a[MAX];
  int b[MAX];
  scanf("%d %d",&n,&m1);
  for(int i=m1-1;i>=0;i--)
  {
    scanf("%d",&a[i]);
  }
  scanf("%d",&m2);
  for(int i=m2-1;i>=0;i--)
  {
    scanf("%d",&b[i]);
  }
  LL res=0;
  int maxlen=max2(m1,m2);
  for(int i=maxlen-1;i>=0;i--)
  {
    int ai=(i<m1)?a[i]:0;
    int bi=(i<m2)?b[i]:0;
    int mul=max3(ai+1,bi+1,2);
    res=(res*(LL)mul+(LL)ai-bi)%p;
    if(res<0) res+=p;
  }
  printf("%lld",res);
  return 0;
}
cs 复制代码
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
  int n;
  scanf("%d",&n);
  int count=1;
  int a[1000]={};
  int sign=1;     //标记是否存在优秀的拆分
  if(n%2==1){     //题目要求是2的正整数幂,把数转化为二进制,整数不断除二,若第一个余数为1,说明有2的0次方,直接输出-1
      sign=0;
    }
  else{
    while(n){
      int w=n%2;
      a[count++]=w;
      n=n/2;
    }
  }
  count-=1;
  if(sign==0) printf("-1");
  else{
    while(count){
      int w=pow(2,count-1);
      if(a[count]!=0) printf("%d ",a[count]*w);   //如果该项为0,则不输出
      count--;
    }
  }
  return 0;
}
相关推荐
我找到地球的支点啦20 分钟前
Matlab系列(009) 一CRC循环冗余校验详解
开发语言·数据结构·算法·matlab·信息与通信
予昊23 分钟前
从零实现“在线五子棋对战“:WebSocket 实时通信 + 段位匹配
java·开发语言·网络·websocket
罗西的思考32 分钟前
【OpenClaw具身硬件】ZeroClaw 源码阅读笔记(3)--- RAG
人工智能·算法·机器学习
weixin_461408581 小时前
Mybatis-flex小记
java·开发语言·mybatis
浪里镖客1 小时前
位姿转换矩阵写法-个人习惯(计算机理解其实是相反的)
线性代数·算法·矩阵
青梅味猪大肠2 小时前
【深入浅出C++】为什么虚表指针可以解决菱形继承
开发语言·c++
潘潘的嵌入式日记2 小时前
I²C 从机接收总被覆盖?双缓冲要在 STOP 时交接
c语言·开发语言·单片机
Lhan.zzZ3 小时前
QML 组件库:VS2022 + Qt 静态库方案
开发语言·c++·qt·visual studio
敲代码的嘎仔3 小时前
28届后端开发-海康威视日常实习一面(已OC)
java·开发语言·后端·面试·海康威视·实习·大厂
小白羊丨4 小时前
如何诊断 Prompt 模板导致的效果下降?
人工智能·算法·prompt