500 强面试题

/*
已知有N个无规律的正整数,请编程序求出其中的素数并打印出能被5整除的数之积
*/
/*
#define  _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
   
bool isPrime(int number)
{ 
 for (int i = 2; i < number; i++)
 if (!(number % i))
  return false;
 return true;
}   
  
void main()
{
 int multi_sum = 1,m = 0;
 int a[1024] = { 0 }; int i = 0;
 printf("Please input the number end with 0:\n");
 while (scanf("%d",&m),m)
 {
  if (isPrime(m))
   a[i++] = m;
  if (!(m % 5))
   multi_sum *= m;
 }
 for (int j = 0; j < i;j++)
 if (a[j])
  printf("%-4d", a[j]);
 printf("\n%d\n", multi_sum);
 system("pause");
}*/
/*
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void f500241(int *p)//判断是否为质数
{
 for (int i = 0; i < 20; i++)
 {
  int flag = 1;
  for (int j = 2; j < p[i] - 1; j++)
  {
   if (p[i] % j == 0)
   {
    flag = 0;
    break;
   }
  }
  if (flag == 1)
  {
   printf("%d\t", p[i]);
  }
 }
}
void f500242(int *p)//判断能否为5整除,能整除的求出相乘之积
{
 int sum = 1;
 for (int i = 0; i < 20; i++)
 {
  if (p[i] % 5 != 0)
  {
   continue;
  }
  else
  {
   sum *= p[i];
  }
 }
 printf("sum=%d", sum);
}
void main()
{
 int a[20];
 for (int i = 0; i < 20; i++)//分配随机整数
 {
  a[i] = rand() % 100 + 10;
  printf("%d\t", a[i]);
 }
 printf("\n\n");
 f500241(a);
 printf("\n\n");
 f500242(a);
 system("pause");
}*/
/*
Bessel函数Jn(X)有以下的递推关系:
J[n+1](x)=(2n+1)/x*J[n](x)-J[n-1](x)
并且已知:J[0](x)=sinx/x
J[1](x)=sinx/x^2-cosx/x
编写程序,利用递推关系,由任意的n和x≠0求Jn(X)。
注:本程序不允许使用数组。
贝塞尔曲线:Cocos中使用
*/
/*
#define  _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
float Bessel(float x, int n)
{
 if (n == 1)
  return sin(x) / x;
 else if (n == 2)
  return sin(x) / powf(x, 2) - cos(x) / x;
 else
  return  (2 * n + 1) / x*Bessel(x, n - 1) - Bessel(x, n - 2);
}
void main()
{
 int number = 0;
 printf("\ninput number end with 0\n");
 
 while (scanf("%d",&number),number)
 {
  printf("%f\n", Bessel(number, 3));
 }
 system("pause");
}*/
/*
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double Bessel(int n, double x);    //Bessel函数原型
void main()
{
 int n;
 scanf_s("%d", &n);
 double x;
 scanf_s("%lf", &x);
 double result = 0;
 if (x != 0)        //判断x是否为零,x=0函数趋于无穷,x!=0则进行计算函数
 {
  result = Bessel(n, x);
  printf("J[n](x) = %f\n", result);
 }
 else
 {
  printf("The function of bessel's is went to ∞ !");
 }
 system("pause");
}
double Bessel(int n, double x)
{
 if (n == 1)      //结束条件1
 {
  return sin(x) / x;
 }
 else if (n == 2)    //结束条件2
 {
  return sin(x) / (x*x) - cos(x) / x;
 }
 else
 {
  return (2 * n + 1) / x * Bessel(n - 1, x) - Bessel(n - 2, x);   //递归条件
 }
}*/
/*
把一个偶数位的数从当中分开成为两个数,
这两个数的和的平方等于原数。
如(8 + 1)2 = 81,(20 + 25)2 = 2025。
求10至9999之间满足这样条件的数是哪些 ? 
共有多少个 ?
*/
/*
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
void countN(int number, int &count)
{
 int num_1 = 0, num_2 = 0;
 if (number < 100)
 {
  num_1 = number / 10;
  num_2 = number % 10;
 }
 else if (number > 999)
 {
  num_1 = number / 100;
  num_2 = number % 100;
 }//
 if ((num_1 + num_2)*(num_1 + num_2) == number)
 {
  count++;
  printf("%d\t(%d+%d)^2 == %d\n", number,num_1,num_2,number);
 }
}
void main()
{
 int i = 0;
 int count = 0;
 for (i = 10; i < 100; i++)
  countN(i, count);
 for (i = 1000; i <= 9999; i++)
  countN(i, count);
 printf("%d\n", count);
 system("pause");
}*/
/*
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
//返回数字位数
int GWS26(int num)
{
 int count = 0;
 while (num)
 {
  count++;
  num /= 10;
 }
 return count;
}
bool IsSpecal26(int num)
{
 if (GWS26(num) % 2)
 {
  return false;
 }
 else
 {
  int temp = 1;
  for (int i = 0; i < GWS26(num) / 2; i++)
  {
   temp *= 10;
  }
  if (num == (num%temp + num / temp)*(num%temp + num / temp))
   return true;
  else
   return false;
 }
}
void main()
{
 int num = 0;
 for (int i = 10; i <= 9999; i++)
 {
  if (IsSpecal26(i))
  {
   printf("%5d", i);
   num++;
  }
 }
 printf("\n这些数字共有%d", num);
 system("pause");
}
*/
/*
计算Yn的值,直到|Yn - Yn-1|<10-6为止,并打印出此时共作了多少次COS计算。
提示:Yn+1=COS(Yn),故本题适用于使用迭代法。
*/
/*
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
float y(float x, int n)
{
 if (n == 1)
  return cos(x);
 else
  return cos(y(x, n - 1));
}
void main()
{
 float x = 1.0;
 int n = 2;
 while (abs(y(x,n) - y(x,n-1)) >= powf(10.0,-6))
 {
  n++;
 }
 printf("%d\n", n+2);
 system("pause");
}
*/

