C语言趣味小题

C语言趣味小题,第1张

#include<蠢凳雹李stdio.h>

int check(int i)

{ int j=i*i

if(j/带肆旅100==(j/10)%10 || j/100==j%10 || (j/10)%10==j%10 )

return j

return 0

}

void main()

{ int i,j

for(i=11i<32i++)

{j=check(i)

if(j)printf("%d ",j)//write to file

}

}

a 是格式化字符串, 替换后程序实际打印为世厅:

printf( "main() {char*a=%c%s%cprintf(a,34,a,34)}", 34, "main() {char*a=%c%s%cprintf(a,34,a,34)}", 34 )

34的ASCII是双引号,最后打印为

main() {char*a="main() {char*a=%c%s%cprintf(a,34,a,34)}",34)}

理解上应该不是很戚返盯困难,不过看上去高和比较复杂而已

#include<stdio.h>

typedef struct GOODTHINGS

{

int trueIndex //做好事的号码 -1 代表没有指示谁做了好事帆简含

int falseIndex//没做好事的号码 -1代表没有指示谁没做好事

GOODTHINGS()

{

trueIndex = -1

falseIndex = -1

}

} GoodThings, *pGoodThings

bool isOK(const pGoodThings pA, int nSizeofA)

void main()

{

GoodThings a[4]

GoodThings temp

a[0].falseIndex = 0//A指示自己态笑没做好事

a[1].trueIndex = 2 //B说是C

a[2].trueIndex = 3 //C说是D

a[3].falseIndex = 3//D说C说谎,即D指示自己没做好事

int trueCount = 0

for(int i = 0i <4i++)

{

temp.falseIndex = a[i].falseIndex

temp.trueIndex = a[i].trueIndex

if(i == 0 || i == 3)

{

a[i].trueIndex = i //a[i] tell lies

a[i].falseIndex = -1

}

else

{

a[i].falseIndex = a[i].trueIndex

a[i].trueIndex = -1 //a[i] tell lies

}

if(isOK(a, 4) == true)

return

a[i].falseIndex = temp.falseIndex

a[i].trueIndex = temp.trueIndex

}

}

bool isOK(const pGoodThings pA, int nSizeofA)

{

int trueIndex = -1//没有指示

for(int i = 0i <nSizeofAi++)

{

if(pA[i].trueIndex != -1 &&trueIndex == -1) //咐凯首次指示pA中谁做了好事

trueIndex = pA[i].trueIndex

if(pA[i].trueIndex != -1 &&pA[i].trueIndex != trueIndex) //与公共指示谁做好事不同,则互斥退出

return false

}

if(trueIndex == -1)

{

printf("Can't specify the index...\r\n")

return true

}

for(i = 0i <nSizeofAi++)

{

if(pA[i].falseIndex == trueIndex &&pA[i].falseIndex != -1) //指示trueIndex没做好事,又指示trueIndex做了好事,互斥退出

return false

}

printf("result index: %d\r\n", trueIndex)

return true

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/12558783.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存