- 相关推荐
C笔试题
1) 读文件file1.txt的内容(例如):
12
34
56
输出到file2.txt:
56
34
12
(逆序)
2)输出和为一个给定整数的所有组合
例如n=5
5=1+4;5=2+3(相加的数不能重复)
则输出
1,4;2,3,
C笔试题
。第一题,注意可增长数组的应用.
#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);
}
兄弟,这样的题目若是做不出来实在是有些不应该, 给你一个递规反向输出字符串的例子,可谓是反序的经典例程.
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;
}
借签了楼上的“递规反向输出”
#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字节对齐,
sizeof(struct BBB) = sizeof(*p)
= 4 + 4 + 2 + 1 + 1/*补齐*/ + 2*5 + 2/*补齐*/ = 24 bytes (经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
你可以参考一下指针运算的细节
【C笔试题】相关文章:
C/C++笔试题目大全08-22
笔美国国家仪器试题目09-23
C++面试笔试题目07-16
阿里巴巴C++工程师笔试题目10-17
新浪笔经04-27
c 面试编程问题08-11
新闻总署笔经10-13
consulting firm笔经05-06
IBM公司笔经09-15
营销卷笔经10-25