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. genymotion 模拟器 真是牛叉了 速度超快啊!!! 不解释了!建议大家速度去体验一把吧!

    已经有人写了blog了 我就不再赘述了,详情去这里看去吧!!   android genymotion模拟器怎么使用以及和google提供的模拟器性能对比  http://blog.csdn.net/ ...

  2. android退出activity的方式总结(一)

    在android中使用:[activityname].this.finish();  只是退出了activity的堆栈中,要真正的退出程序在手机cpu中的运行,当应用不再使用时,通常需要关闭应用,可以 ...

  3. SQlSERVER生成唯一编号

    基数表-用来存储编号前缀和类型 建表如下 CREATE TABLE [dbo].[SerialNo]( [sCode] [varchar](50) NOT NULL, [sName] [varchar ...

  4. Linux远程自动输入密码抓取远程资源

    #!/usr/bin/expect -fset timeout 3000set sys_date [lindex $argv 0] #要抓取的文件日期spawn scp /data3/xiaorui/ ...

  5. socket中的option

    /// Set an option on the socket. /** * This function is used to set an option on the socket. * * @pa ...

  6. Hive入门之UDFS函数

    一.UDFS函数介绍 1. 基本UDF (1)SHOWFUNCTIONS:这个用来熟悉未知函数. DESCRIBE FUNCTION<function_name>; (2)A IS NUL ...

  7. Python3.5创建虚拟环境

    为每个程序单独创建虚拟环境可以保证程序只能访问虚拟环境中的包,保持全局解释器的干净整洁,使其只作为创建(更多)虚拟环境的源. windows下创建虚拟环境 Python3.5自带venv,只需执行py ...

  8. nyoj 138 找球号(二)(哈希)

    题目:nyoj——138 /*** 哈希求解...采用链表保存 插入时,可以去除重复 查找 找到该组,然后在改组的查找 当这个组不存在时或是没有找到时是 NO 其他是YES 1e6+1 时间最短 */ ...

  9. 在PADS LAYOUT中如何隐藏不需要的鼠线?

    如下图示,将net GPR_0的鼠线隐藏. 鼠标右键,选择网络----选择你要隐藏的网络------右键选择view nets----点击对话框右边View List里你所选的网络-----在右下角t ...

  10. 航道水下地形DEM构建方法比较

    论文<航道水下数字高程模型的构建方法> 对航道水下地形建立DEM,技术路线:先构建TIN,手动去除多余三角边,再利用CAD ObjectARX二次开发接口中提供的几种内插方法生成grid ...