CQUPT计算机复试2015b卷代码题
搬运请注明出处
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <random>
using namespace std;
#define N 100
// insert
// void insert(int f, char str1[], char str2[])
// {
// char str[N];
// strcpy(str, str1 + f);
// strcpy(str1 + f, str2);
// strcat(str1, str);
// }
// int main(int argc, char const *argv[])
// {
// char str1[N] = "abcdefgcomputer", str2[N] = "xyz";
// int f = 4;
// insert(f, str1, str2);
// puts(str1);
// return 0;
// }
// 各位数字之和
// void adddigit(int *m, int n, int *a)
// {
// int *p, *q, x, k, sum;
// p = m;
// q = a;
// for (int i = 0; i < n; i++, p++, q++)
// {
// x = *p;
// sum = 0;
// while (x)
// {
// k = x % 10;
// sum += k;
// x /= 10;
// }
// *q = sum;
// }
// }
// int main(int argc, char const *argv[])
// {
// int n, m[50], res[50];
// do
// {
// cout << "n(<50)=";
// cin >> n;
// } while (n >= 50);
// for (int i = 0; i < n; i++)
// {
// do
// {
// cout << "m(<32767)=";
// cin >> m[i];
// } while (m[i] >= 32767);
// }
// adddigit(m, n, res);
// for (int i = 0; i < n; i++)
// {
// cout << m[i] << "->" << res[i] << endl;
// }
// return 0;
// }
// DecToBin
void DecToBin(unsigned int iDec, char pBin[])
{
int n, k, i = 31;
n = iDec;
while (i >= 0)
{
k = n % 2;
if (k == 1)
pBin[i] = '1';
else
pBin[i] = '0';
n /= 2;
i--;
}
pBin[32] = '\0';
}
int main(int argc, char const *argv[])
{
int n;
char a[32];
printf("n=");
scanf("%d", &n);
DecToBin(n, a);
puts(a);
return 0;
}
这篇文章包含三道C++编程题目,涉及字符串操作、整数各位数字之和计算以及十进制到二进制的转换。第一题是字符串插入功能;第二题计算整数数组中每个元素的各位数字之和;第三题实现将十进制数转化为二进制字符串。
113

被折叠的 条评论
为什么被折叠?



