参考答案
#include<stdio.h> 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<stdio.h> #include<malloc.h> 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<count; k++)//获取输入数据 { scanf("%d %d", &stu[k].id, &stu[k].score); } //printf("\n"); for(k = 0; k < count-1; k++)//用起泡排序法排序 { for(i = 0; i < count-k-1; i++) { if(stu[i+1].score < stu[i].score) { t = stu[i+1].score; stu[i+1].score = stu[i].score; stu[i].score = t; t = stu[i+1].id; stu[i+1].id = stu[i].id; stu[i].id = t; } while((stu[i].score == stu[i+1].score) && (stu[i].id > stu[i+1].id)) { t = stu[i].id; stu[i].id = stu[i+1].id; stu[i+1].id = t; } } } for(k=0; k<count; k++)//输出数据 { printf("%d %d\n", stu[k].id, stu[k].score); } //printf("\n"); } return 0; }
#include<stdio.h> #include<string.h> int odd(char str[]) { int k, i, t, counter; char temp[10]; temp[8] = '\0'; for(k=0; k<strlen(str); k++) { counter = 0; t = str[k] - 0; for(i=0; i<8; i++) { temp[i]='0'; } i = 7; while(t != 0) { temp[i] = t%2 + 48; if(temp[i] == '1') { counter++; } i--; t = t/2; } if(counter%2 == 0) { temp[0] = '1'; } printf("%s\n", temp); } return 0; } int main() { char str[105]; while(scanf("%s", str) == 1) { odd(str); } return 0; }
#include<stdio.h> #include <stdlib.h> 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<stdio.h> #include<string.h> 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; }