lightoj 1214

lightoj 1214 Large Division  (大数除法)

链接:http://www.lightoj.com/volume_showproblem.php?problem=1214

题意:给定 a, b 两个数,判断 a 是否整除 b 。(a 为 大数)

思路: 用字符串存 a ,转化为 整型数组,用除法原理进行处理,没什么难度。其中关键是负号的处理。

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 using namespace std;
 8 
 9 typedef long long LL;
10 LL b, x, y;
11 char ar[202];  // a
12 int nu[202];    //把 a 字符转化为数字
13 
14 bool option()
15 {
16     int i = 0, j;
17     int len = strlen(ar);       //a 的长度
18     if(ar[0] == '-')  len--, i = 1; //去掉 a 的‘-’号
19     for(j = 0; ar[i]; ++j, ++i)   //转化
20         nu[j] = ar[i] - '0';
21     if(b < 0)    b = -b;    //去掉 b 的负号
22     for(j = 0, y = 0; j < len; )    //循环除法
23     {
24         x = y;
25         while(x <= b && j < len)
26         {
27             x = x * 10 + nu[j];
28             j++;
29         }
30         y = x % b;
31     }
32     if(y)   return false;
33     return true;
34 }
35 
36 int main()
37 {
38     int t, i = 1;
39     //freopen("in.txt", "r", stdin);
40     cin >> t;
41     while(t--)
42     {
43         scanf("%s%lld", ar, &b);
44         printf("Case %d: ", i++);
45         puts(option() ? "divisible" : "not divisible" );
46     }
47     return 0;
48 }

 刚才又写了一遍,用的java,刚学java,求不喷:

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 /**
 5  * @author Administrator
 6  *
 7  */
 8 public class Main {
 9 
10     /**
11      * @param args
12      */
13     public static void main(String[] args) {
14         // TODO Auto-generated method stub
15         BigInteger a, b;
16         int t;
17         Scanner cin = new Scanner(System.in);
18         t = cin.nextInt();
19         for(int i = 1; i <= t; i++){
20             a = cin.nextBigInteger();
21             b = cin.nextBigInteger();
22             if(a.abs().mod(b.abs()).compareTo(BigInteger.ZERO) == 0){
23                 System.out.println("Case "+i+": divisible");
24             }
25             else {
26                 System.out.println("Case "+i+": not divisible");
27             }
28         }
29     }
30 
31 }

 

 

posted @ 2013-08-01 10:03  妮king狼  阅读(329)  评论(0编辑  收藏  举报