9.随机步法
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 10
#define DIRECTIONS 4
#define ALPHABET 26
int main(void)
{
bool m[N][N] = {false};
char n[N][N];
int i, j;
for (i = 0; i < N; i ++)
{
for (j = 0; j < N; j ++)
{
n[i][j] = '.';
}
}
srand((unsigned) time(NULL));
int direction;
int alpha;
m[0][0] = true;
n[0][0] = 'A';
i = 0;
j = 0;
for (alpha = 0; alpha < ALPHABET - 1; ) //'0'向上,'1'向下,'2'向后,'3'向前
{
direction = rand() % DIRECTIONS;
switch(direction)
{
case 0:
if (i - 1 < 0) //向上出界
{
continue;
}
else if (m[i - 1][j] == true) //向上已有字母
{
if ((m[i + 1][j] == true&&m[i][j - 1] == true&&m[i][j + 1] == true) //四周字母
|| j - 1 < 0&&m[i + 1][j] == true&&m[i][j + 1] == true //向后出界,且向下,向前有字母
|| j - 1 < 0&&i + 1 > 9&&m[i][j + 1] == true //向后,向下出界,且向前有字母
|| j + 1 > 9&&m[i + 1][j] == true&&m[i][j - 1] == true //向前出界,且向下,向后以后字母
|| j + 1 > 9&&i + 1 > 9&&m[i][j - 1] == true) //向前,向下出界,且向后有字母
{
continue;
}
}
else
{
m[i - 1][j] = true; //向上占位
n[i - 1][j] = 'B' + alpha;
alpha ++;
i --;
continue;
}
break;
case 1:
if (i + 1 > 9) //向下出界
{
continue;
}
else if (m[i + 1][j] == true) //向下已有字母
{
if ((m[i - 1][j] == true&&m[i][j - 1] == true&&m[i][j + 1] == true) //四周字母
|| j - 1 < 0&&m[i - 1][j] == true&&m[i][j + 1] == true //向后出界,且向前,向上有字母
|| j - 1 < 0&&i - 1 < 0&&m[i][j + 1] == true //向后,向上出界,且向前有字母
|| j + 1 > 9&&i - 1 < 0&&m[i][j - 1] == true //向前,向上出界,且向后有字母
|| j + 1 > 9&&m[i -1][j] == true&&m[i][j - 1] == true) //向前出界,且向后,向上有字母
{
continue;
}
}
else
{
m[i + 1][j] = true; //向下占位
n[i + 1][j] = 'B' + alpha;
alpha ++;
i ++;
continue;
}
break;
case 2:
if (j - 1 < 0) //向后出界
{
continue;
}
else if (m[i][j - 1] == true) //向后已有字母
{
if ((m[i - 1][j] == true&&m[i + 1][j] == true&&m[i][j + 1] == true) //四周有字母
|| i - 1 < 0&&m[i][j + 1] == true&&m[i + 1][j] == true //向上出界,且向前,向下有字母
|| i - 1 < 0&&j + 1 > 9&&m[i + 1][j] == true //向上,向前出界,且向下有字母
|| i + 1 > 9&&j + 1 > 9&&m[i - 1][j] == true //向下,向前出界,且向上有字母
|| i + 1 > 9&&m[i - 1][j] == true&&m[i][j + 1] == true) //向下出界,且向上,向前有字母
{
continue;
}
}
else
{
m[i][j - 1] = true;
n[i][j - 1] = 'B' + alpha;
alpha ++;
j --;
}
break;
case 3:
if (j + 1 > 9) //向前出界
{
continue;
}
else if (m[i][j + 1] == true) //向前已有字母
{
if ((m[i - 1][j] == true&&m[i + 1][j] == true&&m[i][j - 1] == true) //四周有字母
|| i - 1 < 0&&m[i][j - 1] == true&&m[i + 1][j] == true //向上出界,且向后,向下有字母
|| i - 1 < 0&&j - 1 < 0&&m[i + 1][j] == true //向上,向后出界,且向下有字母
|| i + 1 > 9&&j - 1 < 0&&m[i - 1][j] == true //向下,向后出界,且向上有字母
|| i + 1 > 9&&m[i - 1][j] == true&&m[i][j - 1] == true) //向下出界,且向后,向上有字母
{
continue;
}
}
else
{
m[i][j + 1] = true;
n[i][j + 1] = 'B' + alpha;
alpha ++;
j ++;
}
break;
}
}
for (i = 0; i < N; i ++)
{
for (j = 0; j < N; j ++)
{
printf(" %c ", n[i][j]);
}
printf("\n");
}
return 0;
}
10.找到最近时间
#include <stdio.h>
#include <stdlib.h> //调用 abs函数
int main(void)
{
int hour, minute;
int time;
printf ("Enter a 24-hour time:");
scanf ("%d:%d", &hour, &minute);
time = hour * 60 + minute; //将时间换算成分钟
const int leave[8] = {480, 583, 679, 767, 840, 945, 1140, 1305};
const int arrive[8] = {616, 712, 811, 900, 968, 1075, 1280, 1438};
//将离开与到达时间转换成分钟数,便于比较
int min = 24 * 60; //最大化
int i, j;
for (i = 0; i < 8; i ++)
{
if (abs(leave[i] - time) < min) //abs()函数 取绝对值函数
{
min = abs(leave[i] - time);
j = i;
}
}
if (leave[j] / 60 < 12) //判断时间在上午
{
if (arrive[j] / 60 > 12)
{
printf("CLosest departure time is %d:%.2d a.m.", leave[j] / 60, leave[j] % 60);
printf(", arriving at %d:%.2d p.m.", arrive[j] / 60 -12, arrive[j] % 60);
}
else
{
printf("CLosest departure time is %d:%.2d a.m.", leave[j] / 60, leave[j] % 60);
printf(", arriving at %d:%.2d p.m.", arrive[j] / 60, arrive[j] % 60);
}
}
else //判断时间在下午
{
if (arrive[j] / 60 > 12)
{
printf("CLosest departure time is %d:%.2d p.m.", leave[j] / 60 - 12, leave[j] % 60);
printf(", arriving at %d:%.2d p.m.", arrive[j] / 60 -12, arrive[j] % 60);
}
}
system("pause");
return 0;
}
11.转化电话号码
#include <stdio.h>
int main(void)
{
int book[15] = {0};
int ch, i;
printf ("Enter phone number: ");
while ((ch = getchar()) != '\n') //输入回车停止输入
{
if (ch >= 'A'&&ch <= 'Z') // 字母转换数字, A = 65 ...
{
switch (ch)
{
case 65: case 66: case 67:
book[i] = '2';
break;
case 68: case 69: case 70:
book[i] = '3';
break;
case 71: case 72: case 73:
book[i] = '4';
break;
case 74: case 75: case 76:
book[i] = '5';
break;
case 77: case 78: case 79:
book[i] = '6';
break;
case 81: case 82: case 83: case 80:
book[i] = '7';
break;
case 84: case 85: case 86: case 87:
book[i] = '8';
break;
case 88: case 89: case 90:
book[i] = '9';
break;
}
i++;
continue;
}
book[i] = ch;
i++;
}
int j;
printf("In numeric form : ");
for (j = 0; j < i; j ++)
{
printf("%c", book[j]);
}
system("pause");
return 0;
}
12.字母转化
#include <stdio.h>
#include <ctype.h> //调用toupper函数
int main(void)
{
const int book[26] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3,
1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
//确定每一字母面值
int n;
char ch;
printf("Enter a word: ");
while ((ch = getchar()) != '\n')
{
if (toupper(ch) >= 'A'&&toupper(ch) <= 'Z') //toupper将小写字母转换为大写字母
{
n = n + book[toupper(ch) - 'A']; //转化字母序号
} //若为 ch - 65 则输入小写字母时,输出结果恒为0
else
{
printf("Errow! \n");
break;
}
}
printf("Scrabble value: %d", n);
return 0;
}
13.格式输出名字
#include <stdio.h>
int main(void)
{
char book[20] = {'a'};
char ch1, ch2;
printf ("Enter a first and last name: ");
scanf ("%c", &ch1);
while ((getchar()) != ' ') //输入名字,直到输入空格停止
continue;
int i;
while ((ch2 = getchar()) != '\n') //换行结束输入
{
book[i] = ch2;
i ++;
}
printf("You enered the name: ");
int j;
for (j = 0; j < i; j ++)
{
printf("%c", book[j]);
}
printf(", %c.", ch1);
system("pause");
return 0;
}
14.颠倒语句
#include <stdio.h>
#define N 100
int main(void)
{
char book1[N];
int i;
printf("Enter a sentence: ");
for (i = 0; i < N; i ++)
{
book1[i] = getchar(); //将语句存入数组
if (book1[i] == '.'||book1[i] == '?'||book1[i] == '!') //遇到 '.''!''?'退出循环
{
break;
}
}
int n, j;
n = i; //保存数组长度
int s, o;
char book2[N] = {'a'}; //初始化第二个数组
//第i个输出为符号,i - 1跳过符号向前
for (i = i - 1; i >= 0; i --, j ++)
{
o = i; //记录末尾长度
while (book1[i] != ' '&&i > 0)
{
i --;
}
s = i + 1; //记录空格后的第一个字符出现位置
if (i == 0)
{
s --;
}
for (; s <= o; s ++, j ++)
{
book2[j] = book1[s];
}
book2[j] = ' ';
}
book2[j - 1] = book1[n];
printf("Reversal of sentence: ");
int m;
for (m = 0; m < n + 1; m ++)
{
printf("%c", book2[m]);
}
return 0;
}
15.凯撒加密
#include <stdio.h>
int main(void)
{
char a[80]; //数组a记录原句
int n;
int i;
printf("Enter message to be encrypted: ");
for (i = 0; ; i ++)
{
a[i] = getchar();
if (a[i] == '\n')
{
break;
}
}
printf("Enter shift amout (1~25): ");
scanf("%d", &n);
printf("Encrypted message: ");
int j;
for (j = 0; j < i + 1; j ++)
{
if (a[j] <= 'z'&&a[j] >= 'a')
{
printf("%c", ((a[j] - 'a') + n) % 26 + 'a');
}
else if (a[j] <= 'Z'&&a[j] >= 'A')
{
printf("%c", ((a[j] - 'A') + n) % 26 + 'A');
}
else
{
printf("%c", a[j]);
}
}
system("pause");
return 0;
}
16.测试两个单词是否为变位词
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void)
{
char alpha[20]; //存入字符
int book[26] = {0}; //记录字母出现次数
printf("Enter first world: ");
int i;
for (i = 0; i < 20; i ++) //循环记录
{
alpha[i] = getchar();
if ((alpha[i] < 'A'||alpha[i] > 'z')&&alpha[i] != '\n')
{
printf("Errow!");
return 0;
}
if (alpha[i] == '\n')
{
break;
}
}
int j, n;
for (j = 0; j < 26; j ++)
{
n = toupper(alpha[j]) - 'A';
book[n] ++; //记录字母次数
}
char alpha_[20];
int book_[26] = {0};
printf("Enter second world: ");
int m;
for (m = 0; m < 20; m ++)
{
alpha_[m] = getchar();
if ((alpha_[m] < 'A'||alpha_[m] > 'z')&&alpha_[m] != '\n')
{
printf("Errow!");
return 0;
}
if (alpha_[m] == '\n')
{
break;
}
}
int y, x;
for (x = 0; x < 26; x ++)
{
y = toupper(alpha_[x] - 'A');
book_[y] ++;
}
int z;
int w;
if (m == i)
{
for (z = 0; z < 26; z ++)
{
if (book[z] == book_[z])
{
w = true;
}
else
{
w = false;
}
}
}
if (w == true)
{
printf("The worlds are anagrams \n");
}
if (w == false||m != i)
{
printf("The worlds are not anagrams \n");
}
system("pause");
return 0;
}