首页 > 试题广场 >

HTTP状态码

[编程题]HTTP状态码
  • 热度指数:27192 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

KiKi访问网站,得到HTTP状态码,但他不知道什么含义,BoBo老师告诉他常见HTTP状态码:200(OK,请求已成功),202(Accepted,服务器已接受请求,但尚未处理。)400(Bad Request,请求参数有误),403(Forbidden,被禁止),404(Not Found,请求失败),500(Internal Server Error,服务器内部错误),502(Bad Gateway,错误网关)。


输入描述:
多组输入,一行,一个整数(100~600),表示HTTP状态码。


输出描述:
针对每组输入的HTTP状态,输出该状态码对应的含义,具体对应如下:
200-OK
202-Accepted
400-Bad Request
403-Forbidden
404-Not Found
500-Internal Server Error
502-Bad Gateway
示例1

输入

200

输出

OK
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<Integer,String> map = new HashMap<>(16);
        map.put(200,"OK");
        map.put(202,"Accepted");
        map.put(400,"Bad Request");
        map.put(403,"Forbidden");
        map.put(404,"Not Found");
        map.put(500,"Internal Server Error");
        map.put(502,"Bad Gateway");
        while (sc.hasNext()) {
            int state = sc.nextInt();
            System.out.println(map.get(state));
        }
    }
}

发表于 2020-09-23 20:43:07 回复(0)
#include<bits/stdc++.h>
using namespace std;

int main() {
    string s[7] = {
      "200-OK",
      "202-Accepted",
      "400-Bad Request",
      "403-Forbidden",
      "404-Not Found",
      "500-Internal Server Error",
      "502-Bad Gateway"
    };
    string in;
    while(cin >> in) {
        for(int i=0;i<7;i++) {
            if(s[i].find(in)!=-1) {
                cout << s[i].substr(4) << endl;
            }
        }
    }
    return 0;
}

发表于 2020-07-31 10:56:50 回复(0)
#include<iostream>
using namespace std;
int main()
{
    int n;
    while(cin >> n)
    {
        if (n == 200) cout << "OK" << endl;
        else if (n == 202) cout << "Accepted" << endl;
        else if (n == 400) cout << "Bad Request" << endl;
        else if (n == 403) cout << "Forbidden" << endl;
        else if (n == 404) cout << "Not Found" << endl;
        else if (n == 500) cout << "Internal Server Error" << endl;
        else if (n == 502) cout << "Bad Gateway" << endl;
    }

}

发表于 2022-02-26 14:11:46 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int num = sc.nextInt();
            if(num == 200) System.out.println("OK");
            if(num == 202) System.out.println("Accepted");
            if(num == 400) System.out.println("Bad Request");
            if(num == 403) System.out.println("Forbidden");
            if(num == 404) System.out.println("Not Found");
            if(num == 500) System.out.println("Internal Server Error");
            if(num == 502) System.out.println("Bad Gateway");
        }
    }
}

发表于 2021-10-08 11:03:36 回复(0)
#include <stdio.h>
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        switch(n)
        {
            case(200):printf("OK\n");break;
            case(202):printf("Accepted\n");break;
            case(400):printf("Bad Request\n");break;
            case(403):printf("Forbidden\n");break;
            case(404):printf("Not Found\n");break;
            case(500):printf("Internal Server Error\n");break;
            case(502):printf("Bad Gateway\n");break;
        }
    }
    
}

发表于 2020-04-10 22:33:27 回复(1)
可用list也可以用dictionary去做,但是用list会更高效
用list去做
http_name = ['200','202','400','403','404','500','502']
http_meaning = ['OK','Accepted','Bad Request','Forbidden',
                'Not Found','Internal Server Error','Bad Gateway']

while True:
    try:
        word = input()
        index = http_name.index(word)
        print(http_meaning[index])
    except:break

用dictionary去做
http_dict = {'200':'OK','202':'Accepted','400':'Bad Request',
             '403':'Forbidden','404':'Not Found',
             '500':'Internal Server Error','502':'Bad Gateway'}


