1、从字符数组中读出相应的整数、实数。(写的有点可怕,先找第一个数字就会简单很多)
//从一个字符数组中读出相应的整数、实数 #include<stdio.h> #include<math.h> #include<string.h> int main() { void atoif(char a[]); char a[30]; fgets(a,30,stdin); atoif(a); return 0; } void atoif(char a[]) { int i = 0; int j = 0; int l = strlen(a); float b = 0; int n = 1; int c = 0; while(a[i]!='\0'){ if(a[i]<='9'&&a[i]>='0'&&a[i-1]!='.'&&a[i+1]!='.'){ j = i; while(a[j]<='9'&&a[j]>='0'){ c = c*10+(a[j]-'0'); j++; } if(a[j]!='.'){ printf("%d\n",c); break; } } if(a[i] == '.'){ if((i == 0||a[i-1]<='0'||a[i-1]>='9')&&(a[i+1]<='9'&&a[i+1]>='0')){ j = i+1; while((a[j]<='9'&&a[j]>='0')&&j<l){ b = b + (a[j]-'0')*pow(0.1,n); n++; j++; } } else if((i == l-1 ||a[i+1]<='0'||a[i+1]>='9')&&(a[i-1]<='9'&&a[i-1]>='0')){ j = i-1; n = 0; while((a[j]<='9'&&a[j]>='0')&&j>-1){ b+=(a[j]-'0')*pow(10,n); n++; j--; } } else if((a[i-1]<='9'&&a[i-1]>='0')&&(a[i+1]<='9'&&a[i+1]>='0')){ j = i+1; while((a[j]<='9'&&a[j]>='0')&&j<l){ b = b + (a[j]-'0')*pow(0.1,n); n++; j++; } j = i-1; n = 0; while((a[j]<='9'&&a[j]>='0')&&j>-1){ b+=(a[j]-'0')*pow(10,n); n++; j--; } } printf("%f\n",b); break; } //else i++; } }
2、整数转换为一个字符数组
#include<stdio.h> int main(){ void itoa(int a,char *b); int a; char b[10]; printf("please input a integer:"); scanf("%d",&a); itoa(a,b); printf("%s\n",b); return 0; } void itoa(int a,char *b) { int i = 0; int j = 0; char t; while(a!=0) { b[i] = (a%10)+'0'; a = a/10; i++; } b[i] = '\0'; i = i-1; while(j<i) { t = b[j]; b[j] = b[i]; b[i] = t; j++; i--; } }
3、字符串数组的冒泡排序(从小到大)(有个理解错误的问题,一开始输入的单词就应该用二维数组接着,不应该用一个大字符数组,还得提取出来放到二维数组里)
#include<stdio.h>//字符串数组的冒泡排序,从小到大 #include<string.h> #define M 30 #define N 15 int main() { int bubble(char *a,char b[][M]); char a[M]; char b [N][M]; int i; int n; printf("please input a string:"); scanf("%[^\n]",a); n = bubble(a,b); for(i = 0;i <= n;i++) { printf("%s\n",b[i]); } return 0; } int bubble(char *a,char b[][M]) { int i = 0; int j = 0; int k = 0; char c[M]; while(a[i]!='\0') { if(a[i]==' ') { b[j][k] = '\0'; j++; k = 0; } else { b[j][k] = a[i]; k++; } i++; } for(i = 0;i <= j;i++) { for(k = i;k <= j;k++) { if(strcmp(b[i],b[k])>0) { strcpy(c,b[i]); strcpy(b[i],b[k]); strcpy(b[k],c); } } } return j; }
4、素数,有且只有两个相同数的数
#include<stdio.h> int main() { void primetcom(int a,int b); int a,b; printf("please input two number:"); scanf("%d%d",&a,&b); primetcom(a,b); return 0; } void primetcom(int a,int b) { int i; int j; int k; int temp; int count; int n = 0; int c[5]; for(;a <= b;a++){ for(i = 2;i < a;i++) { if(a%i==0) { break; } } if(i == a) { temp = a; j = 0; count = 0; while(temp != 0) { c[j] =temp%10; k = 0; while(k<j) { if(c[k]==c[j]) { count++; } k++; } temp = temp/10; j++; } if(count == 1){ printf("%d\n",a); n++; } } } if(n == 0) { printf("not found a num fits your requirements!\n"); } }
5、b字符串先返回来,和a穿插到c中
#include<stdio.h> #include<string.h> #define N 10 int main() { void change(char *a,char *b,char *c); char a[N]; char b[N]; char c[2*N]; printf("please input two string:"); scanf("%s%s",a,b); change(a,b,c); printf("mix is %s\n",c); return 0; } void change(char *a,char *b,char *c) { int i = 0; int j; int k; char t; j = strlen(b)-1; while(i < j) { t = b[i]; b[i] = b[j]; b[j] = t; i++; j--; } printf("%s\n",b); i = 0; while(a[i/2]!='\0'&&b[i/2]!=0) { if(i%2) { c[i] = b[i/2]; } else { c[i] = a[i/2]; } i++; } j = i/2; if(a[j]!='\0') { while(a[j]!='\0') { c[i] = a[j]; i++; j++; } } else if(b[j]!='\0') { while(b[j]!='\0') { c[i] = b[j]; i++; j++; } } c[i] = '\0'; }
6、二维数组,偶数行从小到大,奇数行从大到小
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 5 int main() { void Output(int a[][N]); void sort(int a[][N]); int i; int j; int a[N][N]; srand((unsigned)time(NULL)); for(i = 0;i < N;i++) { for(j = 0;j < N;j++) { a[i][j] = rand()%10; } } Output(a); sort(a); Output(a); return 0; } void Output(int a[][N]) { int i,j; for(i = 0;i < N;i++) { for(j = 0;j < N;j++) { printf("%4d",a[i][j]); } printf("\n"); } } void sort(int a[][N]) { int i; int j; int k; int temp; for(k = 0;k < N;k++) { if(k%2) { for(i = 0; i < N;i++) { for(j = i;j < N; j++) { if(a[k][i] < a[k][j]) { temp = a[k][i]; a[k][i] = a[k][j]; a[k][j] = temp; } } } } else { for(i = 0; i < N;i++) { for(j = i;j < N; j++) { if(a[k][i] > a[k][j]) { temp = a[k][i]; a[k][i] = a[k][j]; a[k][j] = temp; } } } } } }
7、Troitsky(整数的第一位拿到最后一位,是否是原来数的整数倍)
#include<stdio.h> #define N 100 int main() { int Troitsky(long a[]); long a[N]={0}; int n; int i = 0; n = Troitsky(a); printf("the total number is %d\n",n); while(a[i] != 0) { printf("%ld\n",a[i]); i++; } return 0; } int Troitsky(long a[]) { int n = 0; int i; int b; int j; int k; int num[5]; int newnum; for(i = 10;i<=10000;i++) { b = i; j = 0; k = 0; newnum = 0; while(b != 0){ num[j] = b%10; b = b/10; j++; } for(k = j-2;k > -1;k--){ newnum = newnum*10 + num[k]; } newnum = newnum*10 +num[j-1]; printf("%d\n",newnum); if((newnum%i)==0) { n++; a[n-1] = i; } } return n; }
8、将两个字符串合并成一个不重复单词的字符串
#include<stdio.h> #include<string.h> #define N 10 #define M 20 int main() { int i=0; void set(char a[][M],char b[][M],char c[][M]); char a[N][M]={{"while"},{"for"},{"switch"},{"if"},{"continue"}}; char b[N][M]={{"for"},{"case"},{"do"},{"else"},{"char"},{"switch"}}; char c[2*N][M]={'\0'}; set(a,b,c); while(c[i][0]!='\0') { printf("%s\n",c[i]); i++; } return 0; } void set(char a[][M],char b[][M],char c[][M]){ int i = 0; int j = 0; int k = 0; while(b[i][0]!='\0') { strcpy(c[i],b[i]); i++; } while(a[j][0]!='\0') { for(k = 0;k < i;k++) { if(strcmp(a[j],c[k])==0) { break; } } if(k == i) { strcpy(c[i],a[j]); i++; } j++; } }
9、找出7个莫尼森数,数是素数,2^数次方-1也是素数
#include<stdio.h> #include<math.h> int main() { int isprime(int n); int i = 2; int a; int n; int count = 0; while(1) { n = isprime(i); if(n == 1) { a = pow(2,i)-1; n = isprime(a); } if(n == 1) { count++; printf("%d is a Mersenynum!\n",i); } if(count == 7) { break; } i++; } printf("%d\n",count); return 0; } int isprime(int n) { int i; for(i = 2;i < n;i++) { if((n%i)==0) { break; } } if(i == n) { return 1; } else { return 0; } }
10、对二维数组排序,数组中元素最小的元素排在第一行 ,依次排完(后期应该会单独出一期排序算法 ,现在排序写的是有点复杂)
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 6 int main() { void Output(int a[][N]); void sort(int *p[N]); int min(int *p); srand((unsigned)time(NULL)); int i,j; int a[N][N]; int *p[N]; int *q; for(i = 0;i < N;i++) { for(j = 0;j < N;j++) { a[i][j] = rand()%100; } } Output(a); for(i = 0;i < N;i++) { p[i] = a[i]; } sort(p); for(i = 0;i < N;i++) { q = p[i]; for(j = 0;j < N;j++) { printf("%4d",*q); q++; } printf("\n"); } printf("\n"); return 0; } void Output(int a[][N]) { int i; int j; for(i = 0;i < N;i++) { for(j = 0;j < N;j++) { printf("%4d",a[i][j]); } printf("\n"); } printf("\n"); } int min(int *p) { int i = 0; int min; min = *p; for(i = 0;i < N;i++) { if(min > *p) { min = *p; } p++; } return min; } void sort(int *p[N]) { int i,j,k; int *q; for(i = 0; i < N-1;i++) { for(j = i;j < N;j++) { if(min(p[i])>min(p[j])) { q = p[i]; p[i] = p[j]; p[j] = q; } } } }
标签:语言,int,void,char,++,printf,习题,include,529 From: https://www.cnblogs.com/gunancheng/p/17444638.html