一道ACM题
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.Sample Input
2
1 2
112233445566778899 998877665544332211Sample Output
Case 1:
1 + 2 = 3Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
一下是C语言实现和C#实现。小弟一直在做C语言对C#不很精通以致于C写的代码和C#写的代码行数差不多,希望小弟能够抛砖引玉,请C#达人以更少的代码实现(PS:这是小弟第一次发帖以前只在论坛回帖的。。)#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#define NUMOF(c) ((c) ? (c) - '0' : 0)
#define MK_NODE(p)\
    do {\
        p = (struct node *)malloc(sizeof (struct node));\
        memset(p, 0, sizeof (*p));\
        p->s = (char *)malloc(1000);p->s1 = (char *)malloc(1000);\
        p->r = (char *)malloc(1000);\
        memset(p->s, 0, 1000);\
        memset(p->s1, 0, 1000);\
        memset(p->r, 0, 1000);\
    } while (0)
#define DES_NODE(p)\
    do {\
    free(p->s);\
    free(p->s1);\
    free(p->r);\
    free(p);\
    } while (0)
struct node {
    char *s;
    char *s1;
    char *r;
    struct node *next;
};
char *strrev(char *s)
{
    char *p, *q;
    p = q = s;
    while (*q)
        q++;
    q--;
    while ((p-q) < 0) {
(*p) ^= (*q) ^= (*p) ^= (*q);
        p++;
q--;
    }
    return s;

int main(void)

    struct node *head = NULL, *tail = NULL, *p; 
    int i = 0, n;
scanf("%d", &n); 
    while (i < n) { 
        if (!i) { 
            MK_NODE(head);
tail = head;
p = head; 
        } else { 
            MK_NODE(p);
tail->next = p;
tail = tail->next; 
        } 
        scanf("%s %s", p->s, p->s1); 
        i++; 
    } 
    p = head; 
    n = 1; 
    while(p) { 
        strrev(p->s); 
        strrev(p->s1); 
        i = 0; 
        while (p->s[i] || p->s1[i] || p->r[i]) {
            p->r[i] += NUMOF(p->s[i]) + NUMOF(p->s1[i]); 
            if (p->r[i] > 9) { 
                p->r[i+1] = p->r[i] / 10;
p->r[i] %= 10; 
            }
            p->r[i] += '0'; 
            i++;
        } 
        strrev(p->s);
strrev(p->s1);
strrev(p->r);
        printf("Case %d:\n%s + %s = %s\n", n++, p->s, p->s1, p->r); 
        if (p = p->next)
putchar('\n'); 
        DES_NODE(head); 
        head = p;
    }
    return 0; 
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication4{
    class Bignum{
        List<char> list;
        public Bignum(){ this.list = new List<char>();}
        public Bignum(string str){ this.list = new List<char>(str);}
        public void Append(List<char> lstr){ foreach (char c in lstr) this.list.Add(c);}
        public override string ToString(){
            StringBuilder sb = new StringBuilder();
            foreach (char c in this.list) sb.Append(c);
            return sb.ToString();
        }
        public static Bignum Add(Bignum lnum, Bignum rnum){
            Bignum bignum = new Bignum();
            lnum.list.Reverse(); rnum.list.Reverse();
            for (int i = 0; i < Math.Max(Math.Max(lnum.list.Count, rnum.list.Count), bignum.list.Count); i++){
                if (i < lnum.list.Count){                    
                    if (i < bignum.list.Count) bignum.list[i] += (char)(lnum.list[i] - '0');
                    else bignum.list.Add((char)(lnum.list[i] - '0'));               
                }
                if (i < rnum.list.Count){
                    if (i < bignum.list.Count) bignum.list[i] += (char)(rnum.list[i] - '0');
                    else bignum.list.Add((char)(rnum.list[i] - '0'));
                }
                if (bignum.list[i] > 9){
                    bignum.list.Add((char)(bignum.list[i] / 10));
                    bignum.list[i] %= (char)10;
                }
                bignum.list[i] += '0';
            }
            lnum.list.Reverse(); rnum.list.Reverse(); bignum.list.Reverse();
            return bignum;
        }
        public static Bignum operator +(Bignum lnum, Bignum rnum){ return Add(lnum, rnum);}
    }
    class Program{
        static void Main(string[] args){
            int n;
            string[] sarray;
            List<Bignum> numlist = new List<Bignum>();         
            n = Convert.ToInt32(Console.ReadLine());
            while ((n--) != 0){
                sarray = Console.ReadLine().Split(' ');
                numlist.Add(new Bignum(sarray[0]));
                numlist.Add(new Bignum(sarray[1]));
            }
            n = 1;
            while(numlist.Count > 0) {
                string s = (numlist.Count >= 2) ? "Case:{0}\n{1} + {2} = {3}\n" : "Case:{0}\n{1} + {2} = {3}";
                Console.WriteLine(s, n++, numlist[0], numlist[1], numlist[0] + numlist[1]);
                numlist.RemoveAt(0);
                numlist.RemoveAt(0);
            }
        }
    }
}

解决方案 »

  1.   

    大整数相加...
    ACM题(比如http://acm.pku.edu.cn/)都有时间限制的,你的C#程序可以在时间限制内计算完么?
      

  2.   

    C提交成功的(反馈过来的时间是“0MS”(也就是1MS以下)),但是C#苦于无对应语言提交,只有C C++ JAVA PASCAL 语言可以提交,所以无法得知能否提交通过。虽然我C#是新手,刚学C#不久,但我深深喜欢上了这门语言,虽然是管中窥豹但是通过这“一斑”我能看出来她是一门很漂亮的语言,为什么会没有C#语言选项愤怒ING!!!??难道C#就和ACM无缘???????
      

  3.   

    这上网站的ACM题支持C#:
    http://acm.timus.ru
      

  4.   

    另外,.NET 4.0直接支持大整数:
    http://msdn.microsoft.com/en-us/library/dd268287.aspx
      

  5.   

    谢谢大哥提醒,另还要谢谢你提供支持C#语言的ACM站点
      

  6.   

    没人改进我自己跟贴吧
    下面是改进的C语言实现#include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    #define NUMOF(c) ((c) ? (c) - '0' : 0)
    struct node {
        char s[1024];
        char s1[1024];
        char r[1024];
    } st = {{0},{0},{0}};
    char *strrev(char *s)
    {
        char *p, *q, c;
        p = q = s;
        while (*q)
            q++;
        q--;
        while ((p-q) < 0) {
            (*p) ^= (*q) ^= (*p) ^= (*q);
            p++; q--;
        }
        return s;

    int main(void)

        struct node *p = &st; 
        int i, ii = 0, n;
        scanf("%d", &n); 
        while (ii < n) { 
            memset(p->r, 0, 1000);
            memset(p->s, 0, 1000);
            memset(p->s1, 0, 1000);
            scanf("%s%s", p->s, p->s1); 
            ++ii;
            strrev(p->s); strrev(p->s1);
            i = 0; 
            while (p->s[i] || p->s1[i] || p->r[i]) {
                p->r[i] += NUMOF(p->s[i]) + NUMOF(p->s1[i]); 
                if (p->r[i] > 9) { 
                    p->r[i+1] = p->r[i] / 10; p->r[i] %= 10; 
                }
                p->r[i] += '0'; 
                i++;
            } 
            strrev(p->s); strrev(p->s1); strrev(p->r);
            printf("Case %d:\n%s + %s = %s\n", ii, p->s, p->s1, p->r); 
            if (ii != n) putchar('\n');
        }
        return 0; 
    }
      

  7.   

    以前碰到这种大数运算,很多人都是换Java去AC的,现在C#也支持大数运算了,代码很简单,一般ACM不用担心输入有错误,因此不用处理异常。不知道http://acm.timus.ru现在是否支持Framework 4.0,相信应该不用太长时间。
    BTW,Google每年的GCJ比赛,是支持C#的。要说微软在中国几所高校,推广的确实不太好,这些常年用C或Java做ACM题,培养算法能力的学生,是很难被拉入.Net阵营的,可以肯定的说,几乎全部大学出来的高手都会用C,但没几个会C#。而中国目前在ACM这一块,几乎是世界领先的,微软放弃了这些人,就等于放弃了高端市场。
    using System;
    using System.Numerics;namespace CSharpTest
    {
        class Program
        {
            public static void Main()
            {
                int testCount = int.Parse(Console.ReadLine());
                string[] values;            for (int i = 0; i < testCount; i++)
                {
                    values = Console.ReadLine().Split(' ');
                    BigInteger A = BigInteger.Parse(values[0]);
                    BigInteger B = BigInteger.Parse(values[1]);
                    Console.WriteLine("{0} + {1} = {2}", values[0], values[1], A + B);
                }
            }
        }
    }
      

  8.   


    目前(自2006年12月31日开始到现在)支持的是 Microsoft Visual C# 2008 版本 3.5.30729.1
    http://acm.timus.ru/help.aspx?topic=csharp
    Since 31 December 2006 the Timus Online Judge accepts C# solutions. Programs are compiled on the server with the Microsoft Visual C# 2008 версии 3.5.30729.1. Before 8 February 2009 the Microsoft Visual C# 2005 Compiler 8.00.50727.42 was used.
      

  9.   

    应该是自2006年12月31日开始支持C#,版本是:Microsoft Visual C# 2005 Compiler 8.00.50727.42,
    自2009年2月8日起版本是:Microsoft Visual C# 2008 版本 3.5.30729.1,
    希望在不久的将来支持C# 4.0。