HDU水题(2)

 

HDU1000

这一题吧最简单了。但是很多次都没通过的原因是应该定义变量为long long  避免溢出。代码略

HDU 1001

这一题,我也莫名其妙地写了很多次每次都没算准

主要是在for 循环的时候要注意i<=n;另外是题目要求followed by a blank line.

所以输出的时候要再次换行。代码略;

HDU 1002

 

注意输出数据的形式就可以了!

 

#include <string>
#include <iostream>
using namespace std;
int main(){
    char str1[1001], str2[1001];
    int t, i, len_str1, len_str2, len_max, num = 1, k;
    cin>>t;
 getchar();
    while(t--){
        int a[1001] = {0}, b[1001] = {0}, c[1001] = {0};
        cin>>str1;
        len_str1 = strlen(str1);
        for(i = 0; i <= len_str1 - 1; ++i)
            a[i] = str1[len_str1 - 1 - i] - '0';
      cin>>str2;
        len_str2 =  strlen(str2);
        for(i = 0; i <= len_str2 - 1; ++i)
            b[i] = str2[len_str2 - 1 - i] - '0';
        if(len_str1 > len_str2)
            len_max = len_str1;
        else
            len_max = len_str2;
        k = 0;
        for(i = 0; i <= len_max - 1; ++i){
            c[i] = (a[i] + b[i] + k) % 10;
            k = (a[i] + b[i] + k) / 10;
        }
        if(k != 0)
        c[len_max] = 1;
        cout<<"Case "<<num<<":"<<endl;
        num++;
        cout<<str1<<" + "<<str2<<" = ";
        if(c[len_max] == 1)
          cout<<"1";
        for(i = len_max - 1; i >= 0; --i){
        cout<<c[i];
        }
       cout<<endl;
        if(t >= 1)
        cout<<endl;
    }
    return 0;

}

posted @ 2013-03-26 20:23  90后程序媛  阅读(192)  评论(0编辑  收藏  举报