11.25练习
来源:互联网 发布:森林防火预警软件 编辑:程序博客网 时间:2024/06/15 17:19
1、如输入:Z2009-AShanghaiZ
则输出:lenth = 8;
事例说明:从A到Z的子串为SHanghai,其长度是8.
#include <stdio.h>#include <string.h>int caculate(char string[], char s1, char s2){ int length = 0; while(*string != s1) string++;//从首字母开始 while(*string != s2) { string++;//两个特定字母之间的长度 length++; } return length-1;}int main(void){ int length = 0; char s1,s2; char *string = "Z2009_AShanghaiZ"; printf("Enter two char to count length"); scanf("%c%c", &s1, &s2); length = caculate(string, s1, s2); printf("%c to %c length is %d", s1, s2, length); return 0;}
3、实现整数n(n<=10)的算式:n!+(n-1)!+(n-2)!…+3!+2!+1!
输入 n = 3:结果 3!+2!+1! = 11
函数原型:int jiecheng_sum(int n)
#include <stdio.h>int jiecheng_sum(int n){ int i = 0, j = 0, k = 1, sum = 0; for(i=1; i<n+1; i++)//从1到n { k = 1; for(j=1; j<i+1; j++) { k = k * j;//计算从1到n的阶乘 } sum += k; //相加 } return sum;}int main(void){ int n = 0; if(scanf("%d", &n) == 1 && (n<=10 && n>=1)) { printf("\nsum = %d.\n", jiecheng_sum); } return 0;}
3.一个数组有N个元素,使用冒泡排序法对其进行排序输出
输入为两行。
第一行一个整数n(1=
#include <stdio.h>void maopao(int arr[], int n){ int i = 0, j = 0, k = 0; for(i=0; i<n-1; i++) { for(j=0; j<n-i-1; j++) { if(a[j]>a[j+1]) { k = a[j]; a[j] = a[j+1];//冒泡交换 a[j+1] = k; } } }}int main(void){ int n = 0; int a[1000]; int i = 0; scanf("%d", &n); if(n>=1 && n<=1000) { for(i=0; i<n; i++) scanf("%d", &a[i]); maopao(a,n); for(i=0; i<n; i++) { if(i == 0) printf("%d", a[i]); else printf(" %d", a[i]); } } return 0;}
4.写一个函数找出一个整数数组中第二大的数。
PS1: 66 66 66 66 //无第二大的数字
PS2: 99 99 88 86 // 第二大的数字是88
#include <stdio.h>int findsec(int a[], int n, int *p){ int i = 0, max = 0, semax = 0; int find = 0; max = a[0]; for(i=1; i<n; i++) { if(a[i] > max) { *p = max; max = a[i]; find = 1;//设置岗哨find判断是否进入if语句 } else if(a[i] < max && a[i]>*p) { *p = a[i]; find = 1;//如果没有 max 大 和比*p记录的大 } } return find;}int main(void){ int a[1000]; int n; int i = 0, p = 0; scanf("%d", &n); for(i=0; i<n; i++) scanf("%d", &a[i]); if(findsec(a,n,&p) == 0) { printf("NO SEC NUMBER\n"); } else { printf("SEC = %d.\n",p); } return 0; }
5.一辆卡车违反交通规则,撞人后逃跑。现场有三人目击事件,但都没有记住车号。只留下车号的一些特征。
甲说:车牌的前两位数字是相同的
乙说:车牌的后两位的数字是相同的
丙说:4位车牌恰好是某个整数的平方。
//车号最大9999,所以车号为100以内的整数的平方#include <stdio.h>int judge(void){ int find; int i = 0, a = 0, b = 0, c = 0, d = 0; for(i=32; i<100; i++) { a = (i * i) % 10;//个位 b = ((i * i) % 100) / 10;//十位 c = ((i * i) % 1000) / 100;//百位 d = (i * i) / 1000;//千位 if(a == b)//个位,十位 { if(c == d)//百位,千位 { if(c != a){//个位百位不同 find = i * i; } } } } return find;}int main(void){ printf("%d\n",judge()); return 0;}
6.
What will print out why!
#include <stdio.h>int main (void){ char *p1 = "name"; char *p2; p2 = (char *)malloc(20); meset(p2,0,20); while(*p2++ = *p1++); printf("%s\n", p2); return 0;}
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(void){ char *p1="name"; char *p2; int i = 0; p2 = (char *)malloc(20); memset(p2,0,20); while(*p2++ = *p1++);//p2指针移动。 printf("%s\n", p2);//p2指针指向末尾。 return 0;}//所以到最后没有输出。//想要输出name的必须有一个指针指向p2
7.2、下面是某语言的计算字符串Hash值的算法如下,如果字符‘a’、‘b’对应的ascii的值对应十进制是97、98,
请回答控制台打印出的数值是多少_
typedef unsigned long uintptr_t;
uintptr_t NXStrHash (const void *data)
{
uintptr_t hash = 0;
unsigned char s = (unsigned char )data;
if (s)for (; ; )
{
if (*s == ‘\0’)break;
hash ^= (uintptr_t)*s++;
printf(“1—%ld.\n”, hash);
if (*s == ‘\0’)break;
hash ^= (uintptr_t)*s++<<8;
printf(“2—%ld.\n”, hash);
if (*s == ‘\0’)break;
hash ^= (uintptr_t)*s++<<16;
printf(“3—%ld.\n”, hash);
if (*s == ‘\0’)break;
hash ^= (uintptr_t)*s++<<24;
printf(“4—%ld.\n”, hash);
}
return hash;
};
int main(void)
{
uintptr_t hash_value = NXStrHash(“ab”);
printf(“%ld.\n”, hash_value);
return 0;
}
8.给出三个整数a,b,c。你可以在它们之间插入加号或者乘号以及括号将其变成一个表达式。比如数字1,2,3你可以构造出:
1+2*3=7
1*(2*3)=5
1*2*3=6
(1+2)*3=9
等表达式。现在你需要构造一个值最大的表达式,输出这个值。
输入描述:
第一行包括三个整数a,b,c。1=
#include <strdio.h>int main(void){ int a = 0, b = 0, c = 0; int max = 0; scanf("%d%d%d", &a, &b, &c); if((a >=1 && a <=10) && (b >=1 && b <= 10) && (c >=1 && c <=10)){ if(a*b*c > max) max = a*b*c; if((a+b)*c >max) max = a+b)*c; if(a*(b+c) >max) max = a*(b+c); if(a+b+c >max) max = a+b+c; if(a*b+c > max) max = a*b+c; if(a+b*c >max) max = a+b*c; printf("%d", max); } return 0;}
- 11.25练习
- 练习
- “++”“--”练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 练习
- 更新CentOS内核后删除旧内核
- Finding relation and pattern--Influence of Mathematics: formal inferring with concepts
- 171124 逆向-线程环境块(TEB)
- 访问单个节点的删除
- Day8之面向对象复习2
- 11.25练习
- 变量,数据类型
- 汇编第十五节-外中断
- G
- 数组和方法认识
- JAVA的三种代理模式
- Redhat 6安装Oracle 11g
- 171125 逆向-湖湘杯RE
- jq设置http头部信息和获取头部信息