将十六进制的数转换成十进制的
#include <stdio.h>
int main(){
char str[100];
while(scanf("%s",&str)!=EOF){
int len = strlen(str);
int res=0;
for(int i=2;i<len;i++){
if(str[i]>='0'&&str[i]<='9'){
res = res*16+str[i]-'0';
}else{
res = res*16+str[i]-'A'+10;
}
}
printf("%d\n",res);
}
return 0;
}
转换进制
#include<iostream>
using namespace std;
#include<string>
int main() {
string s = "";
while (cin >> s) {
int num = 0;
for (int i = 2; i < s.size(); i++) {
num *= 16;
if (s[i] >= '0' && s[i] <= '9') num = num + s[i] - '0';
else num += s[i] - 'A' + 10;
}
cout << num<<endl;
}
return 0;
}
对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。 在字符串中,单词之间通过空白符分隔,空白符包括:空格(’ ‘)、制表符(’\t’)、回车符(‘\r’)、换行符(‘\n’)。
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main() {
char str[100];
gets(str);
int len = strlen(str);
int flag = 0;
for (int i = 0; i < len; i++) {
char c=str[i-1];
//(str[i]>='a'&&str[i]<='z') &&
if((i==0)&&(str[i]>='a'&&str[i]<='z')) {str[i]-=32;}
if((c==' '||c=='\t'|| c=='\n' ||c=='\r')&&(str[i]>='a'&&str[i]<='z'))
{
str[i]-=32;
}
}
puts(str);
return 0;
}
推测周几
#include<stdio.h>
#include<stdlib.h>
int isyear(int num){
if(num%400==0 ||(num%4==0 ||num&100!=0)) return 1;
else return 0;
}
int Bd=12;
int Bm=4;
int By=2012;
int main(){
int mon,day;
scanf("%d%d",&mon,&day);
int dayt[12]={31,31,28,31,30,31,30,31,31,30,31,30};
// char zhou[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
//注意以上的这个zhou数组行,C语言数组没有这样表达的
if(isyear(By)==1) dayt[2]=29;
int TolDay=0;
for(int i=Bm;i<=mon;i++){
TolDay+= dayt[i%12];
}
// printf("%d\n",TolDay);
TolDay=TolDay-Bd-(dayt[mon%12]-day);
/*第二种计算相隔多少天的算法
for(int i=Bm;i<mon;i++){
TolDay+= dayt[i%12];
}
// printf("%d\n",TolDay);
TolDay=TolDay+day-Bd);
*/
// printf("%d\n",TolDay);
int m=(4+TolDay%7)%7;
//本想利用字符串数组的下表对应取天数,但是C没有存储字符串元素的数组
switch(m){
case 1:
printf("Monday");
break;
case 2:
printf("Tuseday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 0:
printf("Sunday");
break;
}
}
oj上正确率80???
输入一个整数,判断该整数是否为素数,若是,输出该整数,若否,输出大于该整数的第一个素数。(例如,输入为14,输出17,因为17是大于14的第一个素数)
#include<stdio.h>
int isPrime(int num) {
if(num ==1) return 0;
for (int i = 2; i < num ; i++) {
if (num % i == 0) return 0;
}
return 1;
}
int main() {
int num;
scanf("%d",&num);
//注意边界条件!!! 判断那里不能填写10000,因为10000是边界,
for (int i = num ; i <= 20000; i++) {
if (isPrime(i)==1) {
printf("%d",i);
break;
}
}
return 0;
}
都是错???
#include<stdio.h>
#include<string.h>
char sbu[3]="gzu";
char lower[100];
int fla[100];
int main()
{
char str[100];
gets(str);
for(int i=0; i<strlen(str); i++)
{
if(str[i]>='A'&&str[i]<='Z')
lower[i]=str[i]+32;
else
lower[i]=str[i];
}
for(int i=0; i<strlen(str); i++)
{
int flag=1;
for(int j=0; j<strlen(sbu); j++)
{
if(sbu[i]!=lower[i+j])
{
flag=0;
break;
}
}
if(flag==1)
{
for(int j=i; j<strlen(sbu)+i; j++)
fla[j]=1;
}
}
for(int i=0; i<strlen(str); i++)
{
printf("%d",fla[i]);
}
for(int i=0; i<strlen(str); i++)
{
if(fla[i]==0)
printf("%c",str[i]);
}
return 0;
}
于长度为6位的一个01串,每一位都可能是0或1,一共有64种可能。它的前几个是:
000000
000001
000010
#include<stdio.h>
#include<string.h>
int er[6];//全局变量,默认初始化元素为0
void getER(int num){
int tol=0;
while(num){
int yu=num%2;
er[5-tol]=yu;
tol++;
num=num/2;
}
}
int main()
{
for(int i=0; i<64; i++)
{
getER(i);
for(int i=0;i<6;i++){
printf("%d",er[i]);
}
printf("\n");
}
}
统计二进制中0和1的个数
#include<stdio.h>
#include<string.h>
//注意int型数据,是4个字节,总共有32位bit位
//我们可以值统计1的数量即可
int main()
{
int num;
scanf("%d",&num);
int c1=0;
while(num)
{
int yu=num%2;
if(yu==1) c1++;
num=num/2;
}
printf("count0=%d count1=%d",32-c1,c1);
}
简单模拟折扣计算
#include<stdio.h>
#include<string.h>
//可以观察输出数据,注意数据范围的折扣
//还要注意题目的输出格式
int main()
{
//注意double数据的类型输入格式,如果是int类型输入,则只有80正确率
double money;
while(scanf("%lf",&money)!=EOF)
{
if(money<1000)
{
//%g是什么类型就输出什么类型,所以money*0.95)并没有用特定类型的变量存储
printf("discount=1,pay=%g\n",money);
}
else if(money>=1000&&money<2000)
{
printf("discount=0.95,pay=%g\n",money*0.95);
}
else if(money>=2000&&money<3000)
{
printf("discount=0.9,pay=%g\n",money*0.9);
}
else if(money>=3000&&money<5000)
{
printf("discount=0.85,pay=%g\n",money*0.85);
}
else
{
printf("discount=0.8,pay=%g\n",money*0.8);
}
}
return 0;
}
一个翻转数的原数可以有很多个,例如18的原数可以是81、810、8100等等。因此我们假定:通过翻转数求原数时,所求得的原数的个 位不为零。即18的原数就是81。
Lucy现在给出2个翻转数A、B。
这两个翻转数各自对应的原数是C、D,C 与D的和为 S ,Lucy想知道 S是多少?
#include<stdio.h>
#include<string.h>
int getRev(int a)
{
int na=0;
while(a)
{
int ge=a%10;
na=na*10+ge;
a/=10;
}
return na;
}
int main()
{
int a,b;
while( scanf("%d%d",&a,&b)!=EOF){
printf("%d\n",getRev(a)+getRev(b));
}
return 0;
}
转换为二进制
#include<stdio.h>
int er[25];
int i;
void getEr(int a)
{
i=0;
while(a)
{
int yu=a%2;
er[i++]=yu;
a=a/2;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
getEr(n);
for(int j=i-1; j>=0; j--)
{
printf("%d",er[j]);
}
}
return 0;
}
区分字符,数字,字母
输入:Thdad29#@12aH;77
输出:
ThdadaH
291277
#@;
#include<stdio.h>
int main()
{
char s[100];
while(gets(s)){
for(int i=0;i<strlen(s);i++){
char c=s[i];
if((c<='z'&&c>='a')||(c>='A'&&c<='Z'))
printf("%c",c);
}
printf("\n");
for(int i=0;i<strlen(s);i++){
char c=s[i];
if((c<='9'&&c>='0'))
printf("%c",c);
}
printf("\n");
for(int i=0;i<strlen(s);i++){
char c=s[i];
if((c>'z'||c<'a')&&(c<'A'||c>'Z')&&(c<'0'||c>'9'))
printf("%c",c);
}
}
return 0;
}
生成随机数
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int n=5;
int num[21]= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int main()
{
srand((unsigned)time(NULL));
int a;
int total5=0;
int re=0;
while(n>0)
{
a=rand()%20+1;
//printf("%d\n",a);
if(num[a]==0);
else
{
total5+=a;
num[a]=0;
n--;
}
}
for(int i=1; i<=20; i++)
{
//printf("-----");
// printf("%d\n",num[i]);
if(num[i]!=0)
re+=num[i];
}
printf("%d\n%d",total5,re);
}
删除模式串中的子串
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
gets(str);
for(int i=0; i<strlen(str); i++)
{
char c=str[i];
char c1=str[i+1];
char c2=str[i+2];
if((c=='G'||c=='g')&&(c1=='z'||c1=='Z')&&(c2=='u'||c2=='U'))
{
str[i]='0';
str[i+1]='0';
str[i+2]='0';
i+=3;
}
}
for(int i=0; i<strlen(str); i++)
{
if(str[i]!='0')
printf("%c",str[i]);
}
}
链表排序 头插法
#include<stdio.h>
struct node
{
int data;
struct node * next;
};
//将小的数利用头插入链表
void HeadInsert(int a,struct node*L)
{
struct node*p=L; //这里头节点是为了后面操作的统一性
//新增结点
struct node*q=(struct node*)malloc(sizeof(struct node));
q->data=a;
while(p->next!=NULL&&p->next->data<a)
{
p=p->next;
}
q->next=p->next;
p->next=q;
}
int main()
{
int num;
struct node *p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;//这里不要忘记初始化哦
for(int i=0; i<5; i++)
{
scanf("%d",&num);
HeadInsert(num,p);
}
while(p->next){
printf("%d ",p->next->data);
p=p->next;
}
}
利用头插法合并两个链表
#include<stdio.h>
struct node
{
int data;
struct node * next;
};
//将小的数利用头插入链表
void HeadInsert(int a,struct node*L)
{
struct node*p=L;
//新增结点
struct node*q=(struct node*)malloc(sizeof(struct node));
q->data=a;
while(p->next!=NULL&&p->next->data<a)
{
p=p->next;
}
q->next=p->next;
p->next=q;
}
int main()
{
int num,n1,n2;
struct node *p1=(struct node*)malloc(sizeof(struct node));
p1->next=NULL;
struct node *p2=(struct node*)malloc(sizeof(struct node));
p2->next=NULL;
//创建第一个链表
scanf("%d",&n1);
for(int i=0; i<n1; i++)
{
scanf("%d",&num);
HeadInsert(num,p1);
}
//创建第二个链表
scanf("%d",&n2);
for(int i=0; i<n2; i++)
{
scanf("%d",&num);
HeadInsert(num,p2);
}
//0 1 2 3 4
//利用头插法
while(p1->next){
HeadInsert(p1->next->data,p2);
p1=p1->next;
}
//遍历输出
while(p2->next){
printf("%d ",p2->next->data);
p2=p2->next;
}
return 0;
}
交换相邻奇数位置和偶数位置的数据
12345 — 21435
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
};
void Insert(struct node*L,int a){
struct node*p=L;
struct node*q=(struct node*)malloc(sizeof(struct node));
q->data=a;
q->next=NULL;
while(p->next) p=p->next;
q->next=p->next;
p->next=q;
}
void change(struct node*L){
int n=1;
struct node*p=L;
int tem;
while(p->next&&n<5){
tem=p->next->data;
p->next->data=p->next->next->data;
p->next->next->data=tem;
p=p->next->next;//这里的变化不能忘记哦
n+=2;
}
}
int main(){
int n,data;
scanf("%d",&n);
struct node*p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
for(int i=0;i<n;i++){
scanf("%d",&data);
Insert(p,data);
}
change(p);
while(p->next){
printf("%d ",p->next->data);
p=p->next;
}
}
对字符进行排序
注意sort是C++才有的哦,
qsort是C有的,它的第四个参数需要自己写排序规则,函数头是统一的
#include<stdio.h>
#include<stdlib.h>
int compare(const void* e1,const void *e2){
return *(char*)e1-*(char*)e2;
}
int main(){
char s[20];
while(scanf("%s",s)!=EOF){
qsort(s,strlen(s),sizeof(s[0]),compare);
for(int i=0;i<strlen(s);i++){
printf("%c",s[i]);
}
}
return 0;
}
按照字符串的长度排序 c++才有sort和stable_sort
可以指定比较规则,默认int升序
sort(v.begin,v.end);
stable_sort(str,str+n,cmp); //str是比较的地址及其区间,不是随便给个区间,
#include<bits/stdc++.h>
using namespace std;
bool cmp(string a,string b){
return a.size()<b.size(); //升序 >降序
}
int main(){
int n;
while(cin>>n){
getchar();//清除缓冲区中上一个回车,其实是读上一个换行符,用cin.get()也可以
//string类型的数组元素可以是string类型的字符串
//str[i]="sdfsdf";
string str[n];
for(int i=0;i<n;i++){
getline(cin,str[i]);
if(str[i]=="stop"){
n=i;
break;
}
}
**stable_sort(str,str+n,cmp);**
for(int i=0;i<n;i++){
cout<<str[i]<<endl;
}
}
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int main() {
string s;
int n;
string str[100];
while (cin >> n) {
//吃掉回车
getchar();
for (int i = 0; i < n; i++) {
getline(cin, s);
if (s == "stop") break;
//根据字符串的长度存储到对应下标的数组中
str[s.length()] = s;
}
for (int i = 0; i < 100; i++) {
if (str[i] != "") {
cout << str[i] << endl;
//还原标记数组的初始状态
str[i] = "";
}
}
}
return 0;
}
删除max,min,
#include<stdio.h>
int main(){
int n,num;
int s[100];
scanf("%d",&n);
int max=0;
int min=65535;
for(int i=0;i<n;i++){
scanf("%d",&num);
s[i]=num;
if(num>max) max=num;
if(num<min) min=num;
}
//题目中有个小陷阱,当max==min只要删除一个max,一个min,剩余的按原序输出!!
//考虑输入的数都是同一个数
if (max == min) {
for (int i = 0; i < n-2; i++) {
printf("%d ", s[i]);
}
}
else{
for(int i=0;i<n;i++){
if(s[i]==max||s[i]==min) continue;
printf("%d ",s[i]);
}
}
}
统计英文字母出现频率最高字母
方法1:可以使用链表,结点中增添统计的字段,没识别到一个字母,就将它插入单链表,如果链中已有,则将其count++,反之就插在链前,头插操作简便
方法2:以下
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int main() {
string s;
int cha[26] = { 0 };
getline(cin, s);
for (int i = 0; i < s.size(); i++) {
char c = s[i];
if ((c <= 'Z' && c >= 'A'))
s[i] += 32;
if (s[i] <= 'z' && s[i] >= 'a')
//太妙了 s[i] - 'a' 得到存储的下标 !! 对应统计
cha[s[i] - 'a']++;
}
int k ,max=0;
for (int i = 0; i < 26; i++) {
if (cha[i] > max) {
max = cha[i];
k = i;
}
}
cout <<(char)('a'+k)<< " "<< cha[0+k]<< endl;
return 0;
}
最大公约数
#include<iostream>
#include<algorithm>
using namespace std;
//欧几里得方法
int gcd(int a,int b){
if(b==0) return a;
else return gcd(b,a%b);
}
int main(){
int a,b;
cin>>a>>b;
cout<<gcd(a,b)<<endl;
return 0;
}
读入n个正整数,求出这n个数的最小值、最大值以及它们两的最大公约数,并输出。输入中第一行为n,接下来为n个大于零的整数。
#include<iostream>
#include<algorithm>
using namespace std;
//欧几里得方法
int gcd(int a,int b){
if(b==0) return a;
else return gcd(b,a%b);
}
int main(){
int n,s[1001];
int max=0;
int min=65535;
//int max=a[0],min=a[0]; 可以将第一个元素作为标记比较
cin>>n;
for(int i=0;i<n;i++){
cin>>s[i];
if(s[i]>max) max=s[i];
if(s[i]<min) min=s[i];
}
cout<<min<<" "<<max<<" "<<gcd(max,min)<<endl;
system("pause");
}
输入两个正整数m和n,求其最大公约数和最小公倍数。
最小公倍数=m*n / 最大公因数
#include<iostream>
#include<algorithm>
using namespace std;
int gcd(int a,int b){
if(b==0) return a;
else return gcd(b,a%b);
}
int main(){
int a,b;
cin>>a>>b;
cout<<gcd(a,b)<<" "<<a*b/gcd(a,b)<<endl;
system("pause");
}
输出字符棱形
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int isPrime(int num) {
int i;
if(num <2) return 0;
for (i = 2; i <= num/2; i++) {
if (num % i == 0) return 0;
}
return 1;
}
int main() {
int n,i,j,k;
while(~scanf("%d",&n)){
for (i = 1 ; i <=n; i++) {
for(k=1;k<=n-i;k++){
printf(" ");
}
for(j=1;j<=2*i-1;j++){
printf("*");
}
printf("\n");
}
for (i = n-1 ; i >=1; i--) {
for(k=1;k<=n-i;k++){
printf(" ");
}
for(j=1;j<=2*i-1;j++){
printf("*");
}
printf("\n");
}
}
return 0;
}
topN
相同的数据排名一样
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int isPrime(int num) {
int i;
if(num <2) return 0;
for (i = 2; i <= num/2; i++) {
if (num % i == 0) return 0;
}
return 1;
}
int main() {
int n,i,j,k,fi,s[1000]={0};
int num=0;
scanf("%d",&n);
for (i = 0 ; i <n; i++) {
scanf("%d",&j);
s[j]=1;
}
scanf("%d",&fi);
for(k=0;k<1000;k++){
if(s[k]!=0)num++;
if(num==fi){ printf("%d",k);break;}
}
system("pause");
}
水仙花数,注意输出的格式
include<stdio.h>
#include<stdlib.h>
#include<math.h>
int isHua(int num){
double ge=num%10;
double s=num/10%10;
double b=num/100;
if(pow(ge,3)+pow(s,3)+pow(b,3)==num ) return 1;
return 0;
}
int main() {
int m,n,i,flag;
while(scanf("%d%d",&m,&n)!=EOF){
if(m==0&&n==0) break;
flag=0;
for(i=m;i<=n;i++){
if(isHua(i)==1) {
printf("%d ",i);
flag=1;}
}
if(flag==0) printf("no ");
printf("\n");
}
return 0;
}
加密算法
编写加密程序,加密规则为:将所有字母转化为该字母后的第三个字母,即A->D、B->E、C->F、…、Y->B、Z->C。小写字母同上,其他字符不做转化。输入任意字符串,输出加密后的结果。
例如:输入"I love 007",输出"L oryh 007"
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
int main() {
string str;
//注意运算符的书写
/26个字母的循环
getline(cin,str);
for (int i = 0; i < str.length(); i++) {
char c = str[i];
if (c >= 'A' && c <= 'Z') {
if (c == 'X') printf("A");
else if (c == 'Y') printf("B");
else if (c == 'Z') printf("C");
else printf("%c", (c + 3));
}
else if (c<= 'z' && c>='a') {
if (c == 'x') printf("a");
else if (c == 'y') printf("b");
else if (c == 'z') printf("c");
else printf("%c", (c + 3));
}
else {
printf("%c",c);
}
}
printf("\n");
return 0;
}
加密算法
编写加密程序,加密规则为:将所有字母转化为该字母后的第三个字母,即A->D、B->E、C->F、…、Y->B、Z->C。小写字母同上,其他字符不做转化。输入任意字符串,输出加密后的结果。
例如:输入"I love 007",输出"L oryh 007"
#include<stdio.h>
#include<string.h>
int main()
{
char a[120];
gets(a);
int len = strlen(a) ;
for(int i = 0 ; i < len ; i++)
{
if(a[i]>='A'&&a[i]<='W') a[i]+=3;
else if(a[i]>='a'&&a[i]<='w') a[i]+=3;
//这下面妙哦 26字母的循环转换
else if(a[i]>='X'&&a[i]<='Z') a[i]-=23;
else if(a[i]>='x'&&a[i]<='z') a[i]-=23;
}
printf("%s\n",a);
return 0;
}
最长连续因子
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() {
//注意一些数据的初始化,变量的声明要在最开始,数组要进行初始化,
int num,i;
while(~scanf("%d",&num)){
int max=0;
int k=0,s[10000]={0};
//找因子
for(i=2;i<=num;i++){
if(num%i==0) {s[i]=1;
//输出验证因子 printf("%d ",i);
}
}
//统计连续因子
for(i=2;i<=num;i++){
if(s[i]==1){
s[0]=s[0]+1;
}
else{
if(max<s[0]){ max=s[0]; k=i;}
s[0]=0;
}
//printf("---%d %d\n",max,k);
}
//输出连续因子
for(i=1;i<=max;i++){
printf("%d ",k-max-1+i);
}
}
return 0;
}
上楼梯
注意一开始就在一楼哦
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int n=0;//有n个样例
int num=0;//num个数
int flo[11]={0};//楼层
int time;//时间
int main() {
int i,j,k;
scanf("%d",&n);
for(k=0;k<n;k++){
time=0;
scanf("%d",&num);
for(j=0;j<num;j++){
scanf("%d",&flo[j]);
}
//统计楼层时间
for(i=0;i<num-1;i++){
if(i==0&&flo[i]!=1) time+=(flo[i]-1)*6;
if(flo[i]>flo[i+1])
time+=(flo[i]-flo[i+1])*4;
if(flo[i]<flo[i+1])
time+=(flo[i+1]-flo[i])*6;
}
if(flo[0]==1) time+=(num-1)*3;
else time+=num*3;
printf("%d\n",time);
}
system("pause");
}
杨辉三角
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() {
int i,j,k,n;
int s[101][101]={0};
while(scanf("%d",&n)){
if(n==0) break;
for(i=0;i<n;i++){
for(j=0;j<=i;j++){
if(i==j||j==0) s[i][j]=1;
else{
s[i][j]=s[i-1][j-1]+s[i-1][j];
}
printf("%d ",s[i][j]);
}
printf("\n");
}
}
system("pause");
}
反序数
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() {
int i,j,ge,sum,n;
for(j=1000;j<10000;j++){
//初始化sum
sum=0;
//将原来的数值进行另存
n=j;
for(i=0;i<4;i++){
ge=n%10;
sum=sum*10+ge;
n=n/10;
}
if(sum==j*9){
printf("%d\n",j);
}
}
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
struct Sqlist{
int *data;
int N;
int Maxsize;
};
//初始化顺序表
struct Sqlist *Ini(int max){
struct Sqlist* L;
L=(struct Sqlist*)malloc(sizeof(struct Sqlist));
L->data=(int *)malloc(sizeof(int)*max);
L->N=0;
L->Maxsize=max;
return L;
}
//打印数据
void show(struct Sqlist *L){
int i=0;
for(i=0;i<L->N;i++){
printf("%d ",L->data[i]);
}
}
//插入数据
void Insert(struct Sqlist *L,int a){
if(L->N< L->Maxsize) {
L->data[L->N]=a;
L->N++;
}else{
L->data=(int*)realloc(L->data,(L->Maxsize+1)*sizeof(int));
L->Maxsize++;
L->data[L->N]=a;
L->N++;
}
}
//在指定位置增加数据
struct Sqlist* addloc(struct Sqlist *L ,int elem,int add)
{
int i;
if (add>L->N+1||add<1) {
printf("插入位置有问题");
return L;
}
if (L->N>=L->Maxsize) {
L->data=(int *)realloc(L->data, (L->Maxsize+1)*sizeof(int));
if (!L->data) {
printf("存储分配失败");
}
L->Maxsize+=1;
}
for (i=L->N-1; i>=add-1; i--) {
L->data[i+1]=L->data[i];
}
L->data[add-1]=elem;
L->N++;
return L;
}
struct Sqlist* delOu(struct Sqlist*L){
int i;
for(i=0;i<L->N;i++){
if(L->data[i]%2==1) printf("%d",L->data[i]);
}
}
int main(){
int i,j,ge,sum,n;
struct Sqlist* L=Ini(100);
srand((int)time(NULL));
for(j=0;j<100;j++){
sum=rand()%(100)+100; //生成随机数在100到200之间的随机数
Insert(L,sum);
}
show(L);
printf("-----\n");
delOu(L);
system("pause");
}
顺序表
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
struct Sqlist{
int *data;
int N;
int Maxsize;
};
//初始化顺序表
struct Sqlist *Ini(int max){
struct Sqlist* L;
L=(struct Sqlist*)malloc(sizeof(struct Sqlist));
L->data=(int *)malloc(sizeof(int)*max);
L->N=0;
L->Maxsize=max;
return L;
}
//打印数据
void show(struct Sqlist *L){
int i=0;
for(i=0;i<L->N;i++){
printf("%d ",L->data[i]);
}
}
//插入数据
void Insert(struct Sqlist *L,int a){
if(L->N< L->Maxsize) {
L->data[L->N]=a;
L->N++;
}else{
L->data=(int*)realloc(L->data,(L->Maxsize+1)*sizeof(int));
L->Maxsize++;
L->data[L->N]=a;
L->N++;
}
}
//在指定位置增加数据
struct Sqlist* addloc(struct Sqlist *L ,int elem,int add)
{
int i;
if (add>L->N+1||add<1) {
printf("插入位置有问题");
return L;
}
if (L->N>=L->Maxsize) {
L->data=(int *)realloc(L->data, (L->Maxsize+1)*sizeof(int));
if (!L->data) {
printf("存储分配失败");
}
L->Maxsize+=1;
}
for (i=L->N-1; i>=add-1; i--) {
L->data[i+1]=L->data[i];
}
L->data[add-1]=elem;
L->N++;
return L;
}
struct Sqlist* delOu(struct Sqlist*L){
int i;
for(i=0;i<L->N;i++){
if(L->data[i]%2==1) printf("%d",L->data[i]);
}
}
int main(){
int i,j,ge,sum,n;
struct Sqlist* L=Ini(100);
srand((int)time(NULL));
for(j=0;j<100;j++){
sum=rand()%(100)+100; //生成随机数在100到200之间的随机数
Insert(L,sum);
}
show(L);
printf("-----\n");
delOu(L);
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
struct Sqlist{
int *data;
int N;
int Maxsize;
};
//初始化顺序表
struct Sqlist Ini(int max){
struct Sqlist L;
L=(struct Sqlist*)malloc(sizeof(struct Sqlist));
L->data=(int *)malloc(sizeof(int)*max);
L->N=0;
L->Maxsize=max;
return L;
}
//打印数据
void show(struct Sqlist *L){
int i=0;
for(i=0;iN;i++){
printf("%d ",L->data[i]);
}
}
//插入数据
void Insert(struct Sqlist L,int a){
if(L->N< L->Maxsize) {
L->data[L->N]=a;
L->N++;
}else{
L->data=(int)realloc(L->data,(L->Maxsize+1)*sizeof(int));
L->Maxsize++;
L->data[L->N]=a;
L->N++;
}
}
//在指定位置增加数据
struct Sqlist* addloc(struct Sqlist *L ,int elem,int add)
{
int i;
if (add>L->N+1||add<1) {
printf(“插入位置有问题”);
return L;
}
if (L->N>=L->Maxsize) {
L->data=(int *)realloc(L->data, (L->Maxsize+1)*sizeof(int));
if (!L->data) {
printf(“存储分配失败”);
}
L->Maxsize+=1;
}
for (i=L->N-1; i>=add-1; i–) {
L->data[i+1]=L->data[i];
}
L->data[add-1]=elem;
L->N++;
return L;
}
//查找某个值
int Findkey(struct Sqlist*L,int key){
int i;
for(i=0;iN;i++){
if(L->data[i]==key) return i;
}
return -1;
}
//删除某个位置的值
void delekey(struct Sqlist*L,int loc){
//删除位置的合理性
if(loc<0||loc>L->N-1) return;
int i;
for(i=loc;iN-1;i++){
L->data[i]=L->data[i+1];
}
L->N–;
}
//判断是否是素数
int IsSu(int a){
int i;
if(a<2) return 0;
for(i=2;i<a;i++){
if(i%2==0) return 0;
}
return 1;
}
void Add0(struct Sqlist*L){
int i,j=0;
for(i=0;i<L->N;i++){
if(IsSu(L->data[i])) {
j++;
}
//打印奇数
struct Sqlist* printJi(struct Sqlist*L){
int i;
for(i=0;iN;i++){
if(L->data[i]%2==1) printf("%d ",L->data[i]);
}
}
int main(){
int i,j,ge,sum,n;
struct Sqlist* L=Ini(100);
srand((int)time(NULL));
for(j=0;j<100;j++){
sum=rand()%(100)+100; //生成随机数在100到200之间的随机数
Insert(L,sum);
}
show(L);
printf("-----\n");
i=Findkey(L,123);
delekey(L,4);
show(L);
printf("-----\n");
printJi(L);
system("pause");
}