资源描述
1 实现两个N*N矩阵的乘法,矩阵由一维数组表示:
#include<iostream>
using namespace std;
#define size 2
int * multi(int* a, int* b, int N)
{
int i,j,k,temp;
int * c = (int *)malloc(N*N);
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
temp = i*N + j;
*(c+temp) = 0;
for(k=0;k<N;k++)
{
*(c+temp) += a[i*N + k]*b[k*N + j];
}
cout<<*(c+temp)<<" ";
}
}
return c;
}
int main()
{
int a[size*size] = {2,1,4,3};
int b[size*size] = {1,-1,3,2};
multi(a,b,size);
return 0;
}
2 找到单向链表中间那个元素,如果有两个则取前面一个
#include<iostream>
using namespace std;
struct node
{
int value;
node * next;
};
int main()
{
int val,sum,i;
sum = 0;
node * head = new node;
head->next = NULL;
node * curr = head;
node * temp;
cin>>val;
while(0!=val)
{
sum++;
curr->next = new node;
curr = curr->next;
curr->value = val;
curr->next = NULL;
cin>>val;
}
curr = head;
for(i=0;i<(sum+1)/2;i++)
{
curr = curr->next;
}
cout<<"中间的数字是:"<<curr->value<<endl;
return 0;
}
3 长度为n的整数数组,找出其中任意(n-1)个乘积最大的那一组的乘积,只能用乘法,不可以用除法
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
long GetMax(int array[], int len);
long GetMultiply(int array[], int len, int tag, int count);
int main()
{
int array[] = {0, -2, -3, -4};
printf("%ld\n", GetMax(array, 4));
return 0;
}
long GetMax(int array[], int len)
{
int positive_num = 0;
int negative_num = 0;
int zero_num = 0;
int positive_min = INT_MAX;
int negative_max = INT_MIN;
int negative_min = 0;
int count = 1;
int i;
if(!array)
{
printf("array must not be NULL\n");
exit(-1);
}
if(len < 1)
{
printf("len should be bigger than 0\n");
exit(-1);
}
for(i = 0; i < len; i++)
{
if(array[i] > 0)
{
positive_num++;
positive_min = (positive_min < array[i])? positive_min: array[i];
}
else if(array[i] == 0)
{
zero_num++;
}
else
{
negative_num++;
negative_max = (negative_max > array[i])? negative_max: array[i];
negative_min = (negative_min < array[i])? negative_min: array[i];
}
}
if(zero_num > 1) return 0;
else if(zero_num == 1)
{
if(negative_num % 2 == 1) return 0;
else
{
return GetMultiply(array, len, 0, 1);
}
}
else
{//no zero
if(negative_num % 2 == 1)
{
return GetMultiply(array, len, negative_max, 1);
}
else
{
if(positive_num > 0)
return GetMultiply(array, len, positive_min, 1);
else
return GetMultiply(array, len, negative_min, 1);
}
}
}
long GetMultiply(int array[], int len, int tag, int count)
{
long multiply = 1;
int i;
if(!array)
{
printf("array must not be NULL\n");
exit(-1);
}
if(len < 1)
{
printf("len should be bigger than 0\n");
exit(-1);
}
for(i = 0; i < len; ++i)
if(array[i] == tag && count > 0)
count--;
else multiply *= array[i];
return multiply;
}
4 输出二进制中1的个数
#include<stdio.h>
//using namespace std::;
void main()
{
int count=0;
int n,a=0,b[32];
int m=9999;
//printf("m is %b",m);
while(m)
{
n=m%2;
b[a++]=n;
m=m/2;
printf("a is %d\n",a);
}
while(a)
{
printf("%d",b[--a]);
}
//int *p;
// *p=b[1];
//printf("b is %d",*p++);
while(m){
count++;
m=m&(m-1);
// cout<<count;
printf("count is %d\n",count);
}
}
5 编写一个函数,要求输入年,月,日,时,分,秒,输出该时间的下一秒。如输入2007年12月31日23时59分59秒,则输出2008年1月1日0时0分0秒,具体程序如下:
#include <iostream>
#include <string>
using namespace std;
int ResetTime(int *year,int *month,int *date,int *hour,int *minute,int *second)
{
int dayofmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(*year<0||*month<1||*month>12||*date<1||*date>31||*hour<0||*hour>23||*minute<0||*minute>59||*second<0||*second>60)
return -1;
if(*year%400==0||*year%100==0&&*year%4==0)
dayofmonth[1]=29;
*second +=1;
if(*second>=60)
{
*second=0;
*minute +=1;
if(*minute>=60)
{
*minute=0;
*hour +=1;
if(*hour>=24)
{
*hour=0;
*date +=1;
if(*date>dayofmonth[*month-1])
{
*date=1;
*month +=1;
if(*month >12)
{
*month=1;
*year +=1;
}
}
}
}
}
cout <<*year<<' '<<*month<<' '<<*date<<' '<<*hour <<' '<<*minute<<' '<<*second;
return 1;
}
int main()
{
int y1=2004;
int m1=2;
int d1=28;
int h1=23;
int mm=59;
int se=59;
int n;
n=ResetTime(&y1,&m1,&d1,&h1,&mm,&se);
return 0;
}
6 编写一个函数,实现把C++/C程序代码中的注释去掉,并把结果返回。
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
void remove_comment(char *buf,size_t size)
{
char *p,*end,c;
char *sq_start,*sq_start;
char *lc_start,*bc_start;
size_t len;
p=buf;
end=p+size;
sq_start=NULL;
dq_start=NULL;
lc_start=NULL;
bc_start=NULL;
while(p<end)
{
c=*p;
switch(c)
{
case '\'':
if(dq_start||lc_start||bc_start)
{
p++;
continue;
}
if(sq_start==NULL)
{
sq_start=p++;
}
else
{
len=p++-sq_start;
if(len==2&&*(sq_start+1)=='//')
{
continue;
}
sq_start=NULL;
}
break;
}
}
}待续
7 一个5位数字ABCDE*4=EDCBA,这5个数字不能重复,编程求出这个是数字是多少
程序如下所示:
#include <iostream>
using namespace std;
int main()
{
for(int i=10000;i<100000;i++)
{
int j=0;
int t=i;
while(t!=0)
{
j=j*10+t%10;
t/=10;
}
if((i<<2)==j)
{
cout <<i;
break;
}
}
return 1;
}
展开阅读全文