C语言的几个有趣问题

问题1. 不能使用分号,编写一个“Hello World”程序。

问题2. 如何用C语言打印“ I am print %”?

问题3. 不能使用">、<、>=、<=“等关系运算符,找到3个整数a ,b ,c中的最小的数。

问题4. 程序中不使用“+”法运算,实现整数加法运算。



参考程序:

1. 这是一个比较有趣的问题,但是答案往往十分简单,所采用的是最基本的语句,参考程序如下:

#include <stdio.h>
void main()
{
    if(printf("Hello World!\n")){}  //可以换成while等
}
2. 对于初学者来说,有不少人不知道如何打印百分号这种类型的符号,其实也很简单,无非是在前面加个百分号而已。

#include <stdio.h>
int main()
{
    printf("I am print %%!");
}
3. 第3个问题有一定的挑战性,而且答案不止一个,此外,关系运算符有:

参考程序:

#include <stdio.h>
#define min(a,b) (((a)-(b)) >> 31 ? (a) : (b))
int main()
{
    int a , b , c;
    a = -30;
    b = 59;
    c = 56;
    printf("min = %d",min(min(a,b),c));
}
4. 不能使用"+"法运算,自然想到当初学的计算机组成原理的二进制相加的知识,采用位运算来实现相加:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int x,y;   //输入的两个数
    int inBit = 0x0000;
    int tailBit = 0x0001;
    int result = 0,Rbit;
    int XlastBit,YlastBit;
    int inBitREP;
    printf("please input two numbers :");
    scanf("%d%d",&x,&y);
    while(tailBit)
    {
        XlastBit = x&tailBit;
        YlastBit = y&tailBit;
        Rbit = XlastBit^YlastBit^inBit;
        inBitREP = 0;
        if( (XlastBit == tailBit && YlastBit == tailBit) || ( XlastBit == tailBit && inBit == tailBit) || (YlastBit == tailBit && inBit == tailBit))  //超过两个1,则进位为1
        {
            inBitREP = tailBit<<1;
        }
        inBit = inBitREP;
        result = result|Rbit;
        tailBit = tailBit<<1;
    }
    printf("The result is %d\n",result);
    system("pause");
    return 0;
}

以上便是C语言的几个有趣的问题,希望学习C语言的童鞋们会喜欢,以上代码均在CodeBlocks上运行无误。第一次发帖子,求各位大神轻喷!小弟先谢过了!
注:本博客与博客园上的博客为同一博客主: http://www.cnblogs.com/bestDavid/


我可以回答这个问题。以下是几个有趣的 C 语言程序: 1. Hello World 程序 #include <stdio.h> int main() { printf("Hello World!\n"); return ; } 这是一个经典的 C 语言程序,它会输出 "Hello World!" 到控制台。 2. 猜数字游戏 #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int num, guess, tries = ; srand(time()); num = rand() % 100 + 1; printf("猜数字游戏!\n"); do { printf("请输入一个数字(1-100):"); scanf("%d", &guess); tries++; if (guess > num) { printf("太大了!\n"); } else if (guess < num) { printf("太小了!\n"); } else { printf("恭喜你猜对了!你用了 %d 次机会。\n", tries); } } while (guess != num); return ; } 这是一个简单的猜数字游戏,程序会生成一个 1-100 的随机数,玩家需要猜出这个数字,程序会提示玩家猜的数字是太大了还是太小了,直到猜对为止。 3. 计算器程序 #include <stdio.h> int main() { char op; double num1, num2; printf("请输入一个运算符(+、-、*、/):"); scanf("%c", &op); printf("请输入两个数字:"); scanf("%lf %lf", &num1, &num2); switch(op) { case '+': printf("%.2lf + %.2lf = %.2lf", num1, num2, num1 + num2); break; case '-': printf("%.2lf - %.2lf = %.2lf", num1, num2, num1 - num2); break; case '*': printf("%.2lf * %.2lf = %.2lf", num1, num2, num1 * num2); break; case '/': printf("%.2lf / %.2lf = %.2lf", num1, num2, num1 / num2); break; default: printf("错误的运算符!"); } return ; } 这是一个简单的计算器程序,程序会提示用户输入一个运算符和两个数字,然后根据运算符进行相应的计算并输出结果。 以上是几个有趣的 C 语言程序,它们可以帮助初学者更好地理解 C 语言的基本语法和编程思想。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值