10 条题解

  • 10
    @ 2022-8-17 16:08:03
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        long long a[100000], n, x, cnt = 0;
        cin >> n >> x;
        if (n == 0)
        {
            cout << n;
        }
        while (n > 0)
        {
            a[++cnt] = n % x;
            n /= x;
        }
        for (int i = cnt;i >= 1;i--)
        {
            if (x != 16)
            {
                cout << a[i];
            }
            else
            {
                if (a[i] == 10)
                {
                    cout << 'A';
                }
                else if (a[i] == 11)
                {
                    cout << 'B';
                }
                else if (a[i] == 12)
                {
                    cout << 'C';
                }
                else if (a[i] == 13)
                {
                    cout << 'D';
                }
                else if (a[i] == 14)
                {
                    cout << 'E';
                }
                else if (a[i] == 15)
                {
                    cout << 'F';
                }
                else
                {
                    cout << a[i];
                }
            }
        }
        return 0;
    }
    
    • 5
      @ 2023-1-19 21:15:57
      一个赞拿走
      #include <bits/stdc++.h>
      using namespace std;
      int main(){
          long long n,a[13],i=0,d,s=0;
          cin>>n>>d;
          if(n==0) cout<<0;
          else{
              if(d==16){
                  char a[1000],num[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
                  while (n>0){
                      a[s]=num[n%16];
                      n/=16;
                      s++;
                  }
                  for(int j=s-1;j>=0;j--) cout<<a[j];
              }
              else{
                  while(n>0){
                      a[++i]=n%d;
                      n/=d;
                  }
                  for(int j=i;j>=1;j--) cout<<a[j];
              }
          }
          return 0;
      }
      
      • 2
        @ 2023-7-29 12:49:09

        思路:我编了三个函数来判断进制; 二八进制就不用说了,十六进制则用switch...case语句或if..else if..else语句 再在主函数中用switch判断用哪个函数 上代码!(已AC)

        #include <bits/stdc++.h>
        using namespace std;
        long long n;//超过了int的范围,得用long long
        int d;//可以用int
        char c[50];//因为含有字母,用char开50就行了;还得是全局变量方便一点
        void bin(int x){
            int cnt=0;//方便倒序输出
            if(!x){
            	cout<<0<<endl;
            	return;
        	}//如果x=0,while不执行得先判断
            while(x)c[++cnt]='0'+x%2,x/=2;//++cnt先cnt+1,再调用cnt
        	for(int i=cnt;i>=1;i--)cout<<c[i];
            return;//好习惯
        }
        void oct(int x){
            int cnt=0;
            if(!x){
                cout<<0<<endl;
                return;
            }//如果x=0,while不执行得先判断
            while(x)c[++cnt]='0'+x%8,x/=8;
            for(int i=cnt;i>=1;i--)cout<<c[i];
            return;
        }
        void hexl(int x){
            int cnt=0,f;
            if(!x){
                cout<<0<<endl;
                return;
            }
            while(x){
                f=x%16,x/=16;
                switch(f){
                    case 10:c[++cnt]='A';break;
                    case 11:c[++cnt]='B';break;
                    case 12:c[++cnt]='C';break;
                    case 13:c[++cnt]='D';break;
                    case 14:c[++cnt]='E';break;
                    case 15:c[++cnt]='F';break;
                    default:c[++cnt]='0'+f;break;
                }//判断
            }
            for(int i=cnt;i>=1;i--)cout<<c[i];
            return;
        }
        int main(){
            cin>>n>>d;
            switch(d){
                case 2:bin(n);break;
                case 8:oct(n);break;
                case 16:hex(n);break;
            }
            return 0;
        }
        
        • 2

          其实这道题根本不用打表,十六进制转换只需要把他强制转换成字符型就行了。 具体代码见下:

          if(a[i]<10)cout<<a[i];
          else cout<<(char)(a[i]-10+'A');
          
          • 2
            @ 2022-8-17 16:08:38

            数位拆分那里改成%x,/x就完事了,如果是16进制特殊操作一下

            • 1
              @ 2024-4-14 20:43:15

              进制转换器

              #include<bits/stdc++.h>
              using namespace std;
              signed main(void)
              {
                  long long a[100000], n, x, cnt = 0;
                  //输入n,x
                  //如果n==0,可以直接输出
                  while (n > 0)
                  {
                      a[++cnt]=n % x,n/=x;
                  }
                  /*核心代码*/for(int i=cnt;i>=1;i--)
                  {
                      if (x!=16)
                      {
                          printf("%i",a[i]);
                      }
                      else
                      {
                          switch(a[i])  /*switch语句,不会的可以用if else if...*/
                          {
                              case 10:printf("A"); break;//方便,脱离花括
                              case 11:printf("B"); break;
                              case 12:printf("C"); break;
                              case 13:printf("D"); break;
                              case 14:printf("E"); break;
                              case 15:printf("F"); break;
                              default printf("%i",a[i]);
                          }
                      } 
                  }/*核心代码*/
                  return 0;
              }
              

              image

              给个免费的赞再走!👍

              • 1
                @ 2024-2-18 11:18:38

                数据结构---栈

                特点:后进先出

                定义方法:

                stack<type>s;
                

                函数:

                s.push(value);//在栈首插入指定元素 value 
                s.pop();//删除栈首元素 
                s.top();//获取栈首元素 
                s.size();//获取栈的长度 
                s.empty();//判断栈是否为空,空返回true
                

                AC code

                #include <bits/stdc++.h>
                using namespace std;
                int n,d;
                stack<char>s;
                string t="0123456789ABCDEF";
                int main(){
                	cin>>n>>d;
                	if (!n){
                		cout<<0;
                		return 0;
                	}
                	while(n!=0){
                		s.push(t[n%d]);
                		n=n/d;
                	}
                	while(s.size()){
                		cout<<s.top();
                		s.pop();
                	}
                	return 0;
                }
                
                • 1
                  @ 2023-2-23 19:59:11

                  三种进制转换的代码拼到一块就OK了,有一丢丢长哈 代码:

                  #include<bits/stdc++.h> 
                  using namespace std;
                  void er(long long n)//十进制转换二进制函数
                  {
                      int z[35];
                      int a=35;
                      while (n>0)
                      {
                          z[--a]=n%2;
                          n/=2;
                      }
                      for (int i=a;i<35;i++)
                      {
                          cout << z[i];
                      }
                  }
                  void ba(long long n)//十进制转换八进制函数
                  {
                      int z[15];
                      int a=15;
                      while (n>0)
                      {
                          z[--a]=n%8;
                          n/=8;
                      }
                      for (int i=a;i<15;i++)
                      {
                          cout << z[i];
                      }
                  }
                  void shiliu(long long n)//十进制转换十六进制函数
                  {
                      int temp,count=0,j;
                      int a[100];
                      cin>>n;
                      while (n!=0)
                      {
                          temp=n;
                          n=n/16;
                          a[count]=temp%16;
                          count++;
                      }
                      for (j=count-1;j>=0;j--)
                      {
                          if (a[j]>9&&a[j]<16)
                          {
                              cout << char(a[j]-10+'A');
                          }
                          else
                          {
                              cout << a[j];
                          }
                      }
                  }
                  int main()//如果否则那么判断使用哪种函数
                  {
                      long long a;
                      int b;
                      cin>>a>>b;
                      if (a==0)
                      {
                          cout << 0;
                      }
                      else if (b==2)
                      {
                          er(a);
                      }
                      else if (b==8)
                      {
                          ba(a);
                      }
                      else if (b==16)
                      {
                          shiliu(a);
                      }
                      return 0;
                  }
                  
                  • 1
                    @ 2022-9-9 23:48:24

                    😄 关于判16的简化:

                    char b[6] = {'A', 'B', 'C', 'D', 'E', 'F'}; 
                    for (int i = cnt - 1; i >= 0; i--)
                    	{
                    		if (a[i] < 10)
                    		{
                    			cout << a[i];	
                    		}
                    		else
                    		{
                    			cout << b[a[i] - 10];
                    		}
                    	}
                    
                    • 1
                      @ 2022-8-25 10:09:21

                      首先输入n和d。 然后判断n是否为0:

                      if(n==0)
                      	{
                      		cout<<0;
                      		return 0;
                      	}
                      

                      如果没有这段就不能AC。

                      然后是二进制转换:

                      if (d == 2) {
                      		int i, j = 0;
                      		int a[1000];
                      		i = n;
                      		while (i) {
                      			a[j] = i % 2;//i对2取余,结果存入数组
                      			i /= 2;
                      			j++;         //计算位数
                      		}
                      		for (i = j - 1; i >= 0; i--)
                      			cout << a[i];//输出
                      	}
                      

                      再然后是八进制,直接用oct函数:

                      if (d == 8)cout << oct << n;
                      

                      最后是十六进制,用printf函数即可:

                      if (d == 16) {
                      				printf("%X", n);
                      			}
                      

                      最后return 0。

                    • 1

                    信息

                    ID
                    412
                    时间
                    1000ms
                    内存
                    16MiB
                    难度
                    4
                    标签
                    递交数
                    454
                    已通过
                    193
                    上传者