指针-Problem A: 编写函数:你交换了吗?之一 (Append Code)

Problem A: 编写函数:你交换了吗?之一 (Append Code)
Time Limit: 1 Sec Memory Limit: 2 MB
Submit: 8227 Solved: 4824
[Submit][Status]
Description

从标准输入读取两个整数a、b,按先小后大的次序输出。那么a、b的输出顺序与输入顺序是否一致呢?即,输出相对于输入是否交换过位置。

注意:a和b相等时不产生交换。


用C语言编写函数实现,append.c中函数原型为:

int is_swapped(int * a, int * b);

功能:返回值是0或者1。其它的就不告诉你了,猜猜看-

用C++编写函数实现,append.cc中函数原型为:

bool isSwapped(int &a, int &b);

功能:返回一个布尔值(true或false)。

函数的调用格式见“Append Code”。

Input

两个较小的整数a,b,用空格分开。

Output

输出有两种情况:

  1. “a b NO”, 当a,b没有交换过

  2. “b a YES”, 当a,b交换过

Sample Input
5 3
Sample Output
3 5 YES
HINT

参看系统首页上的“Append Code”使用说明,讨论版(Web Board)上也有。

Append Code
append.c, append.cc,

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int is_swapped(int * a, int * b)
{
    int t;
    if(*a>*b)
    {
        t=*a;
        *a=*b;
        *b=t;
        return 1;
    }
    else
        return 0;
}

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    if(is_swapped(&a, &b))
        printf("%d %d YES", a, b);
    else
        printf("%d %d NO", a, b);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值