/*
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int f(double m, int count)
{
 if (abs(cos(m) - m) < pow(10.0, -6))
 {
  return count + 1;
 }
 else
 {
  return f(cos(m), ++count);
 }
}
void main()
{
 int n = f(1, 3);
 printf("n = %d\n", n);
 system("pause");
}*/
/*
已知有9个数
请求出这些数中的最大值、最小值及平均值
以及有多少个数等于平均值 
*/
/*
#define  _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
bool calc(int *a, int n, int &max, int &min, int &ave, int &n_a)
{
 bool ret = true;
 if (a == NULL)
 {
  ret = false;
  printf("a == NULL Err\n");
  return ret;
 }
 int i = 0,sum= 0;
 for (i = 0; i < n; i++)
 {
  if (max < a[i])
   max = a[i];
  if (min > a[i])
   min = a[i];
  sum += a[i];
 }
 ave = sum / n;
 for (i = 0; i < n;i++)
 if (ave == a[i])
  n_a++;
 return ret;
}
void main()
{
 //calc(int *a, int n, int &max, int &min, int &ave, int &n_a)
 int a[9] = { 0 };
 int max = 0, min = 1000, ave = 0, n_a = 0,i = 0;
 printf("input nine number\n");
 for (i = 0; i < 9; i++)
  scanf("%d", a+i);
 if (calc(a, 9, max, min, ave, n_a))
  printf("max = %d\nmin = %d\nave = %d\nnum = %d\n", max, min, ave, n_a);
 system("pause");
}*/
/*
#include<stdio.h>
#include <stdlib.h>
void f50028(int *p)
{
 int max = p[0];
 int min = p[0];
 int sum = 0;
 float average = 0;
 int count = 0;
 for (int i = 0; i<9; i++)
 {
  if (min>p[i])
  {
   min = p[i];
  }
  if (max < p[i])
  {
   max = p[i];
  }
  sum += p[i];
 }
 average = sum / 9.0;
 printf("\nmin=%d,max=%d,average=%f", min, max, average);
}
void main()
{
 int a[9];
 for (int i = 0; i < 9; i++)//分配随机整数
 {
  a[i] = rand() % 100 + 10;
  printf("%d\t", a[i]);
 }
 f50028(a);
 system("pause");
}*/
/*
求二维数组中每行元素的平均值,不许引入其它的数组。
*/
/*
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
void main()
{
 int a[10][10] = { 0 };
 for (int i = 0; i < 10;i++)
 for (int j = 0; j < 10; j++)
 {
  a[i][j] = rand() % 100 + 10;
  printf("%4d", a[i][j]);
  if (j == 9)
   printf("\n");
 }

 int j = 0;
 for (int i = 0; i < 10; i++)
 {
  int sum = 0;
  for (j = 0; j < 10; j++)
   sum += a[i][j];
  a[i][j - 10] = sum / 10;
  printf("ave = %d\n", a[i][j - 10]);
 }
 system("pause");
}*/
/*
#include<stdio.h>
#include<stdlib.h>
#define N 5
void getArray(int a[][N], int n);      //给二位数组随机赋值原型
void putArray(int a[][N], int n);      //打印出二维数组原型
void main()
{
 int a[N][N] = { 0 };
 getArray(a, N);       //给数组赋值
 putArray(a, N);       //打印数组
 for (int i = 0; i < N; i++)
 {
  int sum = 0;      //第i行和,初始化为0
  for (int j = 0; j < N; j++)
  {
   sum += a[i][j];     //将第i行元素加给sum
  }
  printf("In %dth row! The average is %f  \n", i, sum / 5.0);  //打印结果,没用其他数组哦!!!!
 }
 system("pause");
}
void putArray(int a[][N], int n)     //打印二维数组
{
 for (int i = 0; i < n; i++)
 {
  for (int j = 0; j < N; j++)
  {
   printf("%-4d", a[i][j]);
  }
  printf("\n");
 }
}
void getArray(int a[][N], int n)      //给二维数组随机赋值
{
 for (int i = 0; i < n; i++)
 {
  for (int j = 0; j < N; j++)
  {
   a[i][j] = rand() % 90 + 10;
  }
 }
}*/
/*
编程序计算函数
P(x) = b0xn + b1xn - 1 + ... + bn - 1x + bn的值。
要求先将X的各项系数先输入到数组B中,
然后再用循环结构求P(X)的值
*/
/*
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#define n 10
void main()
{ 
 int B[n + 1] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 float px = 0;
 int x = 2;
 for (int i = n; i >= 0; i--)
  px += powf(x, i)*B[n - i];
 printf("%f\n", px);
 system("pause");
}*/
/*
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
//返回数字位数
const int N = 10;
int PHS30(int *brr, int x)
{
 int sum = 0;
 for (int i = 0; i < N; i++)
 {
  sum += brr[i] * (int)pow((double)x, N - i);
 }
 return sum;
}
void main()
{
 int brr[N] = { 0 };
 int x = 0;
 printf("请输入10个整数最为数组b的值\n");
 for (int i = 0; i < N; i++)
 {
  scanf("%d", &brr[i]);
 }
 printf("请输入x");
 scanf("%d", &x);
 printf(" \n函数结果为:%d", PHS30(brr, x));
 system("pause");
}*/
/*
由随机函数产生一个1至1000之间的整数,让人猜之。
计算机仅回答人猜的数大、小还是相等
当人猜对时,由计算机打印出人一共猜了几次。
*/
/*
#define  _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
void main()
{
 int number = rand() % 1000 + 1;//     1<= number<=1000
 int suppose = 0,count= 0;
 printf("input an number");
 
 while (1)
 {
  scanf("%d", &suppose);
  if (suppose == number)
  {
   count++;
   printf("%d\n", count);
  }
  else if (suppose < number)
  {
   count++;
   printf("input a larger number\n");
  }
  else
  {
   count++;
   printf("input  a smaller number\n");
  }
 }
 printf("%d\n", count);
 system("pause");
}
*/
/*
编程序将一个正整数写成其它两个正整数的平方和,若不能成立时输出“NO”。例如
5 = 1^2 + 2^2  , 25 = 3^2 + 4^2
*/
/*
#define  _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
void main()
{ 
 int a[101] = { 0 };
 
 for (int i = 1; i <= 100; i++)
 {
  a[i] = i;
  a[i] = a[i] * a[i];
 }//get pow

 printf("input a number and end with 0:");
 int number = 0;
 while (scanf("%d",&number),number)
 {
  static int i = 0,j = 0;
  for (i = 1; i <= 100;i++)
  for (j = 1; j <= i;j++)
  if (a[i] + a[j] != number)
   continue;
  else
   printf("%d = %d+%d\n", number, a[j], a[i]);
  if (j==100 && i ==100)
  {
   printf("NO\n");
  }
 }
 system("pause");
}
*/
这里呢;字数有了限制。所以就贴两个有趣的两个数字之间的交换代码~~ 
#include "stdio.h"
#include "stdlib.h"

void main()
{
 int a = 15;
 int b = 240;
 printf("%x,%x\n", a, b);
/*
 {
  a ^= b;
  b ^= a;
  a ^= b;
 }
*/
       
/*
 {
  a = a + b;
  b = a - b;
  a = a - b;
 }
*/
 printf("%x,%x\n", a,b);
 system("pause");
}

转载于:https://my.oschina.net/sunnyandjj/blog/291713

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值