首页 > 试题广场 >

计算单位阶跃函数

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

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

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


输入描述:
题目有多组输入数据,每一行输入一个t(-1000
                        

输出描述:
输出函数的值并换行。
示例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)
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)
#include <iostream>
using namespace std;

int main() {
    int a;
    while (cin >> a ) 
    { 
        if (a > 0) 
            cout << "1" << endl;
        else if (a < 0)
            cout << "0" << endl;
        else
            cout << "0.5" << endl;
    }
}

发表于 2024-05-02 20:37:42 回复(0)
#include <stdio.h>

float Ot(int t) {
    if (t > 0)
        return 1;
    else if (t < 0)
        return 0;
    else
        return 0.5;
}

int main() {
    int input = 0;
    while (scanf("%d", &input) != EOF) {
        float ret = Ot(input);
        if (ret == 0.5)
            printf("%.1f\n", ret);
        else
            printf("%.0f\n", ret);
    }
    return 0;
}

编辑于 2024-04-18 18:24:05 回复(0)
发表于 2024-03-26 13:55:07 回复(0)
#include <stdio.h>

int main() 
{
    int t = 0;

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

    return 0;
}

编辑于 2024-03-18 23:18:19 回复(0)