编号是 :1051
涉及以下几个方面的知识点:
1,思路上,略去中间变量的存储,可以使复杂问题简单化,比如这道题,我完全没有必要将存储数字的数组进行交换颠倒过来,只需要从后往前浏览该数组便可以实现功能;
2,gets函数
函数:gets(字符
指针)
头文件:stdio.h(c中),c++不需包含此头文件
原型:
char*gets(char*
buffer
);
例子:
char str1[5];
gets(str1);
gets(s)函数与 scanf("%s",s) 相似,但不完全相同,使用scanf("%s",s) 函数输入 字符串时存在一个问题,就是如果输入了空格会认为字符串结束,空格后的字符将作为下一个输入项处理,但gets()函数将接收输入的整个字符串直到遇到换行为止。
3,这几次的题目用到字符串数组的知识点还比较多,还有一些字符串的操作;思考问题的时候不要着急着打开编辑器输代码,应该现在之上画画,多想想
代码记录:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
//记录摩尔斯密码信息
char aLetter[35][5]={
".-","-...","-.-.","-..",".","..-.","--.",
"....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--..",
"..--",".-.-","---.","----"
};//要立刻想到字符串存储方式,每行有四个字符,共有35行
//记录每个密码所对应的位数
int anum[35]={2,4,4,3,1,4,3,
4,2,4,3,4,2,2,
3,4,4,3,3,1,3,
4,3,4,4,4,
4,4,4,4};
//输入密文
int n,i;
scanf("%d",&n);
// char str[1000];
getchar();
char str[n][1000];
for(i=0;i<n;i++)
gets(str[i]);
int t;
for(t=0;t<n;t++)
{
printf("%d: ",t+1);
int j=0;
int temp=0;//a数组和aLetter数组的下标
int total=0;
int num[100]={0};//存放每个摩尔斯电码个数的排序
int Lnum=0;
char totLetter[1000];//获取输入密文的字母所对应的摩尔斯电码存储到totLetter中
for(i=0;str[t][i]!='\0';i++)
{
if(str[t][i]=='_')
temp=26;
else if(str[t][i]==',')
temp=27;
else if(str[t][i]=='.')
temp=28;
else if(str[t][i]=='?')
temp=29;
else
temp=str[t][i]-'A';
strncpy(&totLetter[j],aLetter[temp],anum[temp]);
j=j+anum[temp];
total=j;
num[i]=anum[temp];
Lnum+=1;
}
int k=0;
int m;
//使用num里面的数组时反方向使用就可以了
char lstr[10];
for(i=Lnum-1;i>=0;i--)
{
for(j=0;j<num[i];j++)
{
lstr[j]=totLetter[k];
k=k+1;
}
//查找对应的电码
lstr[j]='\0';
for(m=0;m<30;m++)
{
if((strcmp(aLetter[m],lstr))==0)
{
if(m==26)
printf("_");
else if(m==27)
printf(",");
else if(m==28)
printf(".");
else if(m==29)
printf("?");
else
printf("%c",m+'A');
}
}
}
printf("\n");
}
return 0;
}