c语言的习题

  • 从键盘任意输入3个整数,输出其中最大的数。

#include <stdio.h>

void main()

{

int a,b,c;

printf("请输入三个整数:\n");

scanf("%d%d%d",&a,&b,&c);

int max=0;

max=((a>b)?a:b)>c?((a>b)?a:b):c;

printf("输入的三个数中的最大值为:%d\n",max);

}

  • 计算表达式Y的值. x由键盘输入。

为什么下面的表达是错误的????

原因:自己犯了一个小错误,x=0表示赋值,x==0才表示等于

正确的程序代码:

#include <stdio.h>
void main()
{
    int x,y;
    printf("请输入一个整数x:\n");
    scanf("%d",&x);
    if(x>0)  { y=x;}
    /*
    else if(x<0) {y=-x;}
    else {y=0;}
    */
    else if(x==0) {y=0;}
    else {y=-x;}
    printf("与x对应的y值为:%d\n",y);

}

  • 2001年我国人口数为12.9533亿,如果我国人口以每年平均1.07%速度增长,问多少年后我国人口达到或超过15亿。

猜你喜欢

转载自blog.csdn.net/qq_39368007/article/details/82382029
今日推荐