进制转换题

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;
}
相关推荐
Miao1213113 分钟前
微服务 API 测试实践:海外某民宿平台如何构建模式驱动测试基础设施
java·开发语言
CRMEB系统商城1 小时前
开源自建还是SaaS订阅?算一笔3年经济账
java·大数据·开发语言·开源
王维同学2 小时前
[自学][Windows C++]RunOnceEx 注册表分组键的安全遍历
c++·windows·安全·开源
keyipatience2 小时前
线程栈与TLS和线程互斥
java·linux·服务器·开发语言·ubuntu
我的xiaodoujiao3 小时前
快速学习Python基础知识详细图文教程9--函数进阶
开发语言·python·学习·测试工具
战族狼魂3 小时前
高频面试题精选:分治与AI Agent架构
人工智能·算法·大模型·大语言模型
李剑一3 小时前
你用过网易的将军令嘛?它底层实现账号保护的原理相当简单粗暴
算法
Scott9999HH3 小时前
告别流量波动玄学!从法拉第电磁定律到底层 C/C++ 流量累积算法,深度解密工业级电磁流量计选型与开发
c语言·c++·算法
爱喝水的鱼丶3 小时前
SAP-ABAP:ALV通用封装实践——搭建可复用的ALV开发工具类,减少80%重复代码
开发语言·性能优化·sap·abap·erp·alv
中达瑞和-高光谱·多光谱3 小时前
1nm到8nm光谱分辨率,你的应用该选哪款光谱相机?
算法·高光谱·高光谱相机