UVA10494 IfWe Were a Child Again
来源:互联网 发布:ai 人工智能 影评 编辑:程序博客网 时间:2024/06/01 10:11
Problem C
IfWe Were a Child Again
Input: standard input
Output: standard output
Time Limit: 7 seconds
“Oooooooooooooooh!
If I could do the easy mathematics like my school days!!
I can guarantee, that I’d not make any mistake this time!!”
Says a smart university student!!
But his teacher even smarter – “Ok! I’d assign you such projects in your software lab. Don’t be so sad.”
“Really!!” - the students feels happy. And he feels so happy that he cannot see the smile in his teacher’s face.
The Problem
The first project for the poor student was to make a calculator that can just perform the basic arithmetic operations.
But like many other university students he doesn’t like to do any project by himself. He just wants to collect programs from here and there. As you are a friend of him, he asks you to write the program. But, you are also intelligent enough to tackle this kind of people. You agreed to write only the (integer) division and mod (% in C/C++) operations for him.
Input
Input is a sequence of lines. Each line will contain an input number. One or more spaces. A sign (division or mod). Again spaces. And another input number. Both the input numbers are non-negative integer. The first one may be arbitrarily long. The second number n will be in the range (0 < n < 231).
Output
A line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space.
Sample Input
110 / 100
99 % 10
2147483647 / 2147483647
2147483646 % 2147483647
Sample Output
1
9
1
2147483646
这是一题高精度的除法(求余与除法同理)。 因为被除数在long的范围内,题目简单了不少。用字符串接受除数,用一个long接收被除数。
AC代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int len;
int relen;
char str[1000];
char result[1000];
long num;
void sub()
{
int j = 0;
long flag = 0;
for(int i = 0 ; i < len;i++)
{
flag = flag * 10+( str[i] - 48 );
result[j] = flag / num;
flag = flag % num;
j++;
}
relen = j;
}
void mod()
{
long flag = 0;
for(int i = 0 ; i < len ; i++)
{
flag = flag * 10 + str[i] - 48;
flag = flag % num;
}
printf("%ld\n",flag);
}
int main()
{
char c;
while (scanf("%s %c %ld",str,&c,&num)!=EOF) {
len = strlen(str);
if (c == '/') {
int i = 0,j;
sub();
while ( i < relen - 1 && result[i] == 0)
++i;
for (j = i ; j < relen ;j++) {
printf("%d",result[j]);
}
printf("\n");
}
else if(c == '%'){
mod();
}
memset( result ,0 ,sizeof(result));
memset(str ,0 ,sizeof( str) );
}
return 0;
}
- UVA10494 IfWe Were a Child Again
- uva10494 - If We Were a Child Again
- uva10494 if we were a child again
- uva10494 - If We Were a Child Again
- UVA10494 If We Were a Child Again
- UVA10494 - If We Were a Child Again
- 10494 If We Were a Child Again
- 10494 - If We Were a Child Again
- 5312 If We Were a Child Again
- 10494 - If We Were a Child Again
- If We Were a Child Again
- If We Were a Child Again
- 10494 - If We Were a Child Again
- uva If We Were a Child Again
- If We Were a Child Again
- If We Were a Child Again
- UVa UVa 10494 - If We Were a Child Again
- 10494 - If We Were a Child Again大总结
- union 内幕
- 那些妖术——树的遍历
- java.util.Scanner应用详解
- OC面向对象和类
- urb分析,usb_fill_bulk_urb函数理解
- UVA10494 IfWe Were a Child Again
- LeetCode | Search for a Range(查找数据出现的范围)
- oss系统的pager.ftl页面
- MFC activeX入门
- vc6.0中OpenGL开发环境的配置
- 小结
- 操作符重载的应用(数组类)
- 网游服务器数据包设计
- CSerialPort类错误排除