zoukankan      html  css  js  c++  java
  • 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]='';
    				break;
    			}
    			else
    				a[t++]=str[i];
    		}
    		a[t]='';
    		t=0;
    		for(j=i+2;str[j];j++)
    			b[t++]=str[j];
    		b[t]='';
    		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
    ",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("
    ");
    	}
    	return 0;
    }




     

  • 相关阅读:
    超链接导航栏案例
    css中定位功能的特性
    background的使用
    数据库单表查询
    各种算法
    面向对象能做什么和特性及嵌套
    c3算法详解
    面向对象继承
    生成器
    迭代器
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4237322.html
Copyright © 2011-2022 走看看