首页 > 试题广场 >

计算单位阶跃函数

[编程题]计算单位阶跃函数
  • 热度指数:31445 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

KiKi最近学习了信号与系统课程,这门课里有一个非常有趣的函数,单位阶跃函数,其中一种定义方式为:

现在试求单位冲激函数在时域t上的值。


输入描述:
题目有多组输入数据,每一行输入一个t(-1000<t<1000)表示函数的时域t。


输出描述:
输出函数的值并换行。
示例1

输入

11
0
-11

输出

1
0.5
0
def judge(x):
    if x>0:
        x=1
    elif x<0:
        x=0
    else:
        x=0.5
    print(x)

while True:
    try:
        n = eval(input())
        judge(n)
    except:
        break
    

发表于 2020-10-11 03:16:42 回复(0)
题目描述有点问题,单位阶跃和单位冲激不是一个函数吧
发表于 2021-07-29 21:19:59 回复(0)
#include<iostream>
using namespace std;
int main()
{
    int t;
    double result;
    while(scanf("%d",&t)!=EOF)
    {
        if(t<0)
            result=0;
        else if(t==0)
            result=0.5;
        else if(t>0)
            result=1;
        cout<<result<<endl;
    }
    return 0;
}//求教为什么t用double型就出错?为什么不定义result,直接输出“0/0.5/1”也会报错?

发表于 2020-05-07 21:12:29 回复(1)
#include<iostream>
using namespace std;
void phi(int t)
{
    if (t == 0) cout << 0.5 << endl;
    if (t < 0) cout << 0 << endl;
    if (t > 0) cout << 1 << endl;
}
int main()
{
    int t;
    while(cin >> t)
    {
        phi(t);
    }
}

发表于 2022-02-26 14:12:14 回复(0)
#include<stdio.h>
int main()
{
    int a;
    while(~scanf("%d\n",&a))
          {
              if (a<0)
                  printf("0\n");
              else if(a>0)
                  printf("1\n");
              else if(a==0)
                  printf("0.5\n");
          }
    return 0;
}
发表于 2021-07-11 23:08:49 回复(1)
#include <stdio.h>

int main() {
    int t=0;
    while (scanf("%d", &t) != EOF) 
    {
        getchar();
        if(t<0)
        {
            printf("0\n");
        }
        else if(0==t)
        {
            printf("0.5\n");
        }
        else {
        printf("1\n");
        }
    }
    return 0;
}

发表于 2024-01-07 12:40:59 回复(0)
代码为C语言
        用了一个比较笨的方法,不过能写出来->使用if_else语句进行判断+输出
#include <stdio.h>
#include <math.h>
int main()
{
    int t;//定义整形变量
    while(scanf("%d",&t)!=EOF)
    {
        if(t>0)//if_else语句判断
        {
            printf("1\n");
        }
        else if(t==0)
        {
            printf("0.5\n");
        }
        else if(t<0)
        {
            printf("0\n");
        }
    }
    return 0;
}

发表于 2020-12-29 09:55:37 回复(1)
import sys

for i in sys.stdin:
    if int(i) == 0:
        print(0.5)
    else:
        print(1 if int(i) > 0 else 0)

发表于 2025-09-14 14:52:36 回复(0)
double f(int x)
{
    if (x > 0)
    {
        return 1.0;
    }
    else if (x == 0)
    {
        return 0.5;
    }
    else {
        return 0.0;
    }
}

int main()
{
    int x = 0;
    double y = 0;
    while (scanf("%d", &x) != EOF)
    {
        y = f(x);
        if (x >= -1000 && x <= 1000)
        {
            printf("%g\n", y);
        }
        else {
            printf("NO\n");
        }
    }
    return 0;
}

发表于 2025-08-20 11:53:01 回复(0)
#include <stdio.h>

int main() 
{
    int t;
    
    while(scanf("%d",&t)!=EOF)
    {
        if (t>0)
        {printf("1\n");}
        else if(t<0)
        {printf("0\n");}
        else 
        {printf("0.5\n");}
    }

    return 0;
}


发表于 2025-07-11 23:39:38 回复(0)
#include <stdio.h>

int main() {
    int a = 0;
    while (scanf("%d", &a) != EOF)
    {
        if (a > 0)
        {
            printf("1\n");
        }
        else if (a == 0)
        {
            printf("0.5\n");
        }
        else
        {
            printf("0");
        }
    }
    return 0;
}
发表于 2025-06-09 21:41:33 回复(0)
while 1:
    try:
        a=int(input())
        b=0
        if a>0:
            b=1
        elif a==0:
            b=0.5
        else:
            b=0
        print(b)
    except:
        break
发表于 2025-02-17 17:11:06 回复(0)
#include <stdio.h>
int main() {
    int t = 0;
    while (scanf("%d", &t) != EOF) {
        if ( 0 == t){
            printf("0.5\n");
        }
        else{
            t > 0 ? printf("1\n") : printf("0\n");
        }
    }
    return 0;
}
发表于 2025-01-05 21:24:21 回复(0)
#include <stdio.h>

int main() {
    int t;
    while (scanf("%d", &t) == 1) {
        if(t > 0) printf("1\n");
        else if(t < 0)printf("0\n");
        else printf("0.5\n");
    }
    return 0;
}
发表于 2024-12-27 11:24:51 回复(0)
#include <stdio.h>

int main()
{
    int t;
    while(scanf("%d",&t)!=EOF)
    {
                if(t>0)
        {
            printf("1\n");
        }
                if(t<0)
        {
            printf("0\n");
        }
                if(t==0)
        {
            printf("0.5\n");
        }
    }
    return 0;
}
发表于 2024-11-26 19:48:30 回复(0)
#include <stdio.h>

int main() {
    int t, b;
    while (scanf("%d", &t) != EOF) 
    { 
        if(t>0)
        {
          printf("1\n");
        }
        if(t==0)
        {
            printf("0.5\n");
        }
        if(t<0)
        {
            printf("0\n");
        }
        
    }
    return 0;
}

发表于 2024-09-25 18:54:27 回复(0)
while True:
    try:
        num = int(input())
        if num > 0:
            print(1)
        elif num==0:
            print(0.5)
        elif num<0:
            print(0)
    except Exception as e:
        break
发表于 2024-09-25 14:30:11 回复(0)
#include <stdio.h>

int main() {
    int t;
    while (scanf("%d ", &t) != EOF) {
        if (t>0)
          printf("1\n");
          else if(t==0)
          printf("0.5\n");
          else
          printf("0\n");
       
    }
    return 0;
}
发表于 2024-06-19 16:51:47 回复(0)
#include <stdio.h>

int main() {
    int t;
    while (scanf("%d", &t) != EOF) {
        if (t!=0) {
            printf("%d\n", t>0? 1:0);
        }
        else {
            printf("%.1f\n", 0.5);
        }
    }
    return 0;
}
发表于 2024-06-09 17:55:30 回复(0)
#include <stdio.h>

int main()
{
    int t = 0;
//while (1) 和 while (scanf("%d", &t) == 1 是不同的概念。
//while (1) 表示条件永远为真
//while (scanf("%d", &t) == 1 表示循环会根据scanf函数的返回值来确定是否继续执行。只有当成功读取一个整数并赋值给变量t时,条件为真,循环才会继续执行;否则,循环会终止。
    while (scanf("%d", &t)==1)
    {
       
        if(t > 0)
        {
            printf("1\n");
           
        }
        else if(0  == t)
        {
            printf("0.5\n");
        }
        else if(t < 0)
        {
            printf("0\n");
        }
    }

    return 0;
}
发表于 2024-05-08 20:29:14 回复(0)