首页 > 试题广场 >

Colors in Mars (20)

[编程题]Colors in Mars (20)
  • 热度指数:2670 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

输入描述:
Each input file contains one test case which occupies a line containing the three decimal color values.


输出描述:
For each test case you should output the Mars RGB value in the following format: first output "#", then followed by a 6-digit number where all the English characters must be upper-cased.  If a single color is only 1-digit long, you must print a "0" to the left.
示例1

输入

15 43 71

输出

#123456
这种题难度仅次于helloworld,一定要拿分
#include<iostream>
using namespace std;

int main() {
	string s="0123456789ABC";
	int r,g,b;
	cin>>r>>g>>b;
	cout<<"#"<<s[r/13]<<s[r%13]<<s[g/13]<<s[g%13]<<s[b/13]<<s[b%13];
    return 0;
}


发表于 2020-01-31 21:06:59 回复(1)
package go.jacob.day1026;

import java.util.Scanner;

/**
 * 简单题,但要注意,输出的时候每一个颜色固定两位
 * @author Administrator
 *
 */
public class Demo1 {
    static char[] c = new char[] { 'A', 'B', 'C' };

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] color = new String[3];
        for (int i = 0; i < 3; i++) {
            color[i] = solve(sc.nextInt());
        }
        System.out.println("#" + color[0] + color[1] + color[2]);
        sc.close();
    }

    // 十进制转十三进制
    private static String solve(int num) {
        StringBuilder res = new StringBuilder();
        if (num == 0)
            return "00";
        while (num != 0) {
            int t = num % 13;
            if (t < 10)
                res.append(t);
            else
                res.append(c[t - 10]);
            num /= 13;
        }
        String r = res.reverse().toString();
        if (r.length() == 1)
            return "0" + r;
        else
            return r;
    }
}

发表于 2017-10-26 10:38:31 回复(0)
import java.util.Scanner;

public class Main {
	public static String convert(int n){
		String c[]={"0","1","2","3","4","5","6","7","8","9","A","B","C"};
		String s="";
		s+=c[n/13];
		s+=c[n%13];
		return s;
	}
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		System.out.print("#");
		for(int i=0;i<3;i++){
			System.out.print(convert(in.nextInt()));
		}
		
	}

}


发表于 2016-10-23 15:37:34 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int r=sc.nextInt();
        int g=sc.nextInt();
        int b=sc.nextInt();
        String marsR="";
        if(r<13)
            marsR+="0";
        while(r/13>0){
            marsR+=switchNumber(r/13);
            r-=r/13*13;
        }
        marsR+=switchNumber(r%13);
        String marsG="";
        if(g<13)
            marsG+="0";
        while(g/13>0){
            marsG+=switchNumber(g/13);
            g-=g/13*13;
        }
        marsG+=switchNumber(g%13);
        String marsB="";
        if(b<13)
            marsB+="0";
        while(b/13>0){
            marsB+=switchNumber(b/13);
            b-=b/13*13;
        }
        marsB+=switchNumber(b%13);
        System.out.println("#"+marsR+""+marsG+""+marsB);
    }

    public static String switchNumber(int n){
        String result="";
        if(n<10){
            result+=n;
        }else{
            if(n==10)
                result+="A";
            if(n==11)
                result+="B";
            if(n==12)
                result+="C";
        }
        return result;
    }
}

发表于 2018-10-06 21:16:31 回复(0)

#include<iostream>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int m = 13;
string str = "0123456789ABC";

int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    printf("#%c%c%c%c%c%c\n", str[a/m],str[a%m],str[b/m],str[b%m], str[c/m], str[c%m]);

    system("pause");
    return 0;
}
编辑于 2018-09-23 20:58:04 回复(0)
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            String str = scanner.nextLine();
            String[] arr = str.split(" ");
            System.out.print("#");
            String s = "";
            for (int i = 0; i < arr.length; i++) {
                String ss = Integer.toString(Integer.parseInt(arr[i]),13);
                if (Integer.parseInt(arr[i])<13) {
                    ss = "0"+ss;
                }
                s += ss;
            }
            System.out.println(s.toUpperCase());
        }
    }

}

发表于 2018-08-15 17:14:28 回复(0)
// 抄袭了一个itoa的函数 10进制转任意进制转换 
#include <iostream>
#include <iomanip>
using namespace std;

