UVA_If We Were a Child Again

Description

If We Were a Child Again

“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.


Problemsetter:  S. M. Ashraful Kadir, University of Dhaka

题意:大数除小数;大数模小数问题;

这里0 < n < 231 ;

n用long long就行了;

注意:模板里面一起同步将int 改为 long long ;

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #define UNIT 10
 6
 7 using namespace std;
 8
 9 struct Bignum
10 {
11     long long val[10000];
12     int len;
13
14     Bignum ()
15     {
16         memset (val, 0, sizeof(val));
17         len = 1;
18     }
19
20     Bignum operator / (const long long &a) const//大数除小数
21     {
22         Bignum x;
23         long long down = 0;
24         for (int i = len-1; i >= 0; --i)
25         {
26             x.val[i] = (val[i]+down*UNIT) / a;
27             down = val[i] + down*UNIT - x.val[i]*a;
28         }
29         x.len = len;
30         while (x.val[x.len-1] == 0 && x.len > 1)
31             x.len--;
32         return x;
33     }
34
35     long long operator % (const long long &a) const//大数模小数
36     {
37         int x = 0;
38         for (int i = len-1; i >= 0; --i)
39             x = ((x*UNIT)%a+val[i]) % a;
40         return x;
41     }
42
43
44 };
45
46
47 char a[10000],ch;
48 long long b;
49 int main()
50 {
51     //freopen("ACM.txt","r",stdin);
52
53     while(scanf("%s %c %lld", a, &ch, &b)!=EOF)
54     {
55        // printf("%c",ch);
56         Bignum x1,ans;
57         int La=strlen(a);
58         x1.len=0;
59         for(int i=La-1;i>=0;i--)
60         x1.val[x1.len++]=a[i]-‘0‘;
61        // cout<<ch<<b<<endl;
62         if(ch==‘/‘)
63         {
64             ans=x1/b;
65             for(int i=ans.len-1;i>=0;i--)
66             cout<<ans.val[i];
67             cout<<endl;
68         }
69         else if(ch==‘%‘)
70         {
71             cout<<x1%b<<endl;
72         }
73     }
74     return 0;
75 }

时间: 2024-10-11 04:30:17

UVA_If We Were a Child Again的相关文章

加压压缩文件报错gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now

压缩包是直接weget 后面加官网上的tar包地址获取的 [[email protected] ~]# tar -zxvf /home/hadoop/hadoop-2.6.5-src.tar.gz gzip: stdin: not in gzip formattar: Child returned status 1tar: Error is not recoverable: exiting now[[email protected] ~]# tar -xvf /home/hadoop/hadoo

out of memory kill process (java) score or a child

最近在跑大数据,发现 out of memory kill process (java) score or a child,查资料发现是操作系统linux low mem 太低,后来加大内存一样不管用,开始分析代码: 将代码中的String 字符串拼接改成StringBuilder(单线程速度比StringBuffer快) 将获取文件名称的方法file.listFiles() 改成 file.list() 将代码中的不必要的创建对象.数组开销去掉,尤其是在循环里 发现仍然有问题,通过free -

LINUX:解压问题tar: Child returned status

解压某个文件时 #tar -zxvf xxxxx.tar.gz 出现下面的错误提示: gzip: stdin: not in gzip formattar: Child returned status 1tar: Error exit delayed from previous errors You used "tar -zxvf" for the second command. the 'z' option tells tar to use gzip to uncompress th

Java tomcat启动失败(Servlet3.0 Web Project):A child container failed during start

Tomcat启动失败,失败全部信息: 五月 11, 2016 10:21:04 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:MyEL' did not find a matching prop

tar (child): jdk-7u71-linux-x64.tar.gz:无法 open: 没有那个文件或目录

1 错误描述 [email protected]:~$ sudo mkdir /usr/lib/jvm [sudo] password for youhaidong: [email protected]:~$ sudo tar zxvf jdk-7u71-linux-x64.tar.gz -C /usr/lib/jvm tar (child): Error is not recoverable: exiting now tar: Child returned status 2 tar: Erro

[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException] The child node &quot;db_driver&quot; at path &quot;fos_user&quot; must be configured.

$ php bin/console server:run [Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]  The child node "db_driver" at path "fos_user" must be configured. 必须新建一个USer类

解压.tar.gz出错gzip: stdin: not in gzip format tar: /Child returned status 1 tar: Error is not recoverable: exiting now

先查看文件真正的属性是什么? [[email protected] ~]# tar -zxvf tcl8.4.16-src.tar.gz gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now [[email protected] ~]# file tcl8.4.16-src.tar.gz tcl8.4.16-src.tar.gz: HTML d

Linux parent process and child process when &#39;sudo&#39;

如果在一般用户下如user,执行sudo命令,会产生两个进程. ps -ef | grep Container root 4305 643 0 16:37 pts/39 00:00:00 sudo ./ContainerCompilation.shroot 4306 4305 0 16:37 pts/39 00:00:00 /bin/bash ./ContainerCompilation.sh 4306进程的父进程是4305,也就是说unix先生成4305进程将命令交给root,然后产生4306

bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因

这个异常的出现往往是因为非法使用了某些方法引起的. 从字面意思上是说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容.这里很明显这个child是一个View,一个子(child)View必须依赖于父(parent)View,如果你要使用这个child,则必须通过parent,而你如果就是硬想使用这个child,那么就得让这个child与parent脱离父子关系(即removeView()) 何时会出现这种异常呢,典型的