进制转换题

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;
}
相关推荐
罗湖老棍子2 小时前
维护序列(信息学奥赛一本通- P1551)(洛谷-P2023)
算法·线段树·区间修改区间查询·多重懒标记
Boop_wu2 小时前
[Java 算法] 归并排序
数据结构·算法·排序算法
淼淼7632 小时前
QT仪表盘
开发语言·qt
wjcroom2 小时前
融释涡旋理论-对狭义相对论和洛伦兹变换的兼容
开发语言·前端
liulilittle2 小时前
SQLITE3 KG-CC
数据库·c++·sqlite
大明者省2 小时前
Python 程序在 Ubuntu 系统的完整部署流程
开发语言·python·ubuntu
咸甜适中2 小时前
rust序列化和反序列化(json、yaml、toml)详解
开发语言·rust·json
今儿敲了吗2 小时前
49| 枚举排列
数据结构·c++·笔记·学习·算法
智算菩萨2 小时前
【Tkinter】14 事件处理机制深度解析:从基础绑定到高级传播,构建交互式绘图笔记应用
开发语言·笔记·python·microsoft·ui·ai编程·tkinter