本帖最后由 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再提交) |
[技术| 编程·课件·Linux] ACM趣题天天练 2 IBM Minus One
simon3322
· 发布于 2012-08-21 08:27
· 1631 次阅读
转载文章时务必注明原作者及原始链接,并注明「发表于 软院网 RuanYuan.Net 」,并不得对作品进行修改。
本帖最后由 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; } |
评分
...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; } |
评分
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(); } } } |
评分
[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(); } } |
评分
本帖最后由 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; } |