while True:
    try:
        word = input()
        print(http_dict[word])
    except:break


发表于 2021-02-28 17:10:49 回复(0)
#include <stdio.h>

int main(){
    typedef struct {
        int num;
        char arr[30];
    } httpmap;
    
    httpmap arr[7] = {{200, "OK"}, {202, "Accepted"}, {400, "Bad Request"}, {403, "Forbidden"},
                     {404, "Not Found"}, {500, "Internal Server Error"}, {502, "Bad Gateway"}};
    int num = 0;
    while(scanf("%d", &num) != EOF){
        getchar();
        for(int i = 0; i < 7; i++){
            if(arr[i].num == num)
                printf("%s\n", arr[i].arr);
          }
    }
    return 0;
}

发表于 2022-05-22 21:29:57 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()) {
            int state = scan.nextInt();
            
            int[] httpstate = {200,202,400,403,404,500,502};
            String[] result = {"OK","Accepted","Bad Request","Forbidden","Not Found","Internal Server Error","Bad Gateway"};
            for(int i=0;i<httpstate.length;i++){
                if(state == httpstate[i]){
                    System.out.println(result[i]);
                    break;
                }
        }
    }
    }
}

发表于 2021-11-07 12:06:29 回复(0)
代码为C语言
我的思路:
        这道题解法不唯一,可用if_else语句判断并输出,也可用switch结构,更简洁。
#include <stdio.h>
#include <math.h>
int main()
{
    int HTTP;
    while(scanf("%d",&HTTP)!=EOF)
    {
        switch(HTTP)
        {
            case 200:printf("OK\n");break;
            case 202:printf("Accepted\n");break;
            case 400:printf("Bad Request\n");break;
            case 403:printf("Forbidden\n");break;
            case 404:printf("Not Found\n");break;
            case 500:printf("Internal Server Error\n");break;
            case 502:printf("Bad Gateway\n");break;
        }
    }
    return 0;
}

发表于 2020-12-30 15:44:19 回复(0)
#include <stdio.h>

int main() {
    int n = 0;
    while (scanf("%d", &n) != EOF) {
        switch (n) {
            case 200:
                printf("OK\n");
                break;
            case 202:
                printf("Accepted\n");
                break;
            case 400:
                printf("Bad Request\n");
                break;
            case 403:
                printf("Forbidden\n");
                break;
            case 404:
                printf("Not Found\n");
                break;
            case 500:
                printf("Internal Server Error\n");
                break;
            case 502:
                printf("Bad Gateway\n");
                break;
        }
    }
    return 0;
}

编辑于 2024-01-10 10:59:40 回复(0)
http_dict = {'200':'OK','202':'Accepted','400':'Bad Request',
             '403':'Forbidden','404':'Not Found',
             '500':'Internal Server Error','502':'Bad Gateway'}


while True:
    try:
        word = input()
        print(http_dict[word])
    except:break

发表于 2023-01-18 14:28:42 回复(0)
#include <stdio.h>
int main() {
int http=0;
while((scanf("%d",&http))!=EOF){
http==200?printf("OK\n"):http==202?printf("Accepted\n"):http==400?printf("Bad Request\n"):http==403?printf("Forbidden\n"):http==404?printf("Not Found\n"):http==500?printf("Internal Server Error\n"):http==502?printf("Bad Gateway\n"):0;}
}

发表于 2022-10-25 12:19:04 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        Map<Integer,String> type=new HashMap<>();
        type.put(200,"OK");
        type.put(202,"Accepted");
        type.put(400,"Bad Request");
        type.put(403,"Forbidden");
        type.put(404,"Not Found");
        type.put(500,"Internal Server Error");
        type.put(502,"Bad Gateway");
        while(sc.hasNextInt())
            System.out.println(type.get(sc.nextInt()));
    }
}