char* itoa(int num, char *str, int radix)
{
    /*索引表*/
    char index[]="0123456789ABCDEF";
    unsigned unum;/*中间变量*/
    int i=0,j,k;
    /*确定unum的值*/
    if(radix==10&&num<0)/*十进制负数*/
    {
        unum=(unsigned)-num;
        str[i++]='-';
    }
    else unum=(unsigned)num;/*其他情况*/
    /*转换*/
    do{
        str[i++]=index[unum%(unsigned)radix];
        unum/=radix;
    }while(unum);
    str[i]='\0';
    /*逆序*/
    if(str[0]=='-')k=1;/*十进制负数*/
    else k=0;
    char temp;
    for(j=k;j<=(i-1)/2;j++)
    {
        temp=str[j];
        str[j]=str[i-1+k-j];
        str[i-1+k-j]=temp;
    }
    return str;
}


int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    cout << "#";
    char str[100];
    itoa(a, str, 13);
    cout<< setfill('0') << setw(2) << str;
    itoa(b, str, 13);
    cout<< setfill('0') << setw(2) << str;
    itoa(c, str, 13);
    cout<< setfill('0') << setw(2) << str << endl;
}

发表于 2018-07-02 14:28:34 回复(0)
转成13进制
#include<stdio.h>

#include<iostream>

#include<string>

#include<stack>

using namespace std;


void transfer(int input)

{

    stack<int> result;

    while(1)

    {

        int left= input%13;

        input=input/13;

        result.push(left);

        //cout<<left<<endl;

        if(input==0)

            break;

        

    }


    if(result.size()==1)

        cout<<"0";

    

    while(!result.empty())

    {

        int top = result.top();

        if(top==10)

            cout<<"A";

        else if(top==11)

            cout<<"B";

        else if(top==12)

            cout<<"C";

        else

            cout<<top;

        result.pop();

    }


}


int main()

{

    int A,B,C;

    scanf("%d %d %d",&A,&B,&C);

    cout<<"#";

    transfer(A);

    transfer(B);

    transfer(C);

    cout<<endl;

    return 0;

}

发表于 2018-03-01 20:56:07 回复(0)
import java.util.Scanner;

public class Main {
    final static String MarsNum = "0123456789ABC";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int Red = sc.nextInt();
        int Green = sc.nextInt();
        int Blue = sc.nextInt();
        System.out.println("#"+toMarsNum(Red)+toMarsNum(Green)+toMarsNum(Blue));
    }

    static String toMarsNum(int num){
        return ""+MarsNum.charAt(num/13)+MarsNum.charAt(num%13);
    }
}

发表于 2022-11-14 15:58:00 回复(0)
#include<bits/stdc++.h>
using namespace std;

int main(){
	string str="0123456789ABC";
	int a,b,c;
	cin>>a>>b>>c;
	cout<<'#'<<str[a/13%13]<<str[a%13]<<str[b/13%13]<<str[b%13]<<str[c/13%13]<<str[c%13]<<endl;
}

发表于 2022-11-05 16:03:29 回复(0)
进制转换问题
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>

using namespace std;

string trans(int dec) {
    int m = dec;
    string mars = "";
    while(m / 13 > 0) {
        int rem = m % 13;
        if (rem < 10) {
            mars = to_string(rem) + mars;
        } else {
            mars = string(1, 'A' + rem - 10) + mars;
        }
        m = m / 13;
    }
    if (m < 10) {
        mars = to_string(m) + mars;
    } else {
        mars = string(1, 'A' + m - 10) + mars;
    }
    return mars;
}

int main() {
    int r, g, b;
    cin >> r >> g >> b;
    cout << "#" << setw(2) << setfill('0') << trans(r)
        << setw(2) << setfill('0') << trans(g)
        << setw(2) << setfill('0') << trans(b);
    return 0;
}

发表于 2020-09-21 10:43:50 回复(0)
public class Advanced1006 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String r = Integer.toString(scanner.nextInt(), 13).toUpperCase();
        String g = Integer.toString(scanner.nextInt(), 13).toUpperCase();
        String b = Integer.toString(scanner.nextInt(), 13).toUpperCase();

        scanner.close();

        r = r.length() < 2 ? "0" + r : r;
        g = g.length() < 2 ? "0" + g : g;
        b = b.length() < 2 ? "0" + b : b;

        System.out.println("#" + r + g + b);
    }
}

发表于 2020-02-05 23:25:46 回复(0)
#include <cstdio>

