几道网站注册时要求做的题目
#include <stdio.h>
int main()
{
int i=43;
printf("%d\n",printf("%d",printf("%d",i)));
return 0;
}
int main()
{
int i=43;
printf("%d\n",printf("%d",printf("%d",i)));
return 0;
}
输出: 4321
这道题的关键是printf函数返回什么, 平时很少用到,查下文档说返回的是输出字符的个数. 所以最里面的输出43,返回2,中间的输出2,返回1,最外面的输入1,返回1.
#include <stdio.h>
int main()
{
int a = 1;
printf("%d", a << 2 + a);
return 0;
}
int main()
{
int a = 1;
printf("%d", a << 2 + a);
return 0;
}
输出: 8
这个输出要看运算符的优先级,因为"+"先于"<<",所以先 2+a,得值3,表达式就变成了 a << 3, 而a仍然等于1,所以左移3位二进制变成了 00..00 0000 1000 = 8
#include <stdio.h>
#define ARRAY_SIZE 10
int main()
{
int arr[ARRAY_SIZE] = {51,116,53,120,85,66,71,98,86,100};
int i, j;
for(i = 0; i < ARRAY_SIZE; i++)
for(j = 0; j < ARRAY_SIZE-1; j++)
if(arr[j] > arr[j+1]) {
arr[j] ^= arr[j+1];
arr[j+1] ^= arr[j];
arr[j] ^= arr[j+1];
}
for(i = 0; i < ARRAY_SIZE; i++)
printf("%c", arr[i],arr[i]);
}
#define ARRAY_SIZE 10
int main()
{
int arr[ARRAY_SIZE] = {51,116,53,120,85,66,71,98,86,100};
int i, j;
for(i = 0; i < ARRAY_SIZE; i++)
for(j = 0; j < ARRAY_SIZE-1; j++)
if(arr[j] > arr[j+1]) {
arr[j] ^= arr[j+1];
arr[j+1] ^= arr[j];
arr[j] ^= arr[j+1];
}
for(i = 0; i < ARRAY_SIZE; i++)
printf("%c", arr[i],arr[i]);
}
输出: 35BGUVbdtx
这个题是做异或运算,根据公式 a ^ b ^ a = b
#include <stdio.h>
int main() {
int x;
printf("%d",x == (1 && x));
}
int main() {
int x;
printf("%d",x == (1 && x));
}
输出: 0
x未初始化,所以这个值是"脏值",不知道里面是什么值.不知是否有0的可能性. 1 && x 做逻辑与,所以结果为真,即1,再做判断 x==1,返回假,所以输出0.
#include <stdio.h>
int n[]={0x48,
0x65,0x6C,0x6C,
0x6F,0x2C,0x20,
0x77,0x6F,0x72,
0x6C,0x64,0x21,
0x0A,0x00},*m=n;
main(n){
if(putchar (*m)!='\0') main(m++);
}
int n[]={0x48,
0x65,0x6C,0x6C,
0x6F,0x2C,0x20,
0x77,0x6F,0x72,
0x6C,0x64,0x21,
0x0A,0x00},*m=n;
main(n){
if(putchar (*m)!='\0') main(m++);
}
#include <stdio.h>
main()
{
printf("%s:%c%d", "pipi", 41, 72);
}
main()
{
printf("%s:%c%d", "pipi", 41, 72);
}
输出: pipi:)72
按类型输出就对.