1411 - C语言-分段函数

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

有一个函数

y={ x      x<1
    | 2x-1   1<=x<10
    \ 3x-11  x>=10

写一段程序,输入x,输出y

题目输入

一个数x

题目输出

一个数y

输入/输出样例

输入格式

14

输出格式

31

C语言解答

#include<stdio.h>
void main()
{
	int x,y;
	scanf("%d",&x);
	if(x<1)
		y=x;
	else if(1<=x&&x<10)
		y=2*x-1;
	else
		y=3*x-11;
	printf("%d\n",y);
}

C++解答

#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;

int main()
{
	//ifstream cin("aaa.txt");
	int i,j,n,m,x1,x2,x3,x4,len;
	cin>>n;
	if(n<1) cout<<n;
	else if(n<10) cout<<2*n-1;
	else cout<<3*n-11;
	cout<<endl;
	


return 0;
}