猴哥面试
时间: 1ms 内存:128M
描述:
话说猴哥经过半年的刻苦学习,顺利通过了考研初试,正在为明天的复试发愁,神秘的小乌龟给猴哥带来了一张纸条,猴哥打开一看,上写“K公司因程序员马小虎在编写员工管理程序时,将存储身份证号的字符数组的大小定义为18,造成个人信息显示异常,请修改程序,使信息显示正常.”猴哥一拍脑袋说,“这还不简单,把文件中所有定义身份证号的数组的大小从18改成19,不就行了”。小乌龟在旁边冷冷的说,“要是以后身份证号19位不够了,你还要再来一遍吧”,猴哥白了小乌龟一眼,想了一想,也是哈,于是确定用宏定义实现,然后定义了一个参数宏#define IDLEN 19 ,决定将文件中所有定义身份证号的数组大小从18改成IDLEN。请协助猴哥完成。
输入:
第一行为程序中定义身份证号的字符数组名字.
从第二行开始为需要改写的程序代码.
输出:
按要求改写的程序代码
示例输入:
ID
#include
int main()
{
char ID[18],name[18];
int age;
while(~scanf("%s%s%d",ID,name,&age))
{
printf("%s\t%s\t%d\n",ID,name,age);
}
return 0;
}
示例输出:
#define IDLEN 19
#include
int main()
{
char ID[IDLEN],name[18];
int age;
while(~scanf("%s%s%d",ID,name,&age))
{
printf("%s\t%s\t%d\n",ID,name,age);
}
return 0;
}
提示:
参考答案(内存最优[1684]):
#include <iostream>
#include <string.h>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <algorithm>
#include <stdarg.h>
#include <map>
using namespace std;
void getNext(char* str,int next[])
{
int k=-1,l=strlen(str);
for(int i=0;i<l;i++)
{
if(k==-1||str[i]==str[k])
{
k++;
next[i] = k;
}
else
{
k = next[k];
}
}
}
int getPos(const char *str1,const char *str2,int data[],int next[])
{
int l1 = strlen(str1),l2 = strlen(str2);
int j=0;
int k=0;
for(int i=0;i<l1;i++)
{
if(str1[i]==str2[j])
{
j++;
if(j==l2)
{
data[k++]=i-l2+1;
j=0;
}
}
else
{
j = next[j];
}
}
}
int main()
{
int data[100]={0};
int next[100]={0};
char str[20]={0},str2[20]={0},art[1000]={0},temp;
int i=0,j=0;
cin >> str;
strcpy(str2,str);
getchar();
str[strlen(str)]='[';
str[strlen(str)]='1';
str[strlen(str)]='8';
str[strlen(str)]=']';
while(temp=getchar(),temp!=-1)
{
art[i++]=temp;
}
getPos(art,str,data,next);
cout << "#define IDLEN 19" << endl;
for(i=0;i<strlen(art);)
{
if(i!=data[j])
{
cout<<art[i];
i++;
}
else
{
j++;
cout << str2 << "[IDLEN]";
i+=strlen(str2)+4;
}
}
return 0;
}
参考答案(时间最优[0]):
#include <iostream>
#include <string.h>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <algorithm>
#include <stdarg.h>
#include <map>
using namespace std;
void getNext(char* str,int next[])
{
int k=-1,l=strlen(str);
for(int i=0;i<l;i++)
{
if(k==-1||str[i]==str[k])
{
k++;
next[i] = k;
}
else
{
k = next[k];
}
}
}
int getPos(const char *str1,const char *str2,int data[],int next[])
{
int l1 = strlen(str1),l2 = strlen(str2);
int j=0;
int k=0;
for(int i=0;i<l1;i++)
{
if(str1[i]==str2[j])
{
j++;
if(j==l2)
{
data[k++]=i-l2+1;
j=0;
}
}
else
{
j = next[j];
}
}
}
int main()
{
int data[100]={0};
int next[100]={0};
char str[20]={0},str2[20]={0},art[1000]={0},temp;
int i=0,j=0;
cin >> str;
strcpy(str2,str);
getchar();
str[strlen(str)]='[';
str[strlen(str)]='1';
str[strlen(str)]='8';
str[strlen(str)]=']';
while(temp=getchar(),temp!=-1)
{
art[i++]=temp;
}
getPos(art,str,data,next);
cout << "#define IDLEN 19" << endl;
for(i=0;i<strlen(art);)
{
if(i!=data[j])
{
cout<<art[i];
i++;
}
else
{
j++;
cout << str2 << "[IDLEN]";
i+=strlen(str2)+4;
}
}
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。