参考答案
#include
int convert(int arr[], int t)
{
int k = 0, p = t;
if(0 == t)
{
printf("0\n");
return -1;
}
else
{
while(p != 0)
{
arr[k] = p%8;
p = p/8;
k++;
}
k--;
}
while(k >= 0)
{
printf("%d", arr[k--]);
}
printf("\n");
return 1;
}
int main()
{
int a, arr[5];
while(scanf("%d", &a) != EOF)
{
convert(arr, a);
}
return 0;
}
#include
#include
typedef struct
{
int id;
int score;
}LNode;
int main()
{
int count, k, i, t;
LNode stu[100];
while((scanf("%d", &count)) != EOF)
{
for(k = 0; k stu[i+1].id))
{
t = stu[i].id;
stu[i].id = stu[i+1].id;
stu[i+1].id = t;
}
}
}
for(k=0; k
#include
#include
int odd(char str[])
{
int k, i, t, counter;
char temp[10];
temp[8] = '\0';
for(k=0; k
#include
#include
void connectstr(char *temp, const char *str1, char const *str2)
{
int k=0, p=0;
while(str1[k] != '\0')
{
temp[p++] = str1[k++];
}
k=0;
while(str2[k] != '\0')
{
temp[p++] = str2[k++];
}
temp[p] = '\0';
}
int main()
{
char str1[101], str2[101], temp[201];
while(scanf("%s%s", str1, str2) != EOF)
{
connectstr(temp,str1,str2);
printf("%s\n", temp);
}
return 0;
}
5. 题目描述:
实现一个加法器,使其能够输出a+b的值。
输入:
输入包括两个数a和b,其中a和b的位数不超过1000位。
输出:
可能有多组测试数据,对于每组数据,
输出a+b的值。
样例输入:
2 6
10000000000000000000 10000000000000000000000000000000
样例输出:
8
10000000000010000000000000000000
参考答案
#include
#include
void addTwoNum(char *num1, char *num2, char *sum){
int len1 = strlen(num1);
int len2 = strlen(num2);
int i, j, k, carry = 0;
memset(sum, 0, 10002); //初始化数组sum,所有元素清零
//相加操作,注意这里相加的结果是从sum数组的低下标端开始存储的,所以相加完成后要反转数组
k = 0;
for(i = len1-1, j = len2-1; i >= 0 && j >= 0; i--,j--){
sum[k] = num1[i] + num2[j] - '0' + carry;
carry = 0;
//如果相加大于等于10,即有进位
if(sum[k] > '9'){
sum[k] -= 10;
carry = 1;
}
k++;
}
while(i >= 0){
sum[k] = num1[i] + carry;
carry = 0;
if(sum[k] > '9'){
sum[k] -= 10;
carry = 1;
}
k++;
i--;
}
while(j >= 0){
sum[k] = num2[j] + carry;
carry = 0;
if(sum[k] > '9'){
sum[k] -= 10;
carry = 1;
}
k++;
j--;
}
//如果最后两个数相加有进位
if(carry == 1){
sum[k++] = '1';
}
//反转结果数组
for(i = 0,j = k-1;i < j;i++,j--){
//交换sum[i]与sum[j]的值
sum[i] = sum[i] + sum[j];
sum[j] = sum[i] - sum[j];
sum[i] = sum[i] - sum[j];
}
}
int main(){
char num1[10001], num2[10001], sum[10002];
while(scanf("%s %s", num1, num2) != EOF){
addTwoNum(num1, num2, sum);
printf("%s", sum);
}
return 0;
}