simon3322 发表于 2012-8-21 08:27:32

ACM趣题天天练 2 IBM Minus One

本帖最后由 simon3322 于 2012-8-21 10:59 编辑

IBM Minus OneTime 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再提交)


vividly 发表于 2012-8-21 10:01:31

本帖最后由 simon3322 于 2012-8-21 10:37 编辑

已AC。

#include <stdio.h>
#include <string.h>
int main()
{
char a;
int n,i,j;
scanf("%d",&n);
i=0;

while(n--)
{
scanf("%s",a);
for(j=0;a;j++)
{
   if(a=='Z')
    a='A';
   else
    a+=1;
}

i++;
printf("String #%d\n",i);
printf("%s\n\n",a);
}
return 0;
}

jose 发表于 2012-8-21 10:13:53

好水的题啊……

sunhongbo 发表于 2012-8-21 10:41:38

/\   顶啊顶。。。{:6_186:}

fghhslk 发表于 2012-8-21 10:47:56

1楼不上课,真幸福。。。

hslx111 发表于 2012-8-21 11:17:22

这不是趣题这是水题啊...

simon3322 发表于 2012-8-21 11:22:23

hslx111 发表于 2012-8-21 11:17 static/image/common/back.gif
这不是趣题这是水题啊...

基础题....

yel_hb 发表于 2012-8-21 12:42:38

...AC

#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 - 'A') + 1 ) % 26 + 'A');
                        }
                        cout << endl << endl;
                }
        }

        return 0;
}

vividly 发表于 2012-8-21 12:48:18

fghhslk 发表于 2012-8-21 10:47 static/image/common/back.gif
1楼不上课,真幸福。。。

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

fghhslk 发表于 2012-8-21 14:09:49

vividly 发表于 2012-8-21 12:48 static/image/common/back.gif
我可是补专业课筒子。上了半个月课了,今天难得的休息一天。

...............今天你们休息?

extlpf 发表于 2012-8-21 15:30:54

AC水过~~

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();
      }
    }
}

fghhslk 发表于 2012-8-21 17:05:32


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();

        }
}

justcx 发表于 2012-8-21 23:55:47

本帖最后由 justcx 于 2012-8-22 00:28 编辑

#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!='Z')
cout<<char(s+1);
else
cout<<'A';
}
cout<<endl;
cout<<endl;
}
return 0;
}
页: [1]
查看完整版本: ACM趣题天天练 2 IBM Minus One