acm_hdu Problem Archive 1002

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void add(char *a, char *b,char *sum)//字符串a要比字符串b长,或相等
{
int len_a = strlen(a);
int len_b = strlen(b);
char tmp[len_a];
int offset = len_a-len_b;
strncpy(tmp+offset,b,len_b);//后面len_b长度复制,前面offset长度填充字符‘0’
for (int i=0; i<offset; i++)
tmp[i]=‘0‘;
//tmp[len_a] = ‘\0‘;
//printf("tmp=%s\n", tmp);

int flag = 0;
int top = 0;
for (int i=len_a-1; i>=0; i--)
{
int res = (a[i]-48) + (tmp[i]-48) + flag;
if (res >= 10)
{
flag = 1;
res -= 10;
}
else
flag = 0;

sum[i] = res+48;
if (i == 0)//设置最高位是1还是0,并作为返回值,控制main函数中是否打印出来
{
if (flag == 1)
top = 1;
else
top = 0;
}
}
sum[len_a] = ‘\0‘;
if (top == 1)//最高位为1打印出来,否则不打印
printf("%d%s\n",top,sum);
else
printf("%s\n",sum);
}

int main()
{
int n = 0;
scanf("%d ",&n);//%d后面的空格不要丢了,要不然enter会被gets函数(如果用)读入。
//控制串的空白字符使在输入流中跳过一个或多个空白字符.
for (int i=0; i<n; i++)
{
char a[1000];
char b[1000];
char sum[1000];
scanf("%s %s",a,b);//scanf自动在a,b结尾加‘\0‘,若输入把a,b塞满了则NUL在数组外面+1位置
//printf("a=%s\nb=%s\n", a,b);

printf("Case %d:\n",i+1);
printf("%s + %s = ",a,b);
int len_a = strlen(a);
int len_b = strlen(b);
if (len_a >= len_b)
add(a,b,sum);
else
add(b,a,sum);
if (i<n-1)
printf("\n");
}
return 0;
}

时间: 2024-08-03 00:18:43

acm_hdu Problem Archive 1002的相关文章

acm_hdu Problem Archive 1004

#include <stdio.h>#include <stdlib.h>#include <string.h> int main(){ int n = 0; while (scanf("%d",&n) != EOF && n!= 0) { char str[1000][16];//只对各不相同的颜色进行存储 memset(str,0,sizeof(str));//all set to be NUL char tmp[16];

DP 动态规划 Problem B 1002 求最长上升子序列的长度

Problem B  ID:1002 简单题意:给出X和Z两个字符串,求最长上升子序列的长度. 解题思路形成过程:利用矩阵.X字符串中的各字符依次作为行标,Z字符串中的各字符依次作为列标. 从第一行第一列开始逐行遍历:如果当前位置对应的两个字符相同,则在这个位置记录"前一行前一列"的对应的数+1: 如果当前位置对应的两个字符不同,则在这个位置记录"此行前一列"和"此列前一行"对应的两个数的最大值. 遍历结束后,最后一行最后一列获得的数便是最长上升

Problem Archive #1 题解2

接着上一次的题解接着写 E题,水题,The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with xx steps, she w

字符串专题:map POJ 1002

第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser]++ 自动存donser到map并且值加一,如果发现重复元素不新建直接加一, map第一个参数是key,默认字典序升序排列key map<string,int>::iterator ii;         map的迭代器 for(ii=outputer.begin();ii!=outputer.end(

最左/右边的数-hd1060/1061

左边: http://acm.hdu.edu.cn/showproblem.php?pid=1060 1 #include <iostream> 2 #include <math.h> 3 using namespace std; 4 5 int main() 6 { 7 int n,m,d,i; 8 double l; 9 while(cin>>n) 10 11 { 12 for(i=1;i<=n;i++) 13 { 14 cin>>m; 15 l=

【编程练习】电话本号码转换加排序

功能描述:http://poj.org/problem?id=1002 解决关键: 1)使用数值数组代替二维数组--数组角标表示排序的号码,元素的值记录出现的次数:利用数值的天然大小顺序直接按顺序记录标准化后的号码. 2)至于格式问题,不一定拘泥于将格式写入字符串,直接控制格式化字符串. PS.因为号码不会变化且唯一,所以可以这样使用--如果用出现次数(变化的量)排序就不这么方便了. #include<stdio.h> #include<string.h> #include<

poj1002-487-3279

http://poj.org/problem?id=1002 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 288073   Accepted: 51697 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it

487-3279 (poj1002)

http://poj.org/problem?id=1002 这道题耗时一整天,终于搞定了.这道题一开始写的是超时,最后懂了,问题原因是有二重循环加strcmp比较,时间复杂度是O(n^3)所以超时了. #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<vector> using namespace std; char PanC

[C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ

前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的“Problem Archive”,然后从众多题目中选择一个进行读题.构思.编程.然后提交.最后查看题解状态,如果AC了表示这一题被攻克了,否则就要重做了~一般情况下,“刷题”要求精神高度集中且经验丰富,否则很难成功AC,有时候甚至做一题要浪费半天的时间!(有时网速卡了,比抢火车票还要急!) 楼主在