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;
}
时间: 2024-10-11 08:32:21

If We Were a Child Again的相关文章

加压压缩文件报错gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now

压缩包是直接weget 后面加官网上的tar包地址获取的 [[email protected] ~]# tar -zxvf /home/hadoop/hadoop-2.6.5-src.tar.gz gzip: stdin: not in gzip formattar: Child returned status 1tar: Error is not recoverable: exiting now[[email protected] ~]# tar -xvf /home/hadoop/hadoo

out of memory kill process (java) score or a child

最近在跑大数据,发现 out of memory kill process (java) score or a child,查资料发现是操作系统linux low mem 太低,后来加大内存一样不管用,开始分析代码: 将代码中的String 字符串拼接改成StringBuilder(单线程速度比StringBuffer快) 将获取文件名称的方法file.listFiles() 改成 file.list() 将代码中的不必要的创建对象.数组开销去掉,尤其是在循环里 发现仍然有问题,通过free -

LINUX:解压问题tar: Child returned status

解压某个文件时 #tar -zxvf xxxxx.tar.gz 出现下面的错误提示: gzip: stdin: not in gzip formattar: Child returned status 1tar: Error exit delayed from previous errors You used "tar -zxvf" for the second command. the 'z' option tells tar to use gzip to uncompress th

Java tomcat启动失败(Servlet3.0 Web Project):A child container failed during start

Tomcat启动失败,失败全部信息: 五月 11, 2016 10:21:04 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:MyEL' did not find a matching prop

tar (child): jdk-7u71-linux-x64.tar.gz:无法 open: 没有那个文件或目录

1 错误描述 [email protected]:~$ sudo mkdir /usr/lib/jvm [sudo] password for youhaidong: [email protected]:~$ sudo tar zxvf jdk-7u71-linux-x64.tar.gz -C /usr/lib/jvm tar (child): Error is not recoverable: exiting now tar: Child returned status 2 tar: Erro

[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException] The child node &quot;db_driver&quot; at path &quot;fos_user&quot; must be configured.

$ php bin/console server:run [Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]  The child node "db_driver" at path "fos_user" must be configured. 必须新建一个USer类

解压.tar.gz出错gzip: stdin: not in gzip format tar: /Child returned status 1 tar: Error is not recoverable: exiting now

先查看文件真正的属性是什么? [[email protected] ~]# tar -zxvf tcl8.4.16-src.tar.gz gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now [[email protected] ~]# file tcl8.4.16-src.tar.gz tcl8.4.16-src.tar.gz: HTML d

Linux parent process and child process when &#39;sudo&#39;

如果在一般用户下如user,执行sudo命令,会产生两个进程. ps -ef | grep Container root 4305 643 0 16:37 pts/39 00:00:00 sudo ./ContainerCompilation.shroot 4306 4305 0 16:37 pts/39 00:00:00 /bin/bash ./ContainerCompilation.sh 4306进程的父进程是4305,也就是说unix先生成4305进程将命令交给root,然后产生4306

bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因

这个异常的出现往往是因为非法使用了某些方法引起的. 从字面意思上是说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容.这里很明显这个child是一个View,一个子(child)View必须依赖于父(parent)View,如果你要使用这个child,则必须通过parent,而你如果就是硬想使用这个child,那么就得让这个child与parent脱离父子关系(即removeView()) 何时会出现这种异常呢,典型的

A child container failed during start 解决方案

症状:A child container failed during start 适用问题描述:tomcat挂空可以正常运行,载入项目时挂掉. 相关操作:之前为了省事,由于两个servlet功能类似所以,干脆copy A to B结果莫名其妙挂掉. 百度垃圾一逼:推荐谷歌 https://www.yundou.info 无论怎么百度都无法解决,最后从实际出发,研究了报错原因,错误指向servlet map 有问题. 深入琢磨:结论 @WebServlet("/ServletName")