zoukankan      html  css  js  c++  java
  • △UVA10494

    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
    ");
    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("
    ");
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    java7或java8新特性
    反射中,Class.forName和ClassLoader区别
    &和&&的区别
    JAVA时区问题总结
    索引失效原因及解决索引失效方法
    索引失效的7种情况
    MySQL Explain详解
    java switch
    java 生成注释文档
    spring 获取配置文件的值
  • 原文地址:https://www.cnblogs.com/youdiankun/p/3688207.html
Copyright © 2011-2022 走看看