这份程序是我们学院C语言上机考试题库中的部分题,由我同学总结,并共享给大家。(Word中的程序,排版有些问题,还请见谅)
求n个(项)数据之和或积
//求n个数据的和(或积) #include <stdio.h> int main() { int a[100]; int i = 0; int n; printf("此程序将实现累和还有累积的功能,请输入项数\n"); scanf("%d", &n); printf("请输入各项\n"); for(i = 0; i<n; i++){ scanf("%d", &a[i]); } //求累和 int sum = 0; for(i = 0; i<n; i++){ sum = sum + a[i]; } printf("sum = %d\n", sum); //求累积 int ji = 1; for(i = 0; i<n; i++){ ji = ji * a[i]; } printf("ji = %d\n",ji); return 0; }
1#include <stdio.h>
2
3int main()
4 {
5 int a;
6 int sum;
7 int ji;
8 int b[10];
9 scanf("%d",&a);
10 if(a<10){
11 printf("sum =%d\n", a);
12 printf("ji = %d\n", a);
13 }
14 else if(a>=10&&a<100){
15 b[0] = a/10;
16 b[1] = a % 10;
17 printf("sum = %d\n", b[0]+b[1]);
18 printf("ji = %d\n",b[0]*b[1]);
19 }
20 elseif(a>=100&&a<1000){
21 b[0] = a/100;
22 b[1] = (a % 100) / 10;
23 b[2] =( (a % 100) % 10 )/1;
24 printf("sum = %d\n", b[0]+b[1]+b[2]);
25 printf("ji = %d\n",b[0]*b[1]*b[2]);
26 }
27 else if(a>=1000&&a<10000){
28 b[0] = a/1000;
29 b[1] = (a % 1000) / 100;
30 b[2] = ( (a % 1000) % 100) /10;
31 b[3] = ( ( ( a % 1000) % 100) %10) / 1;
32 printf("sum = %d\n", b[0]+b[1]+b[2]+b[3]);
33 printf("sum = %d\n",b[0]*b[1]*b[2]*b[3]);
34 }
35 else
36 printf("It is too big\n");
37
38
39 return 0;
40 }
1#include <stdio.h>
2
3int main()
4 {
5 int n;
6 int i = 0;
7 int sum = 0;
8 int a[100];
9 float average;
10 scanf("%d", &n);
11 printf("\n");
12 for( i = 0 ; i< n; i++){
13 scanf("%d",&a[i]);
14 }
15
16 for(i = 0; i< n; i++){
17 sum = sum + a[i];
18 }
19 printf("average = %.3f\n", (float)sum / n);
20 return 0;
21 }
1#include <stdio.h>
2
3int main()
4 {
5 int n;
6 int a[100];
7 int i = 0;
8 int Max;
9 int Min;
10 scanf("%d", &n);
11 if(n> 100){
12 printf("n is too big\n");
13 }
14 else{
15 for(i = 0; i < n; i++){
16 scanf("%d", &a[i]);
17 }
18 Max = a[0];
19 for(i = 1; i<n; i++){
20 if(Max<a[i])
21 Max = a[i];
22 }
23 Min = a[0];
24 for(i = 1; i<n; i++){
25 if(Min>a[i])
26 Min = a[i];
27 }
28
29
30
31 printf("Max = %d\n", Max);
32 printf("Min = %d\n", Min);
33
34 }
35 return 0;
36 }
1#include <stdio.h>
2
3int main()
4 {
5 int a[100], i, j, temp, r;
6 int n;
7 scanf("%d",&n);
8 if(n>100){
9 printf("n is toobig");
10 }
11 else{
12 for(i = 0; i< n; i++){
13 scanf("%d", &a[i]);
14 }
15 for(i = 0; i<n - 1; i++){
16 r = i;
17 for(j = i+ 1; j<n; j++){
18 if(a[r]> a[j])
19 r = j;
20 }
21
22 if( r != i){
23 temp = a[i];
24 a[i] = a[r];
25 a[r] = temp;
26 }
27 }
28
29 for(i = 0; i< n; i++)
30 printf("%d ", a[i]);
31 }
32
33
34 return 0;
35 }
1#include <stdio.h>
2#include <string.h>
3
4int main()
5 {
6 char a[100];
7 int i;
8 scanf("%s", a);
9 printf("beforeexchange\n\t%s\n", a);
10 for(i = 0; i<strlen(a); i++){
11 if(a[i]>=97&&a[i]<=122)
12 a[i] = a[i] - 32;
13 else if(a[i]>=65&&a[i]<=90)
14 a[i] = a[i] + 32;
15 }
16 printf("after exchange\n\t%s\n", a);
17 return 0;
18 }
1#include <stdio.h>
2#include <string.h>
3
4int main()
5 {
6 char a[100];
7 char b;
8 int n = 0;
9 int i;
10 scanf("%s\n", a);
11 scanf("%c", &b);
12
13
14 for(i = 0; i<strlen(a); i++){
15 if(a[i] == b)
16 n++;
17 }
18
19 printf("%d\n", n);
20 return 0;
21 }
1#include <stdio.h>
2#include <string.h>
3
4int main()
5 {
6 char a[100], b[100];
7 memset(a, 100, 0);
8 memset(b, 100, 0);
9
10 int i = 0;
11 int n;
12 int m;
13 scanf("%s", a);
14 scanf("%s", b);
15
16 n = strlen(a);
17 m = strlen(b);
18
19 for(i= 0; i<m; i++){
20 a[n+i]=b[i];
21 }
22 a[m+n] = ‘\0‘;
23
24 printf("%s\n", a);
25 return 0;
26 }
1#include <stdio.h>
2
3//定义一个这样的函数
4int Fu_Zhi(char *str1, char *str2, char ch)
5 {
6 int i = 0, j = 0;
7 int k;
8 while(str1[i]!=ch)
9 i++;
10
11 j = i;
12 k = i;
13 while(i>=0){
14 str2[j-1] = str1[i-1];
15 i--;
16 j--;
17 }
18 str2[k] = ‘\0‘;
19 return 0;
20 }
21
23int main()
24 {
25 char a[100];
26 char b[100];
27 char ch;
29 printf("请输入指定字符:");
30 scanf("%c", &ch);
31 printf("请输入指定字符串:");
32 scanf("%s", a);
34 Fu_Zhi(a, b, ch);
36 printf("%s\n", b);
37
38 return 0;
39 }
1#include <stdio.h>
2
3//定义一个这样的函数
4int Fu_Zhi(char *str1, char *str2, char ch)
5 {
6 int i = 0, j = 0;
7 while(str1[i]!=ch)
8 i++;
9 while(str1[i]!=‘\0‘){
10 str2[j]= str1[i+1];
11 i++;
12 j++;
13 }
14 str2[j] = ‘\0‘;
15 return 0;
16 }
17
18
19int main()
20 {
21 char a[100];
22 char b[100];
23 char ch;
24
25 printf("请输入指定的字符:");
26 scanf("%c", &ch);
27 printf("请输入指定的字符串:");
28 scanf("%s", a);
29
30 Fu_Zhi(a, b, ch);
31
32 printf("%s\n", b);
33
34 return 0;
35 }
1#include <stdio.h>
2#include <stdlib.h>
3
4struct zhengjiewei{
5 char a[20];
6 int b;
7 int c[10][10];
8 char ch;
9
10};
11
12int main()
13 {
14 struct zhengjiewei * twob;
15 twob =(struct zhengjiewei *)malloc (sizeof(struct zhengjiewei));
16 // 要给结构体的变量分配内存, 不能把它当成局部变量来处理...
17 scanf("%s",twob->a);
18 scanf("%d",&twob->c[3][6]);
19 printf("%d\n", twob->c[3][6]);
20 printf("%s\n", twob->a);
21
22 return 0;
23
24 }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/lfhappypain/article/details/47281943