算法练习
来源:互联网 发布:淘宝主店怎么绑定分店 编辑:程序博客网 时间:2024/05/01 22:15
1回文词,镜像词判断。(输入舞空白字符和0,可用scanf)
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 "; //镜像词字符串。mirror charconst char* msg[] = {"not a palindrome","a regular palindrome","a mirrored string","a mirrored palindrome"};//输出数组 output arraychar r( char ch) { if(isalpha(ch)) return rev[ch - 'A']; //judge if alpha,return it's value in the letter table. return rev[ch - '0' + 25]; //judge and return the number.}int main(){ char s[60]; while( scanf("%s",s) == 1){ int len = strlen(s); int p = 1, m = 1; for(int i = 0; i < (len+1)/2; i++){ if(s[i] != s[len-1-i]) p = 0; if(r(s[i])!=s[len-1-i]) m = 0; } printf("%s -- is %s.\n\n",s,msg[m*2+p]); } return 0;}
WERTYU,如果手在键盘上放错位置,想输入qwertyu时则输入了wertyui。修改此错误。
#include <stdio.h>#include <stdlib.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;}
蛇形填字。
#include <stdio.h>#include <stdlib.h>#include <string.h>#define maxn 20int a[maxn][maxn]; // named a two dimension array respectively represent the line and rowint main(){ int n,x,y,tot = 0; scanf("%d",&n); memset(a,0,sizeof(a)); tot = a[x=0][y=n-1] = 1; while(tot < n*n) { while((x+1) < n && !a[x+1][y]) a[++x][y] = ++tot; //if line exist,and the next line is 0. the number next line add 1. while((y-1>=0) && !a[x][y-1]) a[x][--y] = ++tot; //go back to add 1 on the same line in different column. while((x-1>=0)&& !a[x-1][y]) a[--x][y] = ++tot; //then add 1 on the same column in different line; while(y+1 < n && !a[x][y+1]) a[x][++y] = ++tot; //add 1 on the same line in different column from left to right. } for(x = 0; x < n; x++) { for(y = 0 ; y < n; y++) printf("%3d",a[x][y]); //%3d,printf an int number take 3 blocks printf("\n"); //printf number in column needn't change line. } return 0;}
输出结果:
input:9
25 26 27 28 29 30 31 32 1
24 51 52 53 54 55 56 33 2
23 50 69 70 71 72 57 34 3
22 49 68 79 80 73 58 35 4
21 48 67 78 81 74 59 36 5
20 47 66 77 76 75 60 37 6
19 46 65 64 63 62 61 38 7
18 45 44 43 42 41 40 39 8
17 16 15 14 13 12 11 10 9
开灯问题,n盏灯,第一人把所有灯打开。第2人按下所有2的倍数,第三人按下所有三的倍数。开关再按开的灯会关闭,一共k个人,问最后亮几盏灯。
//n盏灯,第一个人把所有灯打开,第2个人按下所有编号为2的倍数开关,第三个人按3的倍数开关。共k个人,k<=n<=1000.问最后几盏灯亮着#include <stdio.h>#include <stdlib.h>#include <string.h>#define maxn 1010int a[maxn]; //using a[n] represent the light situation.int main(){ int n,k,first = 1; memset(a,0,sizeof(a)); scanf("%d%d",&n,&k); for(int i = 1;i<=k; i++) for(int j = 1; j<=n;j++) if(j%i==0) a[j]=!a[j]; //i means number of people. if j%i==0,then change the light situation. for(int i = 1; i<=n; i++) if(a[i]) { if(first) first = 0;else printf(" ");printf("%d",i); } return 0;}
0 0
- 【练习】经典算法练习
- 算法练习
- 算法练习
- 算法练习
- 算法练习
- 【算法练习】
- 算法练习
- 算法练习
- 算法练习
- 算法练习
- 算法练习
- 算法练习
- 算法练习
- 算法练习
- 算法练习
- 算法练习
- 算法练习
- 算法练习
- Android中的Service:Binder,Messenger,AIDL
- 算术转化和整型提升的奥秘
- SlidingMenu侧滑
- 字符串分割c++
- C#中DllImport用法汇总
- 算法练习
- springmvc的头文件格式
- 基于socket的可发送表情简单即时通讯
- Spring Boot application.propertis配置文件的相关通用属性
- sqlMapConfig.xml的头文件格式
- PREV-34矩阵翻硬币 (高精度开方,相乘)
- 比赛排名
- 子串计算
- log4j的文件配置