标签:des style io ar color os sp for on
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 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).
A line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space.
110 / 100
99 % 10
2147483647 / 2147483647
2147483646 % 2147483647
1
9
1
2147483646
#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; }
标签:des style io ar color os sp for on
原文地址:http://blog.csdn.net/phytn/article/details/41490729