c语言算法2

来源:互联网 发布:linux 查看用户数 编辑:程序博客网 时间:2024/05/29 06:55

¨  提示用户分别输入年月日,判断日期是否合法

¨  杨辉三角形

    在屏幕上显示杨辉三角形

 

       0                     1

       1                  1      1

       2               1     2     1

       3           1    3     3      1

       4       1      4     6     4     1

       5   1     5     10    10     5     1

¨  输入一个字符串,然后反序输出结果

¨  从一个目录字符串中提取文件名、扩展名和文件路径,如("C:\My Documents\Software\Test 1.00.doc")

¨  判断一个Email地址是否合法

¨  输入5名学生的信息(学号,姓名,性别,数学分数),根据学员的成绩,

1、 输出不及格学员的详细信息。

2、 求学员的总成绩和平均成绩,并统计不及格人数。

¨  编写一个程序,将输入的十进制正整数转换成址六进制整数后输出。输出其他进制呢

¨  有5个各不相同的正整数,他们的和是135,且按照从小到大的顺序,后面一个数是前面一个数的整数倍。编写程序求这5个数。

¨  编写一函数能够统计出一个字符串中字母、数字、空格和其他字符的个数,要求在主函数中输入字符并输出统计结果。

¨  已知a数组元素共5个,依次为12,10,5,3,1;b数组元素共4个,依次为:4,6,8,15。执行一个函数后产生新数组c,结果为:15,12,10,8,6,5,4,3,1。数组a,b,c的长度分别为1=5,m=4,n=9。编写这个函数以实现上述功能。

¨  编写一个程序,输入两个字符串str1 和str2,要求各串中无重复出现的字符,求两者的交集。若该交集非空,则输出结果。

¨ 编写一个程序,判定一个字符串是否是另一个字符串的子串,若是,返回子串在主串中的位置。

¨  编写一个程序,计算一个字符串中子串出现的次数。

¨ 编写一个程序,从键盘接收一个字符串,然后按照字符顺序从小到大进行排序,并删除重复的字符。

¨ 编写一个程序,将用户输入的由数字字符和非数字字符组成的字符串中的数字提取出来。例如,输入“asd123rt456fg789“,则产生的整数分别是123456789

¨  编写一个程序,用12个月份的英文名称初始化一个字符指针数组,当从键盘输入整数为1~12时,显示相应的月份名,键入其他整数时显示错误信息。

¨  作用指针数组对输入的3个整数序列(每个序列5个整数),分别从小到大进行排序,排序方法不限。

¨  编程实现字符串复制,即编写一个strcpyl函数:strcpyl(s1,s2,m,n)。功能:将S2字符串中从第M个字符位置开始共N个字符复制到S1字符串中。在主程序中设置S1=“hello”,s2=”happy new year!”,若调用strcpyl(s1,s2,7,3),则结果S1的值为”new”,要求用指针实现。



按题目顺序做的


#include "stdafx.h"

#include<cstdlib>
#include<string.h>
int _tmain(int argc, _TCHAR* argv[])
{
int array[6][6] = {0};
int i, j;

for (i = 0; i < 6; i++)
{
array[i][0] = 1;
array[i][i] = 1;
}

for (i = 2; i < 6; i++)
{
for (j = 1; j < i; j++)
{
array[i][j] = array[i - 1][j - 1] + array[i - 1][j ];
}
}

for (i = 0; i < 6; i++)
{
for (j = 0; j < 6; j++)
{
printf("%d\t",array[i][j] );
}
printf("\n");
}

system("pause");
return 0;
}


#include "stdafx.h"
#include<cstdlib>
#include<string.h>
int _tmain(int argc, _TCHAR* argv[])
{
char str[100];
gets_s(str);
int j; printf("字符串逆序是: \n");
for (j = strlen(str)-1; j >=0; j--)
{
printf(" %c",str[j] );
}

system("pause");
return 0;
}


#include "stdafx.h"
#include<cstdlib>
#include<string.h>
int main()
{
int ten, temp;//十进制
char c[100], cc = 0;
scanf_s("%d", &ten);
while (ten>0)
{
temp = ten % 6;
if (temp >= 0 && temp<10)
{
c[cc++] = temp + '0';
}

ten /= 6;
}
for (int i = cc - 1; i >= 0; i--) {
printf("%c", c[i]);
}
printf("\n");
system("pause");
return 0;
}

#include "stdafx.h"
#include<cstdlib>
#include<string.h>
int main()
{
int a, b, c, d, e;
for (a = 1; a<27; a++) {
for (b = (2 * a); b<135; b++) {
for (c = (b * 2); c<135; c++) {
for (d = (c * 2); d<135; d++) {
for (e = (d * 2); d<135; e++) {
if ((a + b + c + d + e) == 135){
printf("a=%d,b=%d,c=%d,d=%d,e=%d\n", a, b, c, d, e);
}
}
}
}
}
}
system("pause");
return 0;
}



#include "stdafx.h"
#include<cstdlib>
#include<string.h>
void TongJi(char s[])
{
int ZiMu = 0, KongGe = 0, ShuZi = 0, QiTa = 0, i;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] == 32)KongGe++;
else if ((s[i] >= 48) && (s[i] <= 57))ShuZi++;
else if (((s[i] >= 97) && (s[i] <= 122)) || ((s[i] >= 65) && (s[i] <= 90)))ZiMu++;
else QiTa++;
}
printf("空格:%d;数字:%d;字母:%d;其他:%d。\n", KongGe, ShuZi, ZiMu, QiTa);
}

