进制转换题

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;
}
相关推荐
小糖学代码1 天前
LLM系列:1.python入门:15.JSON 数据处理与操作
开发语言·python·json·aigc
handler011 天前
从源码到二进制:深度拆解 Linux 下 C 程序的编译与链接全流程
linux·c语言·开发语言·c++·笔记·学习
小白学大数据1 天前
现代Python爬虫开发范式:基于Asyncio的高可用架构实战
开发语言·爬虫·python·架构
渔舟小调1 天前
P19 | 前端加密通信层 pikachuNetwork.js 完整实现
开发语言·前端·javascript
不爱吃炸鸡柳1 天前
数据结构精讲:树 → 二叉树 → 堆 从入门到实战
开发语言·数据结构
网络安全许木1 天前
自学渗透测试第21天(基础命令复盘与DVWA熟悉)
开发语言·网络安全·渗透测试·php
t***5441 天前
如何在Dev-C++中使用Clang编译器
开发语言·c++
码界筑梦坊1 天前
93-基于Python的中药药材数据可视化分析系统
开发语言·python·信息可视化
Qbw20041 天前
【Linux】进程地址空间
linux·c++