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 ...
随机推荐
- python IOError: invalid mode ('r') or filename
我想要用pandas.read_table()将数据表中的数据读到一个pandas DataFrame对象中: import pandas as pd unames = ['user_id', 'ge ...
- Myeclipse2014 SVN安装方法以及项目上传到svn服务器
1. 打开 Myeclipse 工具栏下的Help下的Install from Site 2.打开后弹出窗口, 并点击Add标签,如下图: 3.现在是最重要的一步,填写相关信息. 在对话框Name输入 ...
- activity的打开关闭动画
Activity的打开关闭或者说相互跳转之间可以设置动画的.默认的打开关闭直接消失或出现,比较不优美,但是有的手机Rom对这个默认做了修改,比如红米HM1,默认的就是新页面自右向左滑动出现,自左向右滑 ...
- iOS 常用第三方
MWPhotoBrowser 非常好用的图片浏览器 FDFullscreenPopGesture 用于全屏滑动切换视图 Aspects 用于快速AOP编程 AFNetworking iOS开发中最为火 ...
- C/C++中volatile关键字详解 (转)
1. 为什么用volatile? C/C++ 中的 volatile 关键字和 const 对应,用来修饰变量,通常用于建立语言级别的 memory barrier.这是 BS 在 "The ...
- C和C++运算符 (转)
这里是C和C++语言的运算符列表.所有列出的运算符皆含纳于C++:第三个栏目里的内容也使用C来描述.应当注意的是C不支持运算符重载. 下列运算符在两个语言中都是顺序点(运算符未重载时): && ...
- ring3 dll hide
ZwQuerySystemInformation(SystemProcessInformation,SystemInformation,Length,ReturnLength); pS ...
- cocostudio导出plist文件
今天在用Armature类时用到cocostudio导出文件,由于美术的原因他使用的是中文命名法(这你敢相信),后面在导入程序中跟了下代码发现是解析plist文件有误,我就来比较正常功能文件和有错文件 ...
- html mysql special character
function html_encode(str) { var s = ""; if (str.length == 0) return ""; s = str. ...
- ByteBuffer常用方法详解
原文 http://blog.csdn.net/u012345283/article/details/38357851 缓冲区(Buffer)就是在内存中预留指定大小的存储空间用来对输入/输出(I/ ...