int main() {
	int c[3];
	char c13[6] = {'0', '0', '0', '0', '0', '0'};
	scanf("%d%d%d", &c[0], &c[1], &c[2]);

	for(int i = 0; i < 3; i++) {
		int temp, num = 0;
		do {
			temp = c[i] % 13;

			if(temp >= 10)  c13[i * 2 + num++] = 'A' + (temp - 10);
			else  c13[i * 2 + num++] = '0' + temp;

			c[i] /= 13;
		} while(c[i] != 0);
	}

	printf("#");
	for(int i = 0; i < 3; i++) {
		printf("%c%c", c13[i * 2 + 1], c13[i * 2]);
	}

	return 0;
}

发表于 2019-08-31 08:47:20 回复(0)
MemoryC
import java.util.Scanner; public class Main {     static char[]nums={'0','1','2','3','4','5','6','7','8','9','A','B','C'};     public static void main(String[] args) {         Scanner scanner=new Scanner(System.in);         String[] line=scanner.nextLine().trim().split(" ");         StringBuilder builder=new StringBuilder();         builder.append("#");         for(String dec :line){             int val=Integer.parseInt(dec);             builder.append(nums[val/13]);             builder.append(nums[val%13]);         }         System.out.println(builder.toString());     } }
以上
发表于 2019-05-05 14:22:45 回复(0)
//可能这题太水了......牛客显示速度排在第一
#include <iostream>
#include <string>
using namespace std;

//1027 Colors in Mars

char ot[13]={'0','1','2','3','4','5','6','7','8','9','A','B','C'};

string  convert(int n ,int radix=13){
    string str;
    str+=ot[n/radix];
    str+=ot[n%radix];
    
    return str;
    
}

int main() {
    int n,i=3;
    cout<<"#";
    while(i--){
        cin>>n;    
        cout<<convert(n);
    }
    return 0;    
}
发表于 2019-02-02 13:52:56 回复(0)
简单的进制转换
def baseN(num,d):
    return ((num==0) and '0') or (baseN(num/d,d).lstrip('0')+'0123456789ABC'[num%d])
red,green,blue=map(int,raw_input().split())
print '#{}{}{}'.format(baseN(red,13).rjust(2,'0'),baseN(green,13).rjust(2,'0'),baseN(blue,13).rjust(2,'0'))
发表于 2019-01-16 11:29:01 回复(0)
#include<bits/stdc++.h>  using namespace std; void trans(int n){  int a = n/13; int b = n%13;
    (0 <= a && a <= 9) ? printf("%d", a) : printf("%c", ('A'+a-10));
    (0 <= b && b <= 9) ? printf("%d", b) : printf("%c", ('A'+b-10));
} int main() { int r, g, b;
    scanf("%d %d %d", &r, &g, &b);
    printf("#");trans(r); trans(g);trans(b); return 0;
}

发表于 2018-11-06 09:35:58 回复(0)
#include"string"
#include"vector"
#include"iostream"
using std::string;
using std::vector;
using std::cin;
using std::cout;
vector<string> radix13 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C" };
string temp = "#";
void MarsColor();

int main()
{  MarsColor();  return 0;
}

void MarsColor()
{  int R, G, B;  cin >> R >> G >> B;  temp += radix13[R / 13] + radix13[R % 13] +
        radix13[G / 13] + radix13[G % 13] + 
        radix13[B / 13] + radix13[B % 13];  cout << temp;
}

编辑于 2018-06-15 18:06:16 回复(0)
#include<iostream>
#include<cstdio>
using namespace std;
string input;
char to_itoc(int n)
{     if(n <=9 )         return n+'0';     else      {         return     n-10+'A';     }
}
int main()
{     cout <<'#';     for(int i = 0; i < 3;i++)     {         int c;         cin >> c;              printf("%c%c",to_itoc(c/13),to_itoc(c%13));     }
}

编辑于 2018-04-28 19:04:56 回复(0)
import java.util.Scanner;

public class 十进制转十三进制 {
public static String transform(int num) {
String remainder ;
if(num%13 == 10) {
remainder = "A";
}else if(num%13 == 11){
remainder = "B";
}else if(num%13 == 12){
remainder = "C";
}else {
remainder = String.valueOf(num%13);
}
return remainder;
}
public static String result(int num) {
String remainder = transform(num);
String decade = transform(num/13);
return decade+remainder;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int red = input.nextInt();
int green = input.nextInt();
int blue = input.nextInt();
input.close();
System.out.println("#"+result(red)+result(green)+result(blue));
}
}

发表于 2017-07-28 16:18:15 回复(0)