C/C++编程学习 - 第4周 ① 整数大小比较

题目链接

题目描述

小蒜蒜爱玩比大小:输入两个整数,比较它们的大小。

输入格式
一行,包含两个整数 x 和 y,中间用单个空格隔开。

0 ≤ x < 232,−231 ≤ y < 231

输出格式
一个字符:
若 x>y,输出’>’;
若 x=y,输出’=’;
若 x<y,输出’<’。

Sample Input

1000 100

Sample Output

>

思路

输入两个整数,比较大小,按题目的要求做就行,尽管给出的 n 是在 int 范围内的,但最好还是开long long 防止溢出。

C语言代码:

#include<stdio.h>
int main()
{
    
    
	long long x, y;
	scanf("%lld%lld", &x, &y);
	if(x < y) printf("<");
	else if(x > y) printf(">");
	else printf("=");
	return 0;
}

C++代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	long long x, y;
	while(cin >> x >> y)
		if(x > y) cout << ">" << endl;
		else if(x == y) cout << "=" << endl;
		else if(x < y) cout << "<" << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44826711/article/details/112879404
今日推荐