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 ...
随机推荐
- CSSOM View Module
就在8月份,也就是上次gf大姨妈来的时候,W3C出炉了CSSOM视图模块(CSS Object Model View)草案.CSSOM视图模块(CSSOM View Module)定义了一些 API, ...
- Seinfeld(栈模拟)
Seinfeld Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 布线问题(prime)
布线问题 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件:1.把所有 ...
- JavaScript之ClassName属性学习
在前面的style属性学习中,知道了通过style属性可以控制元素的样式,从而实现了行为层通过DOM的style属性去干预变现层显示的目地,但是这种就是不好的,而且为了实现通过DOM脚本设置的样式,你 ...
- C#文件压缩
/// <summary> /// 文件压缩 /// </summary> /// <param name="filesUrl">多个文件路径& ...
- C#将图片转化为黑白图片
最近项目需要将上传的图片转化为黑白图片 在网上找了很多资料,测试通过,上代码 using System; using System.Collections.Generic; using System. ...
- BZOJ 3170: [Tjoi 2013]松鼠聚会( sort )
题目的距离为max(|x1-x2|, |y1-y2|) (切比雪夫距离). 切比雪夫距离(x, y)->曼哈顿距离((x+y)/2, (x-y)/2) (曼哈顿(x, y)->切比雪夫(x ...
- Hadoop学习之shuffle过程
转自:http://langyu.iteye.com/blog/992916,多谢分享,学习Hadopp性能调优的可以多关注一下 Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方, ...
- javascript小练习-Firefox和低版本chrome下必须采用document方法获取到元素。
pxpx;background-color: aqua;display: none;} </style></head><body><input type=&q ...
- php基础教程笔记
php的环境搭建很简单,从网上下载wamp service 2.5,官方网址http://www.wampserver.com/,有32位和64位的,必须下载跟系统一致的版本,不然会出现奇怪的错误,这 ...