from:
http://blog.csdn.net/fansnaf/archive/2006/11/03/1364447.aspx本人很弱,這幾個(gè)題也搞不定,特來(lái)求救:
1)讀文件file1.txt的內(nèi)容(例如):
12
34
56
輸出到file2.txt:
56
34
12
(逆序)
2)輸出和為一個(gè)給定整數(shù)的所有組合
例如n=5
5=1+4;5=2+3(相加的數(shù)不能重復(fù))
則輸出
1,4;2,3。
望高手賜教!!
第一題,注意可增長(zhǎng)數(shù)組的應(yīng)用.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int MAX = 10;
int *a = (int *)malloc(MAX * sizeof(int));
int *b;
FILE *fp1;
FILE *fp2;
fp1 = fopen("a.txt","r");
if(fp1 == NULL)
{printf("error1");
exit(-1);
}
fp2 = fopen("b.txt","w");
if(fp2 == NULL)
{printf("error2");
exit(-1);
}
int i = 0;
int j = 0;
while(fscanf(fp1,"%d",&a[i]) != EOF)
{
i++;
j++;
if(i >= MAX)
{
MAX = 2 * MAX;
b = (int*)realloc(a,MAX * sizeof(int));
if(b == NULL)
{
printf("error3");
exit(-1);
}
a = b;
}
}
for(;--j >= 0;)
fprintf(fp2,"%d\n",a[j]);
fclose(fp1);
fclose(fp2);
return 0;
}
第二題.
#include <stdio.h>
int main(void)
{
unsigned long int i,j,k;
printf("please input the number\n");
scanf("%d",&i);
if( i % 2 == 0)
j = i / 2;
else
j = i / 2 + 1;
printf("The result is \n");
for(k = 0; k < j; k++)
printf("%d = %d + %d\n",i,k,i - k);
return 0;
}
#include <stdio.h>
void main()
{
unsigned long int a,i=1;
scanf("%d",&a);
if(a%2==0)
{
for(i=1;i<a/2;i++)
printf("%d",a,a-i);
}
else
for(i=1;i<=a/2;i++)
printf(" %d, %d",i,a-i);
}
兄弟,這樣的題目若是做不出來(lái)實(shí)在是有些不應(yīng)該, 給你一個(gè)遞規(guī)反向輸出字符串的例子,可謂是反序的經(jīng)典例程.
void inverse(char *p)
{
if( *p = = '\0' )
return;
inverse( p+1 );
printf( "%c", *p );
}
int main(int argc, char *argv[])
{
inverse("abc\0");
return 0;
}
借簽了樓上的“遞規(guī)反向輸出”
#include <stdio.h>
void test(FILE *fread, FILE *fwrite)
{
char buf[1024] = {0};
if (!fgets(buf, sizeof(buf), fread))
return;
test( fread, fwrite );
fputs(buf, fwrite);
}
int main(int argc, char *argv[])
{
FILE *fr = NULL;
FILE *fw = NULL;
fr = fopen("data", "rb");
fw = fopen("dataout", "wb");
test(fr, fw);
fclose(fr);
fclose(fw);
return 0;
}
在對(duì)齊為4的情況下
struct BBB
{
long num;
char *name;
short int data;
char ha;
short ba[5];
}*p;
p=0x1000000;
p+0x200=____;
(Ulong)p+0x200=____;
(char*)p+0x200=____;
希望各位達(dá)人給出答案和原因,謝謝拉
解答:假設(shè)在32位CPU上,
sizeof(long) = 4 bytes
sizeof(char *) = 4 bytes
sizeof(short int) = sizeof(short) = 2 bytes
sizeof(char) = 1 bytes
由于是4字節(jié)對(duì)齊,
sizeof(struct BBB) = sizeof(*p)
= 4 + 4 + 2 + 1 + 1/*補(bǔ)齊*/ + 2*5 + 2/*補(bǔ)齊*/ = 24 bytes (經(jīng)Dev-C++驗(yàn)證)
p=0x1000000;
p+0x200=____;
= 0x1000000 + 0x200*24
(Ulong)p+0x200=____;
= 0x1000000 + 0x200
(char*)p+0x200=____;
= 0x1000000 + 0x200*4
你可以參考一下指針運(yùn)算的細(xì)節(jié)
寫(xiě)
一段程序,找出數(shù)組中第k大小的數(shù),輸出數(shù)所在的位置。例如{2,4,3,4,7}中,第一大的數(shù)是7,位置在4。第二大、第三大的數(shù)都是4,位置在1、
3隨便輸出哪一個(gè)均可。函數(shù)接口為:int find_orderk(const int* narry,const int n,const int
k)
要求算法復(fù)雜度不能是O(n^2)
謝謝!
可以先用快速排序進(jìn)行排序,其中用另外一個(gè)進(jìn)行地址查找
代碼如下,在VC++6.0運(yùn)行通過(guò)。給分吧^-^
//快速排序
#include<iostream>
usingnamespacestd;
intPartition (int*L,intlow,int high)
{
inttemp = L[low];
intpt = L[low];
while (low < high)
{
while (low < high && L[high] >= pt)
--high;
L[low] = L[high];
while (low < high && L[low] <= pt)
++low;
L[low] = temp;
}
L[low] = temp;
returnlow;
}
voidQSort (int*L,intlow,int high)
{
if (low < high)
{
intpl = Partition (L,low,high);
QSort (L,low,pl - 1);
QSort (L,pl + 1,high);
}
}
intmain ()
{
intnarry[100],addr[100];
intsum = 1,t;
cout << "Input number:" << endl;
cin >> t;
while (t != -1)
{
narry[sum] = t;
addr[sum - 1] = t;
sum++;
cin >> t;
}
sum -= 1;
QSort (narry,1,sum);
for (int i = 1; i <= sum;i++)
cout << narry[i] << '\t';
cout << endl;
intk;
cout << "Please input place you want:" << endl;
cin >> k;
intaa = 1;
intkk = 0;
for (;;)
{
if (aa == k)
break;
if (narry[kk] != narry[kk + 1])
{
aa += 1;
kk++;
}
}
cout << "The NO." << k << "number is:" << narry[sum - kk] << endl;
cout << "And it's place is:" ;
for (i = 0;i < sum;i++)
{
if (addr[i] == narry[sum - kk])
cout << i << '\t';
}
return0;
}
1、找錯(cuò)
Void test1()
{
char string[10];
char* str1="0123456789";
strcpy(string, str1);// 溢出,應(yīng)該包括一個(gè)存放'\0'的字符string[11]
}
Void test2()
{
char string[10], str1[10];
for(I=0; I<10;I++)
{
str1[i] ='a';
}
strcpy(string, str1);// I,i沒(méi)有聲明。
}
Void test3(char* str1)
{
char string[10];
if(strlen(str1)<=10)// 改成<10,字符溢出,將strlen改為sizeof也可以
{
strcpy(string, str1);
}
}
2.
void g(int**);
int main()
{
int line[10],i;
int *p=line; //p是地址的地址
for (i=0;i<10;i++)
{
*p=i;
g(&p);//數(shù)組對(duì)應(yīng)的值加1
}
for(i=0;i<10;i++)
printf("%d\n",line[i]);
return 0;
}
void g(int**p)
{
(**p)++;
(*p)++;// 無(wú)效
}
輸出:
1
2
3
4
5
6
7
8
9
10
3. 寫(xiě)出程序運(yùn)行結(jié)果
int sum(int a)
{
auto int c=0;
static int b=3;
c+=1;
b+=2;
return(a+b+c);
}
void main()
{
int I;
int a=2;
for(I=0;I<5;I++)
{
printf("%d,", sum(a));
}
}
// static會(huì)保存上次結(jié)果,記住這一點(diǎn),剩下的自己寫(xiě)
輸出:8,10,12,14,16,
4.
int func(int a)
{
int b;
switch(a)
{
case 1: 30;
case 2: 20;
case 3: 16;
default: 0
}
return b;
}
則func(1)=?
// b定義后就沒(méi)有賦值。
5:
int a[3];
a[0]=0; a[1]=1; a[2]=2;
int *p, *q;
p=a;
q=&a[2];
則a[q-p]=a[2]
解釋?zhuān)褐羔樢淮我苿?dòng)一個(gè)int但計(jì)數(shù)為1
今天早上的面試題9道,比較難,向牛人請(qǐng)教,國(guó)內(nèi)的一牛公司,坐落在北京北四環(huán)某大廈:
1、線形表a、b為兩個(gè)有序升序的線形表,編寫(xiě)一程序,使兩個(gè)有序線形表合并成一個(gè)有序升序線形表h;
答案在 請(qǐng)化大學(xué) 嚴(yán)銳敏《數(shù)據(jù)結(jié)構(gòu)第二版》第二章例題,數(shù)據(jù)結(jié)構(gòu)當(dāng)中,這個(gè)叫做:兩路歸并排序
Linklist *unio(Linklist *p,Linklist *q){
linklist *R,*pa,*qa,*ra;
pa=p;
qa=q;
R=ra=p;
while(pa->next!=NULL&&qa->next!=NULL){
if(pa->data>qa->data){
ra->next=qa;
qa=qa->next;
}
else{
ra->next=pa;
pa=pa->next;
}
}
if(pa->next!=NULL)
ra->next=pa;
if(qa->next!=NULL)
ra->next==qa;
return R;
}
2、運(yùn)用四色定理,為N個(gè)局域舉行配色,顏色為1、2、3、4四種,另有數(shù)組adj[][N],如adj[i][j]=1則表示i區(qū)域與j區(qū)域相鄰,數(shù)組color[N],如color[i]=1,表示i區(qū)域的顏色為1號(hào)顏色。
四色填充
3、用遞歸算法判斷數(shù)組a[N]是否為一個(gè)遞增數(shù)組。
遞歸的方法,記錄當(dāng)前最大的,并且判斷當(dāng)前的是否比這個(gè)還大,大則繼續(xù),否則返回false結(jié)束:
bool fun( int a[], int n )
{
if( n= =1 )
return true;
if( n= =2 )
return a[n-1] >= a[n-2];
return fun( a,n-1) && ( a[n-1] >= a[n-2] );
}
4、編寫(xiě)算法,從10億個(gè)浮點(diǎn)數(shù)當(dāng)中,選出其中最大的10000個(gè)。
用外部排序,在《數(shù)據(jù)結(jié)構(gòu)》書(shū)上有
《計(jì)算方法導(dǎo)論》在找到第n大的數(shù)的算法上加工
5、編寫(xiě)一unix程序,防止僵尸進(jìn)程的出現(xiàn).
同學(xué)的4道面試題,應(yīng)聘的職位是搜索引擎工程師,后兩道超級(jí)難,(希望大家多給一些算發(fā))
1.給兩個(gè)數(shù)組和他們的大小,還有一動(dòng)態(tài)開(kāi)辟的內(nèi)存,求交集,把交集放到動(dòng)態(tài)內(nèi)存dongtai,并且返回交集個(gè)數(shù)
long jiaoji(long* a[],long b[],long* alength,long blength,long* dongtai[])
2.單連表的建立,把'a'--'z'26個(gè)字母插入到連表中,并且倒敘,還要打印!
方法1:
typedef struct val
{ int date_1;
struct val *next;
}*p;
void main(void)
{ char c;
for(c=122;c>=97;c--)
{ p.date=c;
p=p->next;
}
p.next=NULL;
}
}
方法2:
node *p = NULL;
node *q = NULL;
node *head = (node*)malloc(sizeof(node));
head->data = ' ';head->next=NULL;
node *first = (node*)malloc(sizeof(node));
first->data = 'a';first->next=NULL;head->next = first;
p = first;
int longth = 'z' - 'b';
int i=0;
while ( i<=longth )
{
node *temp = (node*)malloc(sizeof(node));
temp->data = 'b'+i;temp->next=NULL;q=temp;
head->next = temp; temp->next=p;p=q;
i++;
}
print(head);
3.可怕的題目終于來(lái)了
象搜索的輸入信息是一個(gè)字符串,統(tǒng)計(jì)300萬(wàn)輸入信息中的最熱門(mén)的前十條,我們每次輸入的一個(gè)字符串為不超過(guò)255byte,內(nèi)存使用只有1G,
請(qǐng)描述思想,寫(xiě)出算發(fā)(c語(yǔ)言),空間和時(shí)間復(fù)雜度,
4.國(guó)內(nèi)的一些帖吧,如baidu,有幾十萬(wàn)個(gè)主題,假設(shè)每一個(gè)主題都有上億的跟帖子,怎么樣設(shè)計(jì)這個(gè)系統(tǒng)速度最好,請(qǐng)描述思想,寫(xiě)出算發(fā)(c語(yǔ)言),空間和時(shí)間復(fù)雜度,
#include string.h
main(void)
{ char *src="hello,world";
char *dest=NULL;
dest=(char *)malloc(strlen(src));
int len=strlen(str);
char *d=dest;
char *s=src[len];
while(len--!=0)
d++=s--;
printf("%s",dest);
}
找出錯(cuò)誤!!
#include "string.h"
#include "stdio.h"
#include "malloc.h"
main(void)
{
char *src="hello,world";
char *dest=NULL;
dest=(char *)malloc(sizeof(char)*(strlen(src)+1));
int len=strlen(src);
char *d=dest;
char *s=src+len-1;
while(len--!=0)
*d++=*s--;
*d='\0';
printf("%s",dest);
}
1. 簡(jiǎn)述一個(gè)Linux驅(qū)動(dòng)程序的主要流程與功能。
2. 請(qǐng)列舉一個(gè)軟件中時(shí)間換空間或者空間換時(shí)間的例子。
void swap(int a,int b)
{
int c; c=a;a=b;b=a;
}
--->空優(yōu)
void swap(int a,int b)
{
a=a+b;b=a-b;a=a-b;
}
6. 請(qǐng)問(wèn)一下程序?qū)⑤敵鍪裁唇Y(jié)果?
char *RetMenory(void)
{
char p[] = “hellow world”;
return p;
}
void Test(void)
{
char *str = NULL;
str = RetMemory();
printf(str);
}
RetMenory執(zhí)行完畢,p資源被回收,指向未知地址。返回地址,str的內(nèi)容應(yīng)是不可預(yù)測(cè)的, 打印的應(yīng)該是str的地址
寫(xiě)一個(gè)函數(shù),它的原形是int continumax(char *outputstr,char *intputstr)
功能:
在字符串中找出連續(xù)最長(zhǎng)的數(shù)字串,并把這個(gè)串的長(zhǎng)度返回,并把這個(gè)最長(zhǎng)數(shù)字串付給其中一個(gè)函數(shù)參數(shù)outputstr所指內(nèi)存。例如:"abcd12345ed125ss123456789"的首地址傳給intputstr后,函數(shù)將返回
9,outputstr所指的值為123456789
int continumax(char *outputstr, char *inputstr)
{
char *in = inputstr, *out = outputstr, *temp, *final;
int count = 0, maxlen = 0;
while( *in != '\0' )
{
if( *in > 47 && *in < 58 )
{
for(temp = in; *in > 47 && *in < 58 ; in++ )
count++;
}
else
in++;
if( maxlen < count )
{
maxlen = count;
count = 0;
final = temp;
}
}
for(int i = 0; i < maxlen; i++)
{
*out = *final;
out++;
final++;
}
*out = '\0';
return maxlen;
}
不用庫(kù)函數(shù),用C語(yǔ)言實(shí)現(xiàn)將一整型數(shù)字轉(zhuǎn)化為字符串
方法1:
int getlen(char *s){
int n;
for(n = 0; *s != '\0'; s++)
n++;
return n;
}
void reverse(char s[])
{
int c,i,j;
for(i = 0,j = getlen(s) - 1; i < j; i++,j--){
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void itoa(int n,char s[])
{
int i,sign;
if((sign = n) < 0)
n = -n;
i = 0;
do{/*以反序生成數(shù)字*/
s[i++] = n%10 + '0';/*get next number*/
}while((n /= 10) > 0);/*delete the number*/
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
方法2:
#include <iostream>
using namespace std;
void itochar(int num);
void itochar(int num)
{
int i = 0;
int j ;
char stra[10];
char strb[10];
while ( num )
{
stra[i++]=num%10+48;
num=num/10;
}
stra[i] = '\0';
for( j=0; j < i; j++)
{
strb[j] = stra[i-j-1];
}
strb[j] = '\0';
cout<<strb<<endl;
}
int main()
{
int num;
cin>>num;
itochar(num);
return 0;
}
前幾天面試,有一題想不明白,請(qǐng)教大家!
typedef struct
{
int a:2;
int b:2;
int c:1;
}test;
test t;
t.a = 1;
t.b = 3;
t.c = 1;
printf("%d",t.a);
printf("%d",t.b);
printf("%d",t.c);
謝謝!
t.a為01,輸出就是1
t.b為11,輸出就是-1
t.c為1,輸出也是-1
3個(gè)都是有符號(hào)數(shù)int嘛。
這是位擴(kuò)展問(wèn)題
01
11
1
編譯器進(jìn)行符號(hào)擴(kuò)展
求組合數(shù): 求n個(gè)數(shù)(1....n)中k個(gè)數(shù)的組合....
如:combination(5,3)
要求輸出:543,542,541,532,531,521,432,431,421,321,
#include<stdio.h>
int pop(int *);
int push(int );
void combination(int ,int );
int stack[3]={0};
top=-1;
int main()
{
int n,m;
printf("Input two numbers:\n");
while( (2!=scanf("%d%*c%d",&n,&m)) )
{
fflush(stdin);
printf("Input error! Again:\n");
}
combination(n,m);
printf("\n");
}
void combination(int m,int n)
{
int temp=m;
push(temp);
while(1)
{
if(1==temp)
{
if(pop(&temp)&&stack[0]==n) //當(dāng)棧底元素彈出&&為可能取的最小值,循環(huán)退出
break;
}
else if( push(--temp))
{
printf("%d%d%d ",stack[0],stack[1],stack[2]);//§ä¨ì¤@?
pop(&temp);
}
}
}
int push(int i)
{
stack[++top]=i;
if(top<2)
return 0;
else
return 1;
}
int pop(int *i)
{
*i=stack[top--];
if(top>=0)
return 0;
else
return 1;
}
1、用指針的方法,將字符串“ABCD1234efgh”前后對(duì)調(diào)顯示
#include <stdio.h>
#include <string.h>
#include <dos.h>
int main()
{
char str[] = "ABCD1234efgh";
int length = strlen(str);
char * p1 = str;
char * p2 = str + length - 1;
while(p1 < p2)
{
char c = *p1;
*p1 = *p2;
*p2 = c;
++p1;
--p2;
}
printf("str now is %s\n",str);
system("pause");
return 0;
}
2、有一分?jǐn)?shù)序列:1/2,1/4,1/6,1/8……,用函數(shù)調(diào)用的方法,求此數(shù)列前20項(xiàng)的和
#include <stdio.h>
double getValue()
{
double result = 0;
int i = 2;
while(i < 42)
{
result += 1.0 / i;//一定要使用1.0做除數(shù),不能用1,否則結(jié)果將自動(dòng)轉(zhuǎn)化成整數(shù),即0.000000
i += 2;
}
return result;
}
int main()
{
printf("result is %f\n", getValue());
system("pause");
return 0;
}
有一個(gè)數(shù)組a[1000]存放0--1000;要求每隔二個(gè)數(shù)刪掉一個(gè)數(shù),到末尾時(shí)循環(huán)至開(kāi)頭繼續(xù)進(jìn)行,求最后一個(gè)被刪掉的數(shù)的原始下標(biāo)位置。
以7個(gè)數(shù)為例:
{0,1,2,3,4,5,6,7} 0-->1-->2(刪除)-->3-->4-->5(刪除)-->6-->7-->0(刪除),如此循環(huán)直到最后一個(gè)數(shù)被刪除。
方法1:數(shù)組
#include <iostream>
using namespace std;
#define null 1000
int main()
{
int arr[1000];
for (int i=0;i<1000;++i)
arr[i]=i;
int j=0;
int count=0;
while(count<999)
{
while(arr[j%1000]==null)
j=(++j)%1000;
j=(++j)%1000;
while(arr[j%1000]==null)
j=(++j)%1000;
j=(++j)%1000;
while(arr[j%1000]==null)
j=(++j)%1000;
arr[j]=null;
++count;
}
while(arr[j]==null)
j=(++j)%1000;
cout<<j<<endl;
return 0;
}方法2:鏈表
#include<iostream>
using namespace std;
#define null 0
struct node
{
int data;
node* next;
};
int main()
{
node* head=new node;
head->data=0;
head->next=null;
node* p=head;
for(int i=1;i<1000;i++)
{
node* tmp=new node;
tmp->data=i;
tmp->next=null;
head->next=tmp;
head=head->next;
}
head->next=p;
while(p!=p->next)
{
p->next->next=p->next->next->next;
p=p->next->next;
}
cout<<p->data;
return 0;
}
方法3:通用算法
#include <stdio.h>
#define MAXLINE 1000 //元素個(gè)數(shù)
/*
MAXLINE 元素個(gè)數(shù)
a[] 元素?cái)?shù)組
R[] 指針場(chǎng)
suffix 下標(biāo)
index 返回最后的下標(biāo)序號(hào)
values 返回最后的下標(biāo)對(duì)應(yīng)的值
start 從第幾個(gè)開(kāi)始
K 間隔
*/
int find_n(int a[],int R[],int K,int& index,int& values,int s=0) {
int suffix;
int front_node,current_node;
suffix=0;
if(s==0) {
current_node=0;
front_node=MAXLINE-1;
}
else {
current_node=s;
front_node=s-1;
}
while(R[front_node]!=front_node) {
printf("%d\n",a[current_node]);
R[front_node]=R[current_node];
if(K==1) {
current_node=R[front_node];
continue;
}
for(int i=0;i<K;i++){
front_node=R[front_node];
}
current_node=R[front_node];
}
index=front_node;
values=a[front_node];
return 0;
}
int main(void) {
int a[MAXLINE],R[MAXLINE],suffix,index,values,start,i,K;
suffix=index=values=start=0;
K=2;
for(i=0;i<MAXLINE;i++) {
a[i]=i;
R[i]=i+1;
}
R[i-1]=0;
find_n(a,R,K,index,values,2);
printf("the value is %d,%d\n",index,values);
return 0;
}
試題:
void test2()
{
char string[10], str1[10];
int i;
for(i=0; i<10; i++)
{
str1[i] = 'a';
}
strcpy( string, str1 );
}
解
答:對(duì)試題2,如果面試者指出字符數(shù)組str1不能在數(shù)組內(nèi)結(jié)束可以給3分;如果面試者指出strcpy(string,
str1)調(diào)用使得從str1內(nèi)存起復(fù)制到string內(nèi)存起所復(fù)制的字節(jié)數(shù)具有不確定性可以給7分,在此基礎(chǔ)上指出庫(kù)函數(shù)strcpy工作方式的給10
分;
str1不能在數(shù)組內(nèi)結(jié)束:因?yàn)閟tr1的存儲(chǔ)為:{a,a,a,a,a,a,a,a,a,a},沒(méi)有'\0'(字符串結(jié)束符),所以不能結(jié)束
strcpy( char *s1,char *s2)他的工作原理是,掃描s2指向的內(nèi)存,逐個(gè)字符付到s1所指向的內(nèi)存,直到碰到'\0',因?yàn)閟tr1結(jié)尾沒(méi)有'\0',所以具有不確定性,不知道他后面還會(huì)付什么東東。
正確應(yīng)如下
void test2()
{
char string[10], str1[10];
int i;
for(i=0; i<9; i++)
{
str1[i] = 'a'+i; //把a(bǔ)bcdefghi賦值給字符數(shù)組
}
str[i]='\0';//加上結(jié)束符
strcpy( string, str1 );
}
第二個(gè)code題是實(shí)現(xiàn)strcmp
int StrCmp(const char *str1, const char *str2)
做是做對(duì)了,沒(méi)有抄搞,比較亂
int StrCmp(const char *str1, const char *str2)
{
assert(str1 && srt2);
while (*str1 && *str2 && *str1 == *str2) {
str1++, str2++;
}
if (*str1 && *str2)
return (*str1-*str2);
elseif (*str1 && *str2==0)
return 1;
elseif (*str1 = = 0 && *str2)
return -1;
else
return 0;
}
int StrCmp(const char *str1, const char *str2)
{
//省略判斷空指針(自己保證)
while(*str1 && *str1++ = = *str2++);
return *str1-*str2;
}
第三個(gè)code題是實(shí)現(xiàn)子串定位
int FindSubStr(const char *MainStr, const char *SubStr)
做是做對(duì)了,沒(méi)有抄搞,比較亂
int MyStrstr(const char* MainStr, const char* SubStr)
{
const char *p;
const char *q;
const char * u = MainStr;
//assert((MainStr!=NULL)&&( SubStr!=NULL));//用斷言對(duì)輸入進(jìn)行判斷
while(*MainStr) //內(nèi)部進(jìn)行遞增
{
p = MainStr;
q = SubStr;
while(*q && *p && *p++ == *q++);
if(!*q )
{
return MainStr - u +1 ;//MainStr指向當(dāng)前起始位,u指向
}
MainStr ++;
}
return -1;
}
分析:
int arr[] = {6,7,8,9,10};
int *ptr = arr;
*(ptr++)+=123;
printf(“ %d %d ”, *ptr, *(++ptr));
輸出:8 8
過(guò)程:對(duì)于*(ptr++)+=123;先做加法6+123,然后++,指針指向7;對(duì)于printf(“ %d %d ”, *ptr, *(++ptr));從后往前執(zhí)行,指針先++,指向8,然后輸出8,緊接著再輸出8
華為全套完整試題
高級(jí)題
6、已知一個(gè)單向鏈表的頭,請(qǐng)寫(xiě)出刪除其某一個(gè)結(jié)點(diǎn)的算法,要求,先找到此結(jié)點(diǎn),然后刪除。
slnodetype *Delete(slnodetype *Head,int key){}中if(Head->number==key)
{
Head=Pointer->next;
free(Pointer);
break;
}
Back = Pointer;
Pointer=Pointer->next;
if(Pointer->number==key)
{
Back->next=Pointer->next;
free(Pointer);
break;
}
void delete(Node* p)
{
if(Head = Node)
while(p)
}
有一個(gè)16位的整數(shù),每4位為一個(gè)數(shù),寫(xiě)函數(shù)求他們的和。
解釋?zhuān)?br>整數(shù)1101010110110111
和 1101+0101+1011+0111
感覺(jué)應(yīng)該不難,當(dāng)時(shí)對(duì)題理解的不是很清楚,所以寫(xiě)了一個(gè)函數(shù),也不知道對(duì)不對(duì)。
疑問(wèn):
既然是16位的整數(shù),1101010110110111是2進(jìn)制的,那么函數(shù)參數(shù)怎么定義呢,請(qǐng)大蝦指教。
答案:用十進(jìn)制做參數(shù),計(jì)算時(shí)按二進(jìn)制考慮。
/* n就是16位的數(shù),函數(shù)返回它的四個(gè)部分之和 */
char SumOfQuaters(unsigned short n)
{
char c = 0;
int i = 4;
do
{
c += n & 15;
n = n >> 4;
} while (--i);
return c;
}
有1,2,....一直到n的無(wú)序數(shù)組,求排序算法,并且要求時(shí)間復(fù)雜度為O(n),空間復(fù)雜度O(1),使用交換,而且一次只能交換兩個(gè)數(shù).(華為)
#include<iostream.h>
int main()
{
int a[] = {10,6,9,5,2,8,4,7,1,3};
int len = sizeof(a) / sizeof(int);
int temp;
for(int i = 0; i < len; )
{
temp = a[a[i] - 1];
a[a[i] - 1] = a[i];
a[i] = temp;
if ( a[i] == i + 1)
i++;
}
for (int j = 0; j < len; j++)
cout<<a[j]<<",";
return 0;
}
(慧通)
1 寫(xiě)出程序把一個(gè)鏈表中的接點(diǎn)順序倒排
typedef struct linknode
{
int data;
struct linknode *next;
}node;
//將一個(gè)鏈表逆置
node *reverse(node *head)
{
node *p,*q,*r;
p=head;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=NULL;
head=p;
return head;
}
2 寫(xiě)出程序刪除鏈表中的所有接點(diǎn)
void del_all(node *head)
{
node *p;
while(head!=NULL)
{
p=head->next;
free(head);
head=p;
}
cout<<"釋放空間成功!"<<endl;
}
3兩個(gè)字符串,s,t;把t字符串插入到s字符串中,s字符串有足夠的空間存放t字符串
void insert(char *s, char *t, int i)
{
char *q = t;
char *p =s;
if(q == NULL)return;
while(*p!='\0')
{
p++;
}
while(*q!=0)
{
*p=*q;
p++;
q++;
}
*p = '\0';
}
分析下面的代碼:
char *a = "hello";
char *b = "hello";
if(a= =b)
printf("YES");
else
printf("NO");
這個(gè)簡(jiǎn)單的面試題目,我選輸出 no(對(duì)比的應(yīng)該是指針地址吧),可在VC是YES 在C是NO
lz的呢,是一個(gè)常量字符串。位于靜態(tài)存儲(chǔ)區(qū),它在程序生命期內(nèi)恒定不變。如果編譯器優(yōu)化的話,會(huì)有可能a和b同時(shí)指向同一個(gè)hello的。則地址相同。如果編譯器沒(méi)有優(yōu)化,那么就是兩個(gè)不同的地址,則不同