任務和代碼
/*
*Copyright (c)2017,CSDN學院
*All rights reserved.
*文件名稱: main.c
*作 者: 伍志鵬
*完成日期: 2017年9月1日
*版本號: v1.0
*
*問題描述: 計算下面函數的值並輸出(x取整型即可)。
*
* y={x-1,x>=1
* {-x+1,x<1
*
*程序輸出: y的值
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,y;
printf("輸入x的值:");
scanf("%d",&x);
if( x>=1){
y=x-1;
}else{
y=-x+1;
}
printf("y的值是:%d\n",y);
return 0;
}
運行結果
知識點總結
跟住if分支結構並且結合函數的方式來解決該問題。