,m);
output_array[j++]=m;
s=p;
q->next=p->next;
p=p->next;
free(s);
s=NULL;
}
m=p->num;
printf(“%5d\n”,m);
output_array[j]=p->num;
k=j;
for(j=0 ; j<=k; j++)
{
printf("%5d",output_array[j]);
}
printf("\n");
}
int main()
{
int input_array[]={3,1,2,4};
int len=4;
int m=7;
array_iterate(len, input_array, m);
return 0;
}
问题:比较一个数组的元素 是否为回文数组
答案:
#include
#include
void huiwen(char str[])
{
int i,len,k=1;
len=strlen(str);
for(i=0;i
{
if(str[i]!=str[len-i-1])
{
k=1;
break;
}
}
if(k==0)
printf("%s 不是一个回文数\n",str);
else
printf("%s 是一个回文数\n",str);
}
main()
{
char str[100] = {0};
int i;
int len;
printf("Input a string:"); /*提示输入Input a string:*/
scanf("%s", str); /*scan()函数输入一个字符串:*/
huiwen(str);
return 0;
}
选秀节目打分,分为专家评委和大众评委,score[] 数组里面存储每个评委打的分数,
judge_type[] 里存储与 score[] 数组对应的评委类别,judge_type[i] == 1,表示专家评委,
judge_type[i] == 2,表示大众评委,n 表示评委总数。打分规则如下:专家评委和大众评委
的分数先分别取一个平均分(平均分取整),然后,总分 = 专家评委平均分 * 0.6 + 大众
评委 * 0.4,总分取整。如果没有大众评委,则 总分 = 专家评委平均分,总分取整。函数
最终返回选手得分。
函数接口 int cal_score(int score[], int judge_type[], int n)
答案:
#include "iostream"
using namespace std;
int cal_score(int score[], int judge_type[], int n)
{
if(NULL==score||NULL==judge_type||0==n) return 0;
int sum=0;
int sum1=0,count1=0;
int sum2=0,count2=0;
for(int i=0;i
{
if (judge_type[i]==1)
{
sum1=sum1+score[i];
count1++;
}
else
{
sum2=sum2+score[i];
count2++;
}
}
if(0==count2) sum=sum1/count1;
else sum=(sum1/count1)*0.6+(sum2/count2)*0.4;
return sum;
}
void main()
{
int score[3]={12,13,15};
int judge_type[3]={1,1,2};
printf("%d\n",cal_score(score, judge_type, 3) );
}
问题:给定一个数组 input[] ,如果数组长度 n 为奇数,则将数组中最大的元素放到 output[] 数组最中间的位置,如果数组长度 n 为偶数,则将数组中最大的元素放到 output[] 数组中间两个位置偏右的那个位置上,然后再按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。
例如:input[] = {3, 6, 1, 9, 7} output[] = {3, 7, 9, 6,
input[] = {3, 6, 1, 9, 7, 8} output[] = {1, 6, 8, 9, 7, 3}
函数接口 void sort(int input[], int n, int output[])
答案:
#include "iostream"
using namespace std;
void bubblesort(int data[],int n)
{
int temp=0;
for(int i=0;i
{
for(int j=i+1;j
{
if (data[i]
{
temp=data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
}
void sort(int input[], int n, int output[]) { int *sort_input=new int[n]; for(int i=0;i
{ sort_input[i]=input[i]; } bubblesort(sort_input,n); if(1==n%2) { int mid=n/2; int k=0; output[mid]=sort_input[k++]; for(int j=1;j<=n/2;j++) { output[mid-j]=sort_input[k++]; output[mid+j]=sort_input[k++]; } } else { int mid=n/2; int k=0; output[mid]=sort_input[k++]; for(int j=1;j
{ output[mid-j]=sort_input[k++]; output[mid+j]=sort_input[k++]; } output[0]=sort_input[k++];
}
delete sort_input; } void main() { int input1[] = {3, 6, 1, 9, 7}; int output1[5]; memset(output1,0,5*sizeof(int)); int input2[] = {3, 6, 1, 9, 7, 8} ; int output2[6]; memset(output2,0,6*sizeof(int)); sort(input1, 5, output1); sort(input2, 6, output2); for(int k=0;k<5;k++) printf("%d",output1[k]); printf("\n"); for(k=0;k<6;k++) printf("%d",output2[k]); printf("\n"); } 删除字符串中所有给定的子串问题描述:在给定字符串中查找所有特定子串并删除,如果没有找到相应子串,则不作任何操作。 要求实现函数: int delete_sub_str(const char *str, const char *sub_str, char *result_str) 【输入】 str:输入的被操作字符串 sub_str:需要查找并删除的特定子字符串 【输出】 result_str:在str字符串中删除所有 sub_str子字符串后的结果 【返回】 删除的子字符串的个数 答案: #include
#include
int delete_sub_str(const char *str, const char *sub_str, char *result_str)
{
int count=0;
int k=0,j=0;
in |