Description

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

HINT

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str[1000],a[100],b[100],c;
int t,i,j,x,y;
int num,s,d[1000],e,m;
while(gets(str))
{
t=0;
for(i=0;str[i];i++)
{
if(str[i]=='/'||str[i]=='%')
{
c=str[i];
a[t-1]='\0';
break;
}
else
a[t++]=str[i];
}
a[t]='\0';
t=0;
for(j=i+2;str[j];j++)
b[t++]=str[j];
b[t]='\0';
x=strlen(a);
y=strlen(b);
s=0;
if(strcmp(a,b)<0&&x<y)
{
if(c=='/')
cout<<"0"<<endl;
else if(c=='%')
{
cout<<a<<endl;
}
}
else if(strcmp(a,b)==0)
{
if(c=='/')
cout<<"1"<<endl;
else if(c=='%')
{
cout<<"0"<<endl;
}
}
else
{
for(i=0;i<y;i++)
s=s*10+(b[i]-'0');
num=0;
if(c=='/')
{
e=0;
for(i=0;i<x;i++)
{
num=num*10+(a[i]-'0');//从高位向低位逐渐的除(例子123/5,1/5=0,* 1%5=1 * ,(1*10+2)/5=2,* 12%5=2 * ,(2*10+3)/5=4)所以为024
m=num/s;
d[e++]=m;
num%=s;
}
for(i=0;i<e;i++)
{
if(d[i]!=0)
break;
}
for(j=i;j<e;j++)
cout<<d[j];
//printf("%I64d",d[j]);
cout<<endl;
}
else if(c=='%')
{
for(i=0;i<x;i++)
{
num=num*10+(a[i]-'0');//从高位向低位逐渐的取余(例子123%5, 1%5=1 ,(1*10+2)%5=2,(2*10+3)%5=3)所以为余数3
num%=s;
}
cout<<num<<endl;
//printf("%I64d\n",num);
} // cout<<s<<endl;
}
//cout<<a<<endl<<x<<endl;
//cout<<b<<endl<<y<<endl;
}
return 0;
}

另一种方法更简洁

#include<stdio.h>
#include<string.h>
int main()
{
int b,t,len,i,j,k;
char a[1000],flag;
while(scanf("%s %c %d",a,&flag,&b)!=EOF)
{
len=strlen(a);
if(flag=='/')
t=0; //标志作用
else if(flag=='%')
t=1;
k=0;
j=0;
for(i=0;i<len;i++)
{
k=k*10+(a[i]-'0');
if((k/b)!=0||(j!=0))
{
if(t==0)
printf("%d",k/b);
k%=b;
j=1;//标志除了第一次整除的零不输出外,其余均输出,例如100/5输出的是20而不是020
}
}
if(j==0&&t==0) printf("0");//整除的特殊情况例如2/5的值是零
if(t!=0) printf("%d",k);
printf("\n");
}
return 0;
}

If We Were a Child Again的更多相关文章

  1. MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程

    在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...

  2. [翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6

    原文:http://rerun.me/2014/10/21/akka-notes-child-actors-and-path/ Actor是完全的继承结构.你创建的任何Actor肯定都是一个其他Act ...

  3. php php-5.6.4.tar.bz2 apache 兼容问题 child pid 27858 exit signal Segmentation fault

    环境 [root envirotar]# uname -a Linux i2..el6.x86_64 # SMP Thu Jul :: UTC x86_64 x86_64 x86_64 GNU/Lin ...

  4. [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action

    概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在应用程序运行的时候动态创建的内容.给View添加动态内容的方式可归纳为下面几种: Inline cod ...

  5. 调用Child Package

    使用Execute Package Task,能够在一个package中调用并执行其他package,被调用的Package称作 Child Package,Execute Package Task ...

  6. ORA-02292: integrity constraint (xxxx) violated - child record found

    在更新表的主键字段或DELETE数据时,如果遇到ORA-02292: integrity constraint (xxxx) violated - child record found 这个是因为主外 ...

  7. Child <- many-to-one ->Parent

    网上找到个描述的很精妙的例子 Child   <-   many-to-one   ->Parent         class   Child   {         private   ...

  8. [NHibernate]Parent/Child

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...

  9. ScrollView can host only one direct child

    Android 采用ScrollView布局时出现异常:ScrollView can host only one direct child. 异常原因: 主要是ScrollView内部只能有一个子元素 ...

  10. java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

    在ViewPager中,用Fragment显示页面时,报错: java.lang.IllegalStateException: The specified child already has a pa ...

随机推荐

  1. Michael Kors - Wikipedia, the free encyclopedia

    Michael Kors - Wikipedia, the free encyclopedia Michael Kors From Wikipedia, the free encyclopedia   ...

  2. SQL Server索引进阶:第十三级,插入,更新,删除

    在第十级到十二级中,我们看了索引的内部结构,以及改变结构造成的影响.在本文中,继续查看Insert,update,delete和merge造成的影响.首先,我们单独看一下这四个命令. 插入INSERT ...

  3. 每个页面的Title样式

    <style>.zc_lan14 {}{    TEXT-ALIGN: center; FONT-FAMILY: "微软雅黑"; MARGIN-BOTTOM: 3px; ...

  4. 运行JBoss 5.1.0 GA时出现Error installing to Instantiated:name=AttachmentStore state=Described错误的解决办法

    第一次开JBoss服务器:有些时候会遇到这种情况:把以下的文字替换即可 进到类似目录 server/default/conf/bootstrap,打开文件 profile.xml找到: Xml代码 & ...

  5. iOS开发蓝牙 蓝牙4.0的各种踩过的坑,希望你们少踩点

    1.首先建立这个三个参数 @property (nonatomic,strong)CBCentralManager * manager; @property (nonatomic,strong)CBP ...

  6. 获取文件数据流+叠加byte数组(给byte数组加包头包尾)

    OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "(*.mp4)|*.mp4|(*.*)|*.*"; ofd.Res ...

  7. WinSock网络编程基础(3)server

    上一篇讲的是简单的发送数据的客户端的实现.接下来讲的是如何实现收发数据服务器.这里说的服务器其实就是一个进程,它需要等待任意数量的客户端与之建立起连接,以便响应它们的请求. 服务器必须在已知的名称上监 ...

  8. BZOJ 4199: [Noi2015]品酒大会( 后缀数组 + 并查集 )

    求出后缀数组后, 对height排序, 从大到小来处理(r相似必定是0~r-1相似), 并查集维护. 复杂度O(NlogN + Nalpha(N)) ------------------------- ...

  9. JSP数据交互——九大内置对象及其方法详解(一)

    ①既然说到JSP内置对象,那么什么是JSP内置对象呢? 解析:JSP内置对象,就是在编写JSP页面时,不需要做任何声明就可以直接使用的对象. 如下代码片段:  <% int[]  value  ...

  10. 一周学会Mootools 1.4中文教程:(7)汇总收尾

    转眼之间已经第七课了,这也将成为最后一课,如果这7课下来您感觉水平没有达到预想的水平,没关系您可以继续关注本站的博文,我会陆续发一些类似的文章帮您提升水平,另外我最近打算和群里的几个Mootools爱 ...