1027 Colors in Mars --java实现

题目:

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.

Input Specification:

Each input file contains one test case which occupies a line containing the three decimal color values.

Output Specification:

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 its left.

Sample Input:

15 43 71

Sample Output:

#123456

 题意: 将三个数分别转化为13进制再输出,转化后的进制数不超过两位,如果不足两位加上前导0

代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("#");
        for (int i = 0; i < 3; i++) {
            int x = sc.nextInt();
            String s = Integer.toString(x, 13).toUpperCase();
            System.out.print(s.length() > 1 ? s : 0 + s);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图的着色问题可以使用回溯算法来解决。回溯算法是一种试探性的算法,通过不断地尝试所有可能的解决方案,找到符合条件的最优解。 在图的着色问题中,我们要为每个节点选择一种颜色进行着色,使得相邻节点的颜色不相同。具体的回溯算法实现如下: 1. 首先,选择一个未着色的节点作为当前节点。 2. 枚举当前节点可以使用的颜色,如果该颜色和相邻节点的颜色不同,则将当前节点着色为该颜色,并将下一个未着色的节点作为当前节点。 3. 如果所有的节点都已经着色,则找到了一组可行解。 4. 如果当前节点无法找到可用的颜色,则回溯到上一个节点,重新选择颜色。 Java 代码实现如下: ``` public class GraphColoring { private int numOfVertex; // 节点数 private int numOfColor; // 颜色数 private int[] colors; // 节点的颜色 private int[][] graph; // 图 public GraphColoring(int[][] graph, int numOfColor) { this.numOfVertex = graph.length; this.numOfColor = numOfColor; this.colors = new int[numOfVertex]; this.graph = graph; } public void solve() { if(backtrack(0)) { System.out.println("Solution exists!"); printSolution(); } else { System.out.println("Solution does not exist!"); } } private boolean backtrack(int vertex) { if(vertex == numOfVertex) { // 所有节点都已着色 return true; } for(int color=1; color<=numOfColor; color++) { if(isValid(vertex, color)) { colors[vertex] = color; // 着色 if(backtrack(vertex+1)) { // 着色下一个节点 return true; } colors[vertex] = 0; // 回溯 } } return false; } private boolean isValid(int vertex, int color) { for(int i=0; i<numOfVertex; i++) { if(graph[vertex][i] == 1 && colors[i] == color) { // 相邻节点颜色相同 return false; } } return true; } private void printSolution() { for(int i=0; i<numOfVertex; i++) { System.out.println("Vertex " + (i+1) + " is colored with " + colors[i]); } } } ``` 其中,`graph` 表示图的邻接矩阵,`numOfColor` 表示颜色的种数。调用 `solve()` 方法即可求解该问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值