int main()
{
char s[100];

gets_s(s);
TongJi(s);
system("pause");
return 0;
}



#include "stdafx.h"
#include<cstdlib>
#include<string.h>

int _tmain(int argc, _TCHAR* argv[])
{

int intc[9] = {12, 10, 5, 3, 1, 4, 6, 8, 15 };

int max;
for (int i = 0; i < 8; i++)//冒泡排序,一轮找到最大的一个放到最后面,so,9轮
{
for (int j = 0; j < 9-1-i; j++)//当前这一轮,找到最大的,移到最后面,最后界限小心越界
{
if (intc[j]>intc[j + 1])//22相邻的2个比较,大的放后面。一个一个的比,移,直到最后那个一定是最大的,也放到了最后面
{
max = intc[j];
intc[j] = intc[j+1];
intc[j + 1] = max;
}
}
}

for (int k = 8; k>=0; k--)//冒泡排序,一轮找到最大的一个放到最后面,so,9
printf("%d,",intc[k]);

system("pause");

return 0;
}

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include<cstdlib>
int cmpsubstr(char a[81], char b[81])
{
int i, j, flag = -1;
for (i = 0; i <= (strlen(a) - strlen(b)); i++)
{
flag = i;
for (j = 0; j<strlen(b); j++)
if (b[j] != a[i + j])
break;
if (j == strlen(b))
return flag;
}
return -1;
}

int main()
{
char a[81], b[81];
int n;
printf("输入字符串a:");
gets_s(a);
printf("输入字符串b:");
gets_s(b);
if (strlen(a) >= strlen(b))
{
n = cmpsubstr(a, b);
if (n != -1)
printf("b是a的子串,位置从a[%d]开始\n", n);
else
printf("b不是a的子串");
}
else
{
n = cmpsubstr(b, a);
if (n != -1)
printf("a是b的子串,位置从b[%d]开始\n", n);
else
printf("a不是b的子串");
}
system("pause");

return 0;
}



#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include<cstdlib>
#define N 100
void main()
{
int i, j, k, m = 1;
char array[N], temp;
printf("请输入字符串");
gets_s(array);

for (i = 0; i<strlen(array); i++)
{
for (j = 0; j<strlen(array)-1-i; j++)
{

if (array[j]>array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
for (j = 0; j < strlen(array); j++)
if (array[j] == array[j + 1])
array[j] = ' ';


puts(array);
system("pause");
}


#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include<cstdlib>
int main()
{
char a[1000];
int i = 0;
printf("输入字符串:\n");
gets_s(a);
printf("输出结果:\n");
while (a[i])
{
if (a[i] >= '0'&&a[i] <= '9')
printf("%c", a[i]);
if (a[i] >= '0'&&a[i] <= '9' && (a[i + 1]<'0' || a[i + 1]>'9')) //如果是最后一位数字,则换行
printf("\n");
i++;
}
system("pause");
return 0;

}


#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include<cstdlib>
int main()
{
char *pMonth[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
int n;
printf("请输入月份:");
scanf_s("%d", &n);
if (n < 1 || n > 12)
{
printf("月份输入错误!\n");
}
else
{
printf("%s", *(pMonth + n - 1));
}
system("pause");
return 0;
}



#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 5
void bubble_sort(int *p)
{
 int i = 0, j = 0;
 int tmp = 0;
 for (i = 3 * ARRAY_SIZE - 1; i >= 0; i--)
 {
  for (j = 0; j < i; j++)
  {
   if (*(p + j) > *(p + j + 1))
   {
    tmp = *(p + j);
    *(p + j) = *(p + j + 1);
    *(p + j + 1) = tmp;
   }
  }
 }
}
int main()
{
 int i = 0, j = 0, k = 0;
 int a[ARRAY_SIZE] = {0};
 int b[ARRAY_SIZE] = {0};
 int c[ARRAY_SIZE] = {0};
 int *array_p[3] = {a, b, c};
 int *p = (int *)malloc(3 * ARRAY_SIZE * sizeof (int));
 printf("Please input 1st array: \n");
 for (i = 0; i < ARRAY_SIZE; i++)
 {
  scanf("%d", array_p[0] + i);
  *(p + i) = *(array_p[0] + i);
 }
 printf("Please input 2nd array: \n");
 for (j = 0; j < ARRAY_SIZE; j++)
 {
  scanf("%d", array_p[1] + j);
  *(p + ARRAY_SIZE + j) = *(array_p[1] + j);
 }
 printf("Please input 3rd array: \n");
 for (k = 0; k < ARRAY_SIZE; k++)
 {
  scanf("%d", array_p[2] + k);
  *(p + 2 * ARRAY_SIZE + k) = *(array_p[2] + k);
 }
 
 bubble_sort(p);
 for (i = 0; i < 3 * ARRAY_SIZE; i++)
 {
  printf("%d, ", *(p + i));
 }
 printf("\n");
 free(p);
 return 0;
}

原创粉丝点击