蛋蛋的問題(二)

蛋蛋的問題(二)

時間限制:100 ms  |  內存限制:65535 KB
難度:2
描述

教新生的時候,也是學長們最興奮的時候,但是隻侷限於講課的那位帥帥的學長,在最後一排坐着聽課的學長確實無比的苦悶啊,爲了讓小學弟知道,你們的二進制轉換學的很爛,學長爲你們創造了這道很難的題,沒看錯,是很難。。。

二進制的轉換很簡單,下面是搜搜裏面的介紹:

這個題可不是求二進制這麼簡單的,說過了,是很難的。。

下面說問題:對於給出的N個數,求出他們的8進制數或者16進制數,每組數據最前面有標示符,是‘X’,那麼就要轉換成16進制的書,如果是‘0’,呢麼就要轉換成8進制,是不是很難啊,還有更難得:注意(輸出長度規定爲4),可別輸錯了歐。。。。

輸入
有多組數據。
每組數據第一個是一個字符,'0'或者'X',然後是一個整數N(0<N<=100),接下來是N個整數,每個整數都爲小於100的非負整數。
輸出
對應每組輸入,輸出所要求的數據,輸出數據爲單獨一行
樣例輸入
0 5 2 3 4 5 6
X 5 2 3 4 5 6
0 2 99 98
X 2 99 98
X 2 10 11
樣例輸出
   2    3    4    5    6 
   2    3    4    5    6 
 143  142 
  63   62 
   A   B
來源
菜鳥自創
//849744	張燚	蛋蛋的問題(二)	Accepted	 8	 232	C/C++	05-03 17:26:23
#include<stdio.h>
int main()
{
	char c;
	int n;
	int a[110];
	while(scanf("%c %d",&c,&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		getchar();
		if(c=='0')
		{
			for(int i=0;i<n;i++)
			{
				printf("%4o",a[i]);
			}
			printf("\n");
		}
		else 
		{
			for(int i=0;i<n;i++)
			{
				printf("%4X",a[i]);
			}
			printf("\n");
		}
	}
	return 0;
}


//849724	張燚	蛋蛋的問題(二)	TimeLimitExceeded	 --	 --	java	05-03 17:20:08
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		while(input.hasNext()){
			String p=input.next();
			int n=input.nextInt();
			if(p.charAt(0)=='X')
				for(int i=0;i<n;i++){
					int a=input.nextInt();
					System.out.print(String.format("%4s", Integer.toHexString(a).toUpperCase()));
				}
			else{
				for(int i=0;i<n;i++){
					int a=input.nextInt();
					System.out.print(String.format("%4s", Integer.toOctalString(a)));
				}
			}
			System.out.println();
		}
	}
}

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main{//超時
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		while(input.hasNext()){
			String p=input.next();
			int n=input.nextInt();
			if(p.charAt(0)=='X')
				for(int i=0;i<n;i++){
					int a=input.nextInt();
					System.out.print(String.format("%4X", a));
				}
			else{
				for(int i=0;i<n;i++){
					int a=input.nextInt();
					System.out.print(String.format("%4o", a));
				}
			}
			System.out.println();
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main{//超時
	public static void main(String[] args) throws Exception{
		Scanner input=new Scanner(System.in);
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		String s=null;
		while((s=bf.readLine())!=null){
			String p[]=s.split(" ");
			int n=p.length;
			if(p[0].charAt(0)=='X')
				for(int i=2;i<n;i++){
					System.out.print(String.format("%4X", Integer.parseInt(p[i])));
				}
			else{
				for(int i=2;i<n;i++){
					System.out.print(String.format("%4o", Integer.parseInt(p[i])));
				}
			}
			System.out.println();
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章