If We Were a Child Again
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的更多相关文章
- MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程
在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...
- [翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6
原文:http://rerun.me/2014/10/21/akka-notes-child-actors-and-path/ Actor是完全的继承结构.你创建的任何Actor肯定都是一个其他Act ...
- 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 ...
- [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action
概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在应用程序运行的时候动态创建的内容.给View添加动态内容的方式可归纳为下面几种: Inline cod ...
- 调用Child Package
使用Execute Package Task,能够在一个package中调用并执行其他package,被调用的Package称作 Child Package,Execute Package Task ...
- ORA-02292: integrity constraint (xxxx) violated - child record found
在更新表的主键字段或DELETE数据时,如果遇到ORA-02292: integrity constraint (xxxx) violated - child record found 这个是因为主外 ...
- Child <- many-to-one ->Parent
网上找到个描述的很精妙的例子 Child <- many-to-one ->Parent class Child { private ...
- [NHibernate]Parent/Child
系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...
- ScrollView can host only one direct child
Android 采用ScrollView布局时出现异常:ScrollView can host only one direct child. 异常原因: 主要是ScrollView内部只能有一个子元素 ...
- 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 ...
随机推荐
- Linux的正常关机
Azure上的 IaaS 虚拟机可使用多种方式关闭,例如通过 Azure 管理门户.Azure Powershell cmdlet 或 CLI 工具,甚至还可以由交互式登录的用户关闭.Azure 平台 ...
- Ultra-QuickSort(归并排序)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 49267 Accepted: 18035 ...
- canvas学习笔记(一)-认识canvas
canvas是html5新增的一个标签,主要用于图形的绘制.我们可以把它理解为是浏览器给我们提供了一个画板,至于要绘制怎样的画卷,就看神笔马良你的主意了.而在canvas上绘制图形使用的笔,就是js了 ...
- _视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途
iOS视图控制对象生命周期-init.viewDidLoad.viewWillAppear.viewDidAppear.viewWillDisappear.viewDidDisappear的区别及用途 ...
- HTTP的报文格式解析
一.概述 http报文是面向文本的,报文中每一个字段都是一些ASCII码串,各个字段的长度是不确定的.http有两类报文:请求报文 响应报文 二.请求报文 一个http请求报文由 请求行(reque ...
- Canvas路径、描边、填充
<script> var context = document.getElementById('canvas').getContext('2d'); context.font = '48p ...
- C# 想要程序文件移动 而数据保持相对位置
如果用的数据库是access数据库 可以把数据库文件放到bin\debug下面,引用相对位置就可以了 如果程序中有上载文件,而程序需要使用到该文件,那么我们最好也是引用相对文件,我们只需要在数据表中的 ...
- IOS开发之Cocoa编程—— NSUndoManager
在Cocoa中使用NSUndoManager可以很方便的完成撤销操作.NSUndoManager会记录下修改.撤销操作的消息.这个机制使用两个NSInvocation对象栈. NSInvocation ...
- 通过git和Xcode将代码上传到GitHub
长话短说: 第一步:初始化一个本地仓库 git init 第二步:将你的文件添加到缓存区 git add . 后面的空格 . 是将全部的文件都添加到缓存区 可以使用git status 查看状态 ...
- TCP的流量控制
TCP协议作为一个可靠的面向字节流的传输协议,其可靠性和流量控制由滑动窗口协议保证,而拥塞控制则由控制窗口结合一系列的控制算法实现. 要区分TCP的流量控制和拥塞控制: 流量控制是发送方的发送数据的速 ...