• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
youdiankun
博客园    首页    新随笔    联系   管理    订阅  订阅

△UVA10494 - If We Were a Child Again(大数和整型数的除法、取余)

If We Were a Child Again

“Oooooooooooooooh!

If I could do the easy mathematics like my school days!!

I can guarantee, that I’d not make any mistake this time!!”

Says a smart university student!!

But his teacher even smarter – “Ok! I’d assign you such projects in your software lab. Don’t be so sad.”

“Really!!” - the students feels happy. And he feels so happy that he cannot see the smile in his teacher’s face.

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

 

题目大意:一个大数和一个整型数做除法、取余。

 

 1 #include<cstdio>
 2 #include<string.h>
 3 
 4 #define maxn 2005
 5 
 6 char m[maxn];
 7 int ans[maxn];
 8 int n;
 9 
10 int main()
11 {
12     char c;
13     long long i,j,s;//题目要求0<n<2^31,2^31=2147483648,超过10^9最好用long long,这题用int过不了
14     int len,flag;
15     while(scanf("%s %c %lld",m,&c,&n) != EOF)
16     {
17         s=0;
18         len=strlen(m);
19         flag=0;
20         j=0;
21         if(strcmp(m,"0") == 0)
22         {
23             printf("0\n");
24             continue;
25         }
26         if(c=='/')
27         {
28             for(i=0;i<len;i++)
29             {
30                 s=s*10+(m[i]-'0');//大数除法也是模拟手算,//从高位开始取数
31                 if(s>=n && !flag)//找到第一个能除的位置,做标记,之后都能除
32                 {
33                     ans[j++]=s/n;//保存结果
34                     s%=n;//用余数(和下一位)再做除法(总之就是模拟手算^_^)
35                     flag=1;
36                 }
37                 else if(flag)
38                 {
39                     ans[j++]=s/n;
40                     s%=n;
41                 }
42             }
43             for(i=0;i<j;i++)
44             {
45                 printf("%d",ans[i]);
46             }
47         }
48         else if(c== '%')
49         {
50             for(i=0;i<len;i++)
51             {
52                 s=s*10+(m[i]-'0');//从高位开始取数
53                 s%=n;//每次都取余
54             }
55             printf("%lld",s);
56         }
57         printf("\n");
58     }
59     return 0;
60 }

 

posted @ 2014-04-25 10:46  youdiankun  阅读(194)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3