[技术| 编程·课件·Linux] ACM趣题天天练 2 IBM Minus One

simon3322 · 发布于 2012-08-21 08:27 · 1596 次阅读
997
本帖最后由 simon3322 于 2012-8-21 10:59 编辑

IBM Minus One

Time Limit: 2 Seconds      Memory Limit: 65536 KB
You may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only two men are awake, and the ship is controlled by the intelligent computer HAL. But during the flight HAL is acting more and more strangely, and even starts to kill the crew on board. We don't tell you how the story ends, in case you want to read the book for yourself :-)
After the movie was released and became very popular, there was some discussion as to what the name 'HAL' actually meant. Some thought that it might be an abbreviation for 'Heuristic ALgorithm'. But the most popular explanation is the following: if you replace every letter in the word HAL by its successor in the alphabet, you get ... IBM.
Perhaps there are even more acronyms related in this strange way! You are to write a program that may help to find this out.

Input
The input starts with the integer n on a line by itself - this is the number of strings to follow. The following n lines each contain one string of at most 50 upper-case letters.

Output
For each string in the input, first output the number of the string, as shown in the sample output. The print the string start is derived from the input string by replacing every time by the following letter in the alphabet, and replacing 'Z' by 'A'.
Print a blank line after each test case.

Sample Input
2
HAL
SWERC

Sample Output
String #1
IBM
String #2
TXFSD

源自:http://acm.zju.edu.cn/ 1240题

ACM趣题天天练,热烈欢迎论坛各位程序达人果断AC。每天前十名AC并提交自己代码都有红包相送!更有可能认识更多热爱编程喜欢编程的朋友!期待你的代码!

注:如果有提交代码的请在编辑中使用高级—>添加代码段(先AC再提交)


共收到 12 条回复
vividly · #2 · 2012-8-21 10:01:31  回复 支持 反对
本帖最后由 simon3322 于 2012-8-21 10:37 编辑

已AC。

[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
char a[50];
int n,i,j;
scanf("%d",&n);
i=0;

while(n--)
{
  scanf("%s",a);
  for(j=0;a[j];j++)
  {
   if(a[j]=='Z')
    a[j]='A';
   else 
    a[j]+=1;
  }
  
  i++;
  printf("String #%d\n",i);
  printf("%s\n\n",a);
}
return 0;
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

评分

参与人数 3学分 +26 收起 理由
紫凝雪儿 + 8 好给力。。
sunhongbo + 6 加分加分,热烈欢迎积极参与。。。
simon3322 + 12

查看全部评分

jose · #3 · 2012-8-21 10:13:53  回复 支持 反对
好水的题啊……
sunhongbo · #4 · 2012-8-21 10:41:38  回复 支持 反对
/\   顶啊顶。。。
fghhslk · #5 · 2012-8-21 10:47:56  回复 支持 反对
1楼不上课,真幸福。。。

点评

我可是补专业课筒子。上了半个月课了,今天难得的休息一天。  详情 回复 发表于 2012-8-21 12:48
hslx111 · #6 · 2012-8-21 11:17:22  回复 支持 反对
这不是趣题这是水题啊...

点评

基础题....  详情 回复 发表于 2012-8-21 11:22
simon3322 · #7 · 2012-8-21 11:22:23  回复 支持 反对
hslx111 发表于 2012-8-21 11:17
这不是趣题这是水题啊...

基础题....
yel_hb · #8 · 2012-8-21 12:42:38  回复 支持 反对

...AC

[C++] 纯文本查看 复制代码
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int n,k,i,len;
	string str;

	while(cin >> n)
	{
		k = 0;
		while(k++ < n)
		{
			cin >> str;
			len = str.length();
			cout << "String #" << k << endl;
			for(i = 0; i < len; ++i)
			{
				cout << char(((str[i] - 'A') + 1 ) % 26 + 'A');
			}
			cout << endl << endl;
		}
	}

	return 0;
}

评分

参与人数 1学分 +9 收起 理由
simon3322 + 9

查看全部评分

vividly · #9 · 2012-8-21 12:48:18  回复 支持 反对
fghhslk 发表于 2012-8-21 10:47
1楼不上课,真幸福。。。

我可是补专业课筒子。上了半个月课了,今天难得的休息一天。

点评

...............今天你们休息?  详情 回复 发表于 2012-8-21 14:09
fghhslk · #10 · 2012-8-21 14:09:49  回复 支持 反对
vividly 发表于 2012-8-21 12:48
我可是补专业课筒子。上了半个月课了,今天难得的休息一天。

...............今天你们休息?
extlpf · #11 · 2012-8-21 15:30:54  回复 支持 反对
AC水过~~

[Java] 纯文本查看 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = Integer.parseInt(sc.nextLine());
        String str; char ch;
        for(int i = 0; i < num; i++) {
            System.out.println("String #" + (i + 1));
            str = sc.nextLine();
            for(int j = 0; j < str.length(); j++) {
                ch = str.charAt(j);
                if(ch == 'Z') {
                    ch = 'A';
                } else {
                    ch = (char) (ch + 1);
                }
                System.out.print(ch);
            }
            System.out.println();
            System.out.println();
        }
    }
}

评分

参与人数 1学分 +9 收起 理由
simon3322 + 9

查看全部评分

fghhslk · #12 · 2012-8-21 17:05:32  回复 支持 反对
[Java] 纯文本查看 复制代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		List<StringBuffer> strBfrs = new ArrayList<StringBuffer>();
		Scanner sc = new Scanner(System.in);
		int n;
		int num = 0;
		n = Integer.parseInt(sc.nextLine());
		while (n-- > 0) 
			strBfrs.add(new StringBuffer(sc.nextLine()));			
		for (StringBuffer strBfr : strBfrs) {
			for (int i = 0; i < strBfr.length(); i++) 
				strBfr.setCharAt(i, ((char)((strBfr.charAt(i) - 'A' + 1) % 26 + 'A')));
			System.out.println("String #" + ++num);
			System.out.println(strBfr);
			System.out.println();
		}
		sc.close();

	}
}

评分

参与人数 1学分 +6 收起 理由
simon3322 + 6

查看全部评分

justcx · #13 · 2012-8-21 23:55:47  回复 支持 反对
本帖最后由 justcx 于 2012-8-22 00:28 编辑

[C++] 纯文本查看 复制代码
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
string s;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>s;
cout<<"String #"<<i+1<<endl;
for(int j=0;j<s.size();j++)
{
if(s[j]!='Z')
cout<<char(s[j]+1);
else
cout<<'A';
}
cout<<endl;
cout<<endl;
}
return 0;
}

评分

参与人数 1学分 +3 收起 理由
simon3322 + 3

查看全部评分

回帖
B Color Image Link Quote Code Smilies
Command + Enter
快速回复 返回顶部 返回列表