您的位置:首页 > 编程语言 > Java开发

hdu-5670 Machine(水题附上java代码)

2016-04-22 22:34 169 查看
题目链接:

Machine

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

问题描述
有一个机器,它有 m (2\leq m\leq 30)m(2≤m≤30) 个彩灯和一个按钮。每按下按钮时,最右边的彩灯会发生一次变换。变换为:

1. 如果当前状态为红色,它将变成绿色;

2.如果当前状态为绿色,它将变成蓝色;

3.如果当前状态为蓝色,它将变成红色,并且它左边的彩灯(如果存在)也会发生一次变换。

初始状态下所有的灯都是红色的。
询问按下按钮 n (1\leq n< {2}^{63})n(1≤n<2​63​​) 次以后各个彩灯的颜色。

输入描述
输入包含多组数据. 第一行有一个整数T (1\leq T\leq 15)T(1≤T≤15), 表示测试数据的组数. 对于每组数据:
唯一的一行包含2个整数 m (2\leq m\leq 30)m(2≤m≤30) 和 n (1\leq n< {2}^{63})n(1≤n<2​63​​) 。

输出描述
对于每组数据,输出一个长度为mm的字符串,表示从左到右mm个彩灯的颜色。
R代表红色;G代表绿色;B代表蓝色。

输入样例
2
3 1
2 3

输出样例
RRG
GR

思路:

就像3进制一样;

AC代码:
随便一贴我的第一个Java代码;搞到半夜才配好环境弄好eclipse,然后才知道输入输出数组,看了运行时间真的是好慢


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int N=1e5+6;
ll n;
char s[3]={'R','G','B'},ans[32];
int m;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cin>>m>>n;
for(int i=m;i>0;i--)
{
ans[i]=s[n%3];
n=n/3;
}
for(int i=1;i<=m;i++)
{
printf("%c",ans[i]);
}
printf("\n");
}

return 0;
}


import java.util.Scanner;

public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
int [] a=new int [32];
while(t>0)
{
t--;
//Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
long n=sc.nextLong();
for(int i=m;i>0;i--)
{
a[i]=(int)(n%3L);
n=n/3L;
}
for(int i=1;i<=m;i++)
{
if(a[i]==0)
{
System.out.print("R");
}
else if(a[i]==1)
{
System.out.print("G");
}
else System.out.print("B");
}
System.out.println();

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: