- 相關推薦
C筆試題
1) 讀文件file1.txt的內(nèi)容(例如):
12
34
56
輸出到file2.txt:
56
34
12
(逆序)
2)輸出和為一個給定整數(shù)的所有組合
例如n=5
5=1+4;5=2+3(相加的數(shù)不能重復)
則輸出
1,4;2,3,
C筆試題
。第一題,注意可增長數(shù)組的應用.
#include
#include
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
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
void main()
{unsigned long int a,i=1;
scanf(“%d”,&a);
if(a%2==0)
{ for(i=1;i printf(“%d”,a,a-i);
}
else
for(i=1;i<=a/2;i++)
printf(” %d, %d”,i,a-i);
}
兄弟,這樣的題目若是做不出來實在是有些不應該, 給你一個遞規(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
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;
}
在對齊為4的情況下
struct BBB
{ long num;
char *name;
short int data;
char ha;
short ba[5];
}*p;
p=0×1000000;
p+0×200=____;
(Ulong)p+0×200=____;
(char*)p+0×200=____;
解答:假設在32位CPU上,
sizeof(long) = 4 bytes
sizeof(char *) = 4 bytes
sizeof(short int) = sizeof(short) = 2 bytes
sizeof(char) = 1 bytes
由于是4字節(jié)對齊,
sizeof(struct BBB) = sizeof(*p)
= 4 + 4 + 2 + 1 + 1/*補齊*/ + 2*5 + 2/*補齊*/ = 24 bytes (經(jīng)Dev-C++驗證)
p=0×1000000;
p+0×200=____;
= 0×1000000 + 0×200*24
(Ulong)p+0×200=____;
= 0×1000000 + 0×200
(char*)p+0×200=____;
= 0×1000000 + 0×200*4
你可以參考一下指針運算的細節(jié)
【C筆試題】相關文章:
C/C++筆試題目大全08-22
筆美國國家儀器試題目09-23
C++面試筆試題目07-16
阿里巴巴C++工程師筆試題目10-17
新浪筆經(jīng)04-27
c 面試編程問題08-11
新聞總署筆經(jīng)10-13
IBM公司筆經(jīng)09-15
營銷卷筆經(jīng)10-25