判断下列代码段的大O级别: def b3(n): data = [] for i in range(n): data.insert(len(data), i) return dataA.O(n)B.O(n^2)C.O(n^3)D.O(n*log(n))

判断下列代码段的大O级别: def b3(n): data = [] for i in range(n): data.insert(len(data), i) return data

A.O(n)

B.O(n^2)

C.O(n^3)

D.O(n*log(n))


参考答案和解析
O(n)

相关考题:

针对下面程序段,边界值问题可以定位在___(62)___。1:Rem Create a 10 element integer array2:Rem lnitialize each element to -13:Dim data(10) As Integer4:Dim i As Integer5:For i=1 TO 106:data(i)=-17:Next i8:End(62) A. data(1) B. data(0) C. data(9) D. data(10)

阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。【说明】字符串在程序设计中扮演着重要角色。现需要设计字符串基类string,包含设置字 符串、返回字符串长度及内容等功能。另有一个具有编辑功能的串类edlt_string,派生于string,在其中设置一个光标,使其能支持在光标处的插入、删除操作。【程序】include <iostream.h>include <stdio.h>include <string.h>class string{int length;char *data;public:int get_length() {return length;}char *get_data() {return data;}~string() {delete data;}int set data(int in_length, char *in_data);int set_data(char *data);void print() {cout<<data<<endl;}};class edit_string: public string{int cursor;public:int get_cursor() {return cursor;}void move_cursor(int dis) {cursor=dis;}int add_data(string *new_data);void delete_data(int num);};int string::set_data(int in_length,char *in_data){length=in_length;if(!data)delete data;(1)strcpy(data,in_data);return length;}int string::set data(char *in_data){(2)if(!data)delete data;(1)strcpy(data,in_data);return length;}int edit_string::add_data(string *new_data){int n,k,m;char *cp,*pt;n=new_data->get_length();pt=new_data->get_data();cp=this->get_data();m=this->get_length();char *news=new char[n+m+1];for(int i=0; i<cursor; i++)news[i]=cp[i];k=i;for(int j=0; j<n; i++,j++)news[i]=pt[j];cursor=i;for(j=k; j<m; j++,i++)(3)news[i]='\0';(4)delete news;return cursor;}void edit string::delete_data( int num){int m;char *cp;cp=this->get_data();m=this->get_length();for(int i=cursor; i<m; i++)(5)cp[i]='\0';}