发表于 2022-08-08 19:35:10 回复(0)
while True:
    try:
        a=[200,202,400,403,404,500,502]
        b=('OK','Accepted','Bad Request','Forbidden','Not Found','Internal Server Error','Bad Gateway')
        n=int(input())
        print(b[a.index(n)])
    except:
        break

发表于 2021-05-27 18:39:18 回复(0)
#include <stdio.h>

int main() {
    int i = 0;
    while (scanf("%d", &i) != EOF) {
        switch (i) {
            case 200:
                printf("OK\n");
                break;
            case 202:
                printf("Accepted\n");
                break;
            case 400:
                printf("Bad Request\n");
                break;
            case 403:
                printf("Forbidden\n");
                break;
            case 404:
                printf("Not Found\n");
                break;
            case 500:
                printf("Internal Server Error\n");
                break;
            case 502:
                printf("Bad Gateway\n");
                break;
        }
    }
    // }while(i==502?0:1);     //当i = 502时,就会跳出循环,“错误网关”
    return 0;
}

发表于 2024-04-18 18:14:41 回复(0)
#include <stdio.h>

enum
{
    OK = 200,
    Accepted = 202,
    BadRequest = 400,
    Forbidden = 403,
    NotFound = 404,
    InternalServerError = 500,
    BadGateway = 502
};

int main() 
{
    int num = 0;
    int i = 0;
            
    while(scanf("%d", &num) != EOF)
    {
        switch(num)
        {
            case OK:
            printf("OK\n");
            break;
            case Accepted:
            printf("Accepted\n");
            break;
            case BadRequest:
            printf("Bad Request\n");
            break;
            case Forbidden:
            printf("Forbidden\n");
            break;
            case NotFound:
            printf("Not Found\n");
            break;
            case InternalServerError:
            printf("Internal Server Error\n");
            break;
            case BadGateway:
            printf("Bad Gateway\n");
            break;
            default:
            printf("输入错误\n");
            break;
        }
    }

    return 0;
}

发表于 2024-03-19 13:39:13 回复(0)
a = {
    "200":"OK",
    "202":"Accepted",
    "400":"Bad Request",
    "403":"Forbidden",
    "404":"Not Found",
    "500":"Internal Server Error",
    "502":"Bad Gateway"
}

while True:
    try:
     b = input()
     print(a[b])
    except:
        break


发表于 2024-02-04 13:11:58 回复(0)
dic = {'200':'OK','202':'Accepted','400':'Bad Request','403':'Forbidden','404':'Not Found','500':'Internal Server Error','502':'Bad Gateway'}
while True:
    try:
        print (dic[input()])
    except:
        break
发表于 2024-01-18 15:04:16 回复(0)
#include <stdio.h>

int main() {
    int a;
    while(scanf("%d", &a) != EOF){
        switch (a) {
            case 200:printf("OK\n");break;
            case 202:printf("Accepted\n");break;
            case 400:printf("Bad Request\n");break;
            case 403:printf("Forbidden\n");break;
            case 404:printf("Not Found\n");break;
            case 500:printf("Internal Server Error\n");break;
            case 502:printf("Bad Gateway\n");break;
        }
    }
    return 0;
}
编辑于 2024-01-16 12:55:01 回复(0)
#include <stdio.h>
int main()
{
    int n = 0;
    while (scanf("%d", &n) != EOF)
    {
        int stl[7] = { 200,202,400,403,404,500,502 };//升序数组
        char ch[7][50] = { {"OK"},{"Accepted"},{"Bad Request"},{"Forbidden"},{"Not Found"},{"Internal Server Error"},{"Bad Gateway"} };

        int i, j = 0;
        for (i = 0; i < 7; i++,j++)
        {
                if (n == stl[i])
                {
                    printf("%s\n", ch[j]);
                }
        }

    }
    return 0;
}
编辑于 2024-01-08 21:24:44 回复(0)

问题信息

上传者:牛客309119号
难度:
88条回答 3026浏览

热门推荐

通过挑战的用户

查看代码