Easy Problem 5 求绝对值

本文介绍了一个简单的C程序,用于读取多组测试数据中的整数,并输出这些整数的绝对值。程序使用了基本的字符串处理技巧来移除负号。

原文地址:https://my.oschina.net/meiguizhinian/blog/873711

Description

给你一个整数,请求出这个数字的绝对值。

The Input

你的程序需要从标准输入设备(通常为键盘)中读入多组测试数据。每组输入数据占一行,其中仅有一个整数N;N的绝对值不会超过1,000位数;输入的数据没有前导零;如果N是负数,则在负号和最左边一位数字之间不会有空格。每行的行首和行尾都没有多余的空格。在所有数据的前后,以及两组数据之间都没有多余的空行。

The Output

对每组测试数据,你的程序需要向标准输出设备(通常为启动该程序的终端)依次输出一组对应的答案。每组答案占一行,其中仅有一个整数,即N的绝对值。在行首和行尾不要输出多余的空格。在所有数据的前后,以及两组数据之间不要输出多余的空行。

Sample Input

121
-34

Sample Output

121
34

代码

#include <stdio.h>
int main(){
	char buf[1002];
	while (scanf("%s",buf)!=EOF) {
		if (buf[0]=='-') {
			printf("%s\n",buf+1);//跳过第一个
		}
		else {
			printf("%s\n",buf);
		}
	}
	return 0;
}

 

转载于:https://my.oschina.net/meiguizhinian/blog/873711

问题 M: The Lost Cow【Easy】 时间限制: 1.000 Sec 内存限制: 128 MB 题目描述 Farmer John has lost his prize cow Bessie, and he needs to find her! Fortunately, there is only one long path running across the farm, and Farmer John knows that Bessie has to be at some location on this path. If we think of the path as a number line, then Farmer John is currently at position x and Bessie is currently at position y (unknown to Farmer John). If Farmer John only knew where Bessie was located, he could walk directly to her, traveling a distance of |x−y|. Unfortunately, it is dark outside and Farmer John can't see anything. The only way he can find Bessie is to walk back and forth until he eventually reaches her position. Trying to figure out the best strategy for walking back and forth in his search, Farmer John consults the computer science research literature and is somewhat amused to find that this exact problem has not only been studied by computer scientists in the past, but that it is actually called the "Lost Cow Problem" (this is actually true!). The recommended solution for Farmer John to find Bessie is to move to position x+1, then reverse direction and move to position x−2, then to position x+4, and so on, in a "zig zag" pattern, each step moving twice as far from his initial starting position as before. As he has read during his study of algorithms for solving the lost cow problem, this approach guarantees that he will at worst travel 9 times the direct distance |x−y| between himself and Bessie before he finds her (this is also true, and the factor of 9 is actually the smallest such worst case guarantee any strategy can achieve). Farmer John is curious to verify this result. Given x and y, please compute the total distance he will travel according to the zig-zag search strategy above until he finds Bessie. 输入 The single line of input contains two distinct space-separated integers x and y. Both are in the range 0…1,000. 输出 Print one line of output, containing the distance Farmer John will travel to reach Bessie. 样例输入 Copy 3 6 样例输出 Copy 9 模拟问题,用C++
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值