阅读以下说明C++代码,将应填入(n)处的字句写在对应栏内。[说明]以下程序的功能是实现堆栈的一些基本操作。堆栈类stack共有三个成员函数:empty判断堆栈是否为空;push进行人栈操作;pop进行出栈操作。[C++程序]include "stdafx. h"include <iostream, h>eonst int maxsize = 6;class stack {float data[ maxsize];int top;public:stuck(void);~ stack(void);bool empty(void);void push(float a);float pop(void);};stack: :stack(void){ top =0;cout < < "stack initialized." < < endl;}stack:: ~stack(void) {cout < <" stack destoryed." < < endl;bool stack:: empty (void) {return (1);void stack: :push(float a)if(top= =maxsize) {cout < < "Stack is full!" < < endl;return;data[top] =a;(2);}float stack:: pop (void){ if((3)){cout< < "Stack is undcrflow !" < < endl;return 0;(4);return (5);}void main( ){ stack s;coat < < "now push the data:";for(inti=l;i =maxsize;i+ +) {cout< <i< <" ";s. push(i);}coat < < endl;cout< < "now pop the data:";for(i = 1 ;i < = maxsize ;i + + )cout< <s. pop()< <" ";}

下面是一个简单的使用RAWSOCKET实现的ping程序,填入(n)处。/*simple ping program*/struct sockaddr_in saddr;int rawsock;unsigned short in_cksum(unsigned short*addr, int len){ int sum=0;unsigned short res=0;while(1en>1){sum+=*addr++; len-=2;}if(len=1){*((unsigned char *)(res))=*((unsigned char *)addr); sum+=res;}sum=(sum>>16)+(sam 0xffff);sum+=(sum>>16); res=~sum;return res;}void ping(int signo){int len;int i;static unsigned short seq=0;char buff[8192];struct timeval tv;struet icmp*icmph=(struct icmp * )buff;long*data=(long*)icmph→icmp_data;bzero(buff, 8192);gettimeofday(tv, NULL);icmph→icmp_type=ICMP_ECHO;icmph→icmp_code=0;icmph→icmp_cksum=0;icmph→icmp_id=0;icmph→icmp_seq=0;icmph→icmp_id=getpid()0xffff;icmph→icmp_seq=seq++;data[0]=tv.tv_sec;data[1]=tv.tv_usec;for(i=8; i< ; i++)icmph→icmp_data[i]=(unsigned char)i;icmph→icmp_cksum=in_cksum((unsigned short *)buff, ? 72);len; sendto(rawsock, buff, 72, 0, saddr, sizeof(saddr));alarm(1);}void sigint(int signo){ printf("CATCH SIGINT !!! \n");close(rawsock);exit(0);}void dumppkt(char*buf, int len){ struct ip*iph=(struct ip*)buf;int i=iph→ip_h1*4;struct icmp*icmph=(struct icmp*)buf[i];long*data=(long*)iemph→icmp_data;struct timeval tv;gettimeofday(tv, NULL);if(icmph→icmp_type! =ICMP_ECHOREPLY)return;if(icmph→icmp_id! =(getpid()0xffff))return;printf("From %s:ttl=% d seq=% d time=%.2f ms\n",inet_ntoa(iph→ip_src),iph→ip_ttl?,icmph→icmp_seq,(tv.tv_see-data[0])*1000.0+(tv.tv_usec-data[0])/1000.0);}int main(int argc, char*argv[]){ int len;stuct timeval now;char recvbuff[8192];if(1){printf("%s aaa.bbb.ccc.ddd\n", argv[0]);exit(1);}rawsock=soeket(AF_INET, (2), IPPROTO_ICMP);if(rawsock<0) {perror("soeket");exit(1);}bzero ( saddr, sizeof(saddr));saddr.sin_family=(3);if( inet_aton( argv[1], saddr.sin_addr) <0) {printf("invalid IP address: %s\n", argv[1]);exit(1);}signal(SICALRM, ping);signal(SICINT, sigint);alarm(1);while (1){len=read (4), recvbuff, 8192);if( len<0 errno=EINTR)continue;else it( len<0)perror("read");else if( len>0)dumppkt(recvbuff, len);}close (5);exit(0);}

阅读分析本题程序段后回答问题:(1)程序实现了什么功能?(2)写出程序的输出结果 阅读分析本题程序段后回答问题:(1)程序实现了什么功能?(3分)(2)写出程序的输出结果;(4分)(3)写出算法的时间复杂度。(3分)#include stdio.h#define N 7typedef int datatype;void main(void){ int 1,j,t;datatype data[N]={1,2,3, 4,5,6, 7}; /*处理的数据*/i=0;j=N-1;while (ij){ t=data[i];data[i++ ]=data[j];data[j--]=t;}printf(”运行结果为: \n);for(i= =0;iN-1;i++)printf(%d; ,data[i]);}

( 12 )有如下类定义和变量定义:class A{publie:A () {data=0;}~A () {}int GetData ( ) coast { return data;}void SetData ( int n ) {data=n;}private:int data;};ccnst A a;A b;下列函数调用中错误的是A ) a .GetData ( ) ;B ) a .SetData ( 10 ) ;C ) b .GetData ( ) ;D ) b .SetData ( 10 ) ;

下面为C语言程序,边界值问题可以定位在(45)。 int data(3), int i, for(i=1, i<=3, i++)data(i)=100A.data(O)B.data(1)C.data(2)D.data(3)

试题(45)下面为C语言程序,边界值问题可以定位在(45)。int data(3),int i,for (i=1,i=3,i++)data(i)= 100(45)A. data(0)B. data(1)C. data(2)D. data(3)

下列类的定义中,有( ) 处语法错误。 class Base { public: Base(){} Base(int i) { data=i; } private: int data; }; class Derive: public Base { public: Derive(): Base(O) { } Derive(int x) { d=x; } void setvalue(int i) { data=i; } private: d; };A.1B.2C.3D.4

假如整数数列中的数不重复,并存放在数组中。下列给定的程序中,函数fun()的功能是:删除数列中值为X的元素。 N中存放的是数列中元素的个数。请改正程序中的错误,使它能够得出正确的结果。注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。试题程序:include<stdio.h>define N 20fun (int *a,int n,int x){int p=0,i;a[n]=x;while (x!=a[p])p=p+1;if(p==n) return -1;else{for (i=p;i<n;i++)/*************found**************/a[i+1]=a[i];return n-1;}}main(){int w[N]={-3,0,1,5,7,99,10,15,30,90},x,n,i;n=10;printf("The original data :\n");for (i=0;i<n;i++) printf("%5d",w[i]);printf("\nInput x (to delete ): ");scanf("%d",x);printf("Delete : %d\n",x);n=fun(w,n,x);if (n==-1) printf("***No be found!***\n\n");else{printf("The data after deleted:\n");for (i=0;i<n;i++) printf("%5d",w[i]);printf("\n\n");}}

