《算法竞赛入门经典》- 感觉有点意义的基础题(更新中)

1.

输入两个整数a和b,及另一正整数c,计算a/b,结果精确到小数点后c位

输入
输入包含多组数据,每组数据包含三个正整数a,b,c,其中a,b≤106,c≤5,中间用空格隔开。
结束标记为a=b=c=0。

输出
对于每组输入,对应于一行输出,首先输出“Case N: ”,表示第N组输出(从1开始,不用输出引号),接下来输出a/b的小数形式,采用“四舍五入法”,精确到小数点后c位。

样例输入
1 6 4
0 0 0

样例输出
Case 1: 0.1667

代码如下:

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int a,b,c;
 5     int N=1;
 6 while(~scanf("%d%d%d",&a,&b,&c)&&a&&b&&c)
 7 {
 8     printf("Case %d: %.*lf
",N,c,1.0*a/b);
 9     N++;
10  }
11     return 0;
12 }

 2.

UVA10082

A common typing error is to place the hands on the keyboard one row to the right of the correct position. So “Q” is typed as “W” and “J” is typed as “K” and so on. You are to decode a message typed in this manner.

Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except Q, A, Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab, BackSp, Control,etc.] are not represented in the input. You are to replace each letter or punction symbol by the one immediately to its left on the QWERTY keyboard shown above. Spaces in the input should be echoed in the output.

Sample Input

O S, GOMR YPFSU/

Output for Sample Input

I AM FINE TODAY.

#include<stdio.h>
char s[] = "`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./";
int main() {
int i, c;
while((c = getchar()) != EOF) {
for (i=1; s[i] && s[i]!=c; i++); //找错位之后的字符在常量表中的位置
if (s[i]) putchar(s[i-1]); //如果找到,则输出它的前一个字符
else putchar(c);
}
return 0;
}

如果是一个一个if就非常麻烦了orz

3.UVA401 Palindromes 

输入一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字0。所谓
回文串,就是反转以后和原串相同,如abba和madam。所有镜像串,就是左右镜像之后和原串相同,如2S和3AIAE。注意,并不是每个字符在镜像之后都能得到一个合法字符。在本题中,每个字符的镜像如图3-3所示(空白项表示该字符镜像后不能得到一个合法字符)。

输入的每行包含一个字符串(保证只有上述字符。不含空白字符),判断它是否为回文
串和镜像串(共4种组合)。每组数据之后输出一个空行。
样例输入:
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
样例输出:
NOTAPALINDROME -- is not a palindrome.

ISAPALINILAPASI -- is a regular palindrome.

2A3MEAS -- is a mirrored string.
ATOYOTA -- is a mirrored palindrome.

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<ctype.h>
 4 const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";
//下面一句是我不会的东西orz
5 const char* msg[] = {"not a palindrome", "a regular palindrome", "a mirrored string", char r(char ch) { 6 if(isalpha(ch)) return rev[ch - 'A']; 7 return rev[ch - '0' + 25]; 8 } 9 int main() { 10 char s[30]; 11 while(scanf("%s", s) == 1) { 12 int len = strlen(s); 13 int p = 1, m = 1; 14 for(int i = 0; i < (len+1)/2; i++) { 15 if(s[i] != s[len-1-i]) p = 0; //不是回文串 16 if(r(s[i]) != s[len-1-i]) m = 0; //不是镜像串 17 } 18 printf("%s -- is %s. ", s, msg[m*2+p]); 19 } 20 return 0; 21 }

//百度的说法.

char * msg;
msg="1234567890";
这样定义就相当于msg指针指向的是一个const变量,也就是说msg = “1234567890”所在的内存区域是不可写的。所以msg[5] = 'A'没有写内存的权限,所以就会报错。char *msg = "1234567890"这样定义编译器编译的时候就会把msg当做是const变量放在不可写的内存区域。

原文地址:https://www.cnblogs.com/greenaway07/p/10497733.html