HDU-1002以及自己无力的吐槽..

9 篇文章 0 订阅

第一次开始认真严肃的对待oj.第一个题目:正如描述,于是开始窃喜:"I have a very simple problem for you."(http://acm.hdu.edu.cn/showproblem.php?pid=1002)

试了将近10多遍,终于ac了,和网上看到的很多童鞋一样:"实在是太不细心了".

一开始的思路是没有任何问题的,一看要用到不定长度的字符串,我心里就嘀咕了,用java不是可以捡了个string封装好的class的便宜?还有那么多预留的接口,我的口水啊..

于是,我可以告诉你们,悲剧就是这么发生的.贴一下最终修改的代码吧:

import java.util.Scanner;

/*
 *	2012.5.1
 *	HDU_pid:1002
 *	not finished yet
 *	output format and the number of instances
 *	auther:Unibrighter
 */
public class Main implements Runnable
{
	String A;
	String B;
	StringBuffer result;
	Scanner scan;

	void prepareAandB()// get Stirng A and B(upsideDown)
	{
		// String C = scan.nextLine();
		// this.A = C.split(" ")[0];
		// this.B = C.split(" ")[1];
		
		//我一开始竟然蠢到用读入一整行,然后用split方法..蠢到家了..留着知耻后勇吧..
		
		// System.out.println("测试输出C"+C+";A:"+A+"B:"+B);
		this.A = scan.next();
		this.B = scan.next();
	}

	void solve()
	{

		result = new StringBuffer();//一开始把这一句放在类变量的声明中就写了..Wrong Answer!
		StringBuffer reA = (new StringBuffer(this.A)).reverse();
		StringBuffer reB = (new StringBuffer(this.B)).reverse();
		// ensure that reA.length>reB.length
		if (reA.length() < reB.length())
		{
			StringBuffer C = reA;
			reA = reB;
			reB = C;
		}
		int a, b, c, temp = 0;
		for (int i = 0; i < reB.length(); i++)
		{
			a = reA.charAt(i) - '0';
			b = reB.charAt(i) - '0';
			c = a + b + temp;
			if (c > 9)
			{
				temp = 1;
				c = c - 10;
			} else
				temp = 0;
			result.append((char) (c + '0'));
		}
		int counter = reB.length();
		//add the potential carry number
		for (int i = counter; i < reA.length(); i++)
		{
			a = reA.charAt(i) - '0';
			c = a + temp;
			if (c > 9)
			{
				temp = 1;
				c = c - 10;
			} else
				temp = 0;
			result.append((char) (c + '0'));
		}
		if (temp != 0)
			result.append('1');
		result = result.reverse();
		System.out.println(result);
		//后来又想到竟然用了两个循环..丢人啊..唉..
	}

	@Override
	public void run()
	{
		// TODO Auto-generated method stub try
		// get n
		this.scan = new Scanner(System.in);
		int n = this.scan.nextInt();
		scan.nextLine();// 此句坑爹,java api换行用scanner的方法没查出来
		//加这一句,这scanner竟然不知道从下一行开始读..而且也没找到游标换行的方法..
		for (int i = 1; i <= n; i++)
		{
			System.out.println("Case " + i + ":");
			this.prepareAandB();
			System.out.print(this.A + " + " + this.B + " = ");
			this.solve();
			if (i == n)
				break;//这里继续吐槽..我一开始就多打了一个换行,这也样也算Prsentation Wrong?!我输了..
			System.out.println();
		}

	}

	public static void main(String[] args)
	{
		new Thread(new Main()).start();
	}
}

于是我开始知道java在面对底层DS与算法时的笨拙了..以前发现真正懂的C++/C对java做底层开发表现出一脸的鄙夷与不屑..现在终于懂了..

以后DS的东西,还有oj再也不用java写了..虽说类库很强大,但是弊端也多..有尾大不掉之嫌,以后多用C/C++,这才是王道啊.

顺便吐槽:string怎么没有reverse的方法啊?还要自己去转换...还有stringbuffer里面也不封装split的方法,这是想干啥?唉..


另外:看到api上说java已经给封装了一个BigInteger 的类,不知道用这个类AC了,会不会被认可诶?


附上网上看到的C代码,诸位看官可试着自己比较一下..(囧,又在打自己的脸了..)

#include<stdio.h>
#include<string.h>
int main()
{
	char op1[1002],op2[1002];
	int c,i,j,n,m,s1[1002],s2[1002],len1,len2,count;
	count=1;
	scanf("%d",&n);
	m=n;
	while(m--)
	{
		memset(s1,0,1002*sizeof(int));
		memset(s2,0,1002*sizeof(int));
		scanf("%s",op1);
		scanf("%s",op2);
		len1=strlen(op1);
		len2=strlen(op2);
		c=0;
		for(i=len1-1;i>=0;i--)
			s1[c++]=op1[i]-'0';
		c=0;
		for(i=len2-1;i>=0;i--)
			s2[c++]=op2[i]-'0';
		for(i=0;i<1002;i++)
		{
			s1[i]+=s2[i];
			if(s1[i]>=10)
			{
				s1[i]-=10;
				s1[i+1]++;
			}
		}
		printf("Case %d:\n",count++);
		printf("%s + %s = ",op1,op2);
		for(i=1001;i>=0;i--)
			if(s1[i])
				break;
		for(j=i;j>=0;j--)
			printf("%d",s1[j]);
		printf("\n");
		if(count!=n+1)
			printf("\n");
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值