有以下程序:include main(){FILE*fp;int i,k,n; fp=fopen("data.dar","w+");for(i=1;i 有以下程序: #include <stdio.h> main() { FILE *fp; int i,k,n; fp=fopen("data.dar","w+"); for(i=1;i<6;i++) { fprintf(fp,"%d ",i); if(i%3==0) fprintf(fp,"\n"); } rewind(fp); fscanf(fp,"%d%d",k,n); printf("%d%d\n",k,n); fclose(fp); } 程序运行后的输出结果是( )。A.0 0B.123 45C.1 4D.1 2

阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。[说明]下面程序实现十进制向其它进制的转换。[C++程序]include"ioStream.h"include"math.h"includetypedef struct node {int data;node*next;}Node;Class Transform.{DUDlic:void Trans(int d,int i); //d为数字;i为进制void print();private:Node*top;};void Transform.:Trans(int d,int i){int m,n=0;Node*P;while(d>0){(1);d=d/i;p=new Node;if(!n){p->data=m;(2);(3);n++;}else{p->data=m;(4);(5);}}}void Transform.:print(){Node*P;while(top!=NULL){p=top;if(p->data>9)cout<<data+55;elsecout<<data;top=p->next;delete p;}}

有以下程序:include main( ){FILE * fp; int i,k,n;fp = fopen( "data. dat" ,"w +" ) 有以下程序:#include <stdlo.h>main( ){ FILE * fp; int i,k,n; fp = fopen( "data. dat" ,"w +" ) for(i = 1 ;i<6;i ++ ) { fprintf(fp."% d",i); if(i%3 ==0)fprintf(fp," \n"); } rewind(fp); fscanf(fp." % d% d" ,k, n) ;printf(" % d%d \n" ,k,n); fclose(fp);A.0 0B.123 45C.1 4D.1

有以下程序: include main() {FILE *fp; int i,k,n; fp=fopen("data 有以下程序: #include <stdio.h> main() {FILE *fp; int i,k,n; fp=fopen("data.dat","w+"); for(i=1;i<6;i++) {fprintf(fp,"%d ",i); if(i%3==0) fprintf(fp,"\n"); } rewind(fp); fscanf(fp,"%d%d",k,n); printf("%d %d\n",k,n); fclose(fp); } 程序运行后的输出结果是 ______。A.0 0B.123 45C.1 4D.1 2

A)(仕兰微面试题目)#i ncludevoid testf(int*p){*p+=1;}main(){int *n,m[2];n=m;m[0]=1;m[1]=8;testf(n);printf("Data v alue is %d ",*n);}------------------------------B)#i ncludevoid testf(int**p){*p+=1;}main(){int *n,m[2];n=m;m[0]=1;m[1]=8;testf(n);printf(Data v alue is %d",*n);}下面的结果是程序A还是程序B的?Data v alue is 8那么另一段程序的结果是什么?

下而程序实现十进制向其他进制的转换。[C++程序]include"ioStream.h"include"math.h"include <conio.h>typedef struct node{int data;node *next;}Node;class Transform{public:void Trans(int d,int i); //d为数字;i为进制void print();private:Node *top;};void Transform.:Trans(int d,int i){int m,n=0;Node *P;while(d>0){(1) ;d=d/i;p=new Node;if(!n){P->data=m;(2) j(3) ;n++;}else{p->data=m;(4) ;(5) ;}}}void Transform.:print(){Node *P;while(top!=NULL){p=top;if(P->data>9)cout<<data+55:elsecout<<data;top=p->next;delete P;}}

return o; O是代表什么呢?如果不返回O 会怎么样? friendostreamoperator(ostreamVectori=0;iv.size;i++){ov.data[i]'';}returno;}

针对下面程序段,边界值问题可以定位在(62)1:Rem Crege a 10 element integer array2:Rem Initialize each element to -13:Dim data(10) As Integer4:Dim i As Integer5:For i=1 TO 106:data(i)=-17:Next i8:EndA.data(1)B.data(0)C.data(9)D.data(10)

有如下类定义和变量定义: class A{ public: A( ){data=0;} ~A( ){ } int GetData( )const{return data;} void SetData(int n){data=n;} private: int data; }; const A a; A b; 下列函数调用中错误的是A.a.GetData( );B. a.SetData(10);C.b.GetData( );D.b.SetData(10);

下列类的定义中,有( )处语法错误。 class Base { public: Base(){} Base(int i) { data=i; } private: int data; }; class Derive : public Base { public: Derive() : Base(O) {} Derive (int x) { d=x; } void setvalue(int i) { data=i; } private: int d; };A.1B.2C.3D.4

若有以下程序: #include〈iostream〉 using namespace std; int main() { int data[4],i,j,temp; for (i=O; i4; i++) cindata[i]; for (i=1; i4; i++) { j = i-1; temp = data[i]; while (data [j ] tempj =0) { data[j+1] = data[j]; j--; } data[j+1] = temp; } for(i=O;i4;i++) cout〈〈data[i]〈〈" "; cout〈〈end1; return 0; }A.2843B.2348C.8243D.8432

第二题 阅读以下说明和代码,填补代码中的空缺,将解答填入答题纸的对应栏内。【说明】对n个元素进行简单选择排序的基本方法是:第一趟从第1个元素开始,在n个元素中选出最小者,将其交换至第一个位置,第二趟从第2个元素开始,在剩下的n-1个元素中选出最小者,将其交换至第二个位置,依此类推,第i趟从n-i+1个元素中选出最小元素,将其交换至第i个位置,通过n-1趟选择最终得到非递减排序的有序序列。 问题:2.1 【代码】#include void selectSort(int data[ ],int n)//对 data[0]~data[n-1]中的n个整数按非递减有序的方式进行排列{ int i,j,k; int temp; for(i=0;i for(k=i,j=i+1;(1);(2)) //k表示data[i]~data[n-1]中最小元素的下标 if(data[j] if(k!=i) { //将本趟找出的最小元素与data[i]交换 temp=data[i]; (4) ;data[k]=temp; } }}int main(){ int arr[ ]={79,85,93,65,44,70,100,57}; int i,m; m=sizeof(arr)/sizeof(int); //计算数组元素的个数,用m表示 (5); //调用selectSort对数组arr进行非递减排序 for((6);i printf(“%d\t”,arr[i]); printf(“\n”); return 0;}

阅渎以下说明和C代码,回答问题,将解答写入答题纸的对应栏内。 【说明】函数bubbleSort(int arr [ ] int n, int (*compare)(int, int)的功能是根据调用时传递的比较函数 compare 对数組arr的前n个元素进行排序。 【C代码】#define swap(a,b){a=a^b;b=a^b;a=a^b //交换a与b 的值int less(int x, int y){ return((xy)?1: 0);} void bubble Sort(int arr[ ], int n, int (*compare)(int, int)){ int i,j; int swapped= 1; for( i= 0; swapped; 1++) { swapped =0; for(j=0; j【问题1】设有如下数组定义:int data1[ ]={4,2.6.3,1};int data2[ ]={4,2,6.3,1}int datas3[ ]={4,2,6.3,1}请分别给出下面的函数调用执行后,数组 data1、data2和 data3 各自的元素序列。(1)bubble Sort(data1, 5, less);(2)bubbleSort(data2, 5, larger)(3)bubbleSort(data3, 3, larger)

下面为C语言程序,边界值问题可以定位在( )。int data(3),int i,for(i=1,i<=3,i++)data(i)=100A.data(O)B.data(1)C.data(2)D.data(3)

写出算法的功能。intfun(sqstring*s,sqstring*t,intstart){inti=start-1,j=0;while(ilenjlen)if(s-data[i]==t-data[j]){i++;j++;}else{i=i-j+1;j=0;}if(j=t-len)returni-t-len+1;elsereturn-1;}

函数实现串的模式匹配算法,请在空格处将算法补充完整。intindex_bf(sqstring*s,sqstring*t,intstart){inti=start-1,j=0;while(ilenjlen)if(s-data[i]==t-data[j]){i++;j++;}else{i=();j=0;}if(j=t-len)return();elsereturn-1;}}/*listDelete*/

填空题函数实现串的模式匹配算法,请在空格处将算法补充完整。intindex_bf(sqstring*s,sqstring*t,intstart){inti=start-1,j=0;while(ilenjlen)if(s-data[i]==t-data[j]){i++;j++;}else{i=();j=0;}if(j=t-len)return();elsereturn-1;}}/*listDelete*/

问答题写出算法的功能。intfun(sqstring*s,sqstring*t,intstart){inti=start-1,j=0;while(ilenjlen)if(s-data[i]==t-data[j]){i++;j++;}else{i=i-j+1;j=0;}if(j=t-len)returni-t-len+1;elsereturn-1;}