进制转换题

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;
}
相关推荐
re林檎17 小时前
算法札记——5.15
算法
Cyan_RA917 小时前
SpringMVC 数据格式化处理 详解
java·开发语言·spring·mvc·ssm·springmvc·数据格式化
测试员周周17 小时前
【Appium 系列】第08节-pytest 集成 — conftest.py 中的 fixture 与 hook
开发语言·人工智能·python·功能测试·appium·测试用例·pytest
Hui_AI72017 小时前
电商桌面自动化实战:用RPA实现抖店批量铺货
运维·开发语言·人工智能·自然语言处理·自动化·开源软件·rpa
鱼子星_17 小时前
【数据结构与算法】OJ题目详解(一)-单链表:从易到难的面试OJ题目
c语言·数据结构·算法·链表·面试·职场和发展
人道领域17 小时前
【LeetCode刷题日记】递归与回溯实战 257.二叉树的所有路径——一篇文章彻底搞懂回溯
开发语言·python·算法·leetcode
Gofarlic_OMS17 小时前
Mastercam浮动许可利用率低:软件许可浪费,回收再分配
java·大数据·开发语言·架构·制造
ulias21217 小时前
leetcode热题 - 7
数据结构·算法·leetcode
吃好睡好便好17 小时前
在Matlab中用sphere( )函数绘制球面图
开发语言·前端·javascript·学习·算法·matlab·信息可视化
宏笋17 小时前
C++ 标准库常用函数(sort, transform, accumulate, reduce等)
c++