zoukankan      html  css  js  c++  java
  • C语言题库的上机题

    1、编写函数,实现从键盘上输入一个小写字母,将其转化为大写字母。

     1 #include<stdio.h> 
     2 int zhuanhua(char s); 
     3 void main(){ 
     4     char s; 
     5     printf("请输入一个字符:"); 
     6     scanf("%c",&s); 
     7     printf("转化前为:%c
    ",s); 
     8     s=zhuanhua(s); 
     9     printf("转化后为:%c
    ",s); 
    10 } 
    11 int zhuanhua(char s){ 
    12     char S; 
    13     S=s-32; 
    14     return S; 
    15 } 

    2、计算并输出500以内最大的10个能被13或17整除的自然数之和

     1 #include<stdio.h> 
     2 int jisuan(int n);
     3 
     4 void main(){ 
     5     int n=500; 
     6     printf("和是%d
    ",jisuan(n)); 
     7 } 
     8 int jisuan(int n){ 
     9     int i,sum=0; 
    10     for(i=1;i<n;i++) 
    11         if(i%13==0||i%17==0) 
    12             sum+=i; 
    13     return sum; 
    14 } 

    3、将字符串str中的小写字母全部转换成大写字符串。函数原型可声明为:“void  toUpperCase( char  str[ ]) ;".

     1 #include <stdio.h> 
     2 #include<string.h> 
     3 #define N 10 
     4 void  toUpperCase( char  str[ ]);
     5 
     6 void main(){ 
     7     char str[N]; 
     8     printf("请输入一个字符串:"); 
     9     gets(str); 
    10     toUpperCase(str); 
    11     printf("交换后的字符串为:"); 
    12     puts(str); 
    13    
    14 
    15 } 
    16 void  toUpperCase( char  str[ ]){ 
    17     int i; 
    18     for(i=0;;i++){ 
    19         if(str[i]!='') 
    20             str[i]=str[i]-32; 
    21         else 
    22             break; 
    23     } 
    24 } 

    4、编写函数,将一个字符串的全部有效元素逆置。函数原型可声明为“void reverseStr( char * str ) ;”,参数str为指向字符串的指针。

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 void reverseStr( char *str );
     5 
     6 int main( )
     7 {
     8 char str[100];
     9 puts(“请输入字符串:”);
    10 gets(str);
    11 reverseStr( str );
    12 puts(“逆转后的字符串:”);
    13 puts( str );
    14 return 0;
    15 }
    16 
    17 void reverseStr( char *str )
    18 {
    19 char tmp;
    20 char *pi, *pj;
    21 pi = str; 
    22 pj = str + strlen( str ) – 1;
    23 while (pi < pj )
    24 { tmp = *pi; *pi = *pj; *pj = tmp; 
    25 ++pi; --pj;
    26 }
    27 }

    5、Fill the blanks of the following program. Function:output by the character w, such as W-shaped structure into a graphical.
    w       ww         w
     w     w  w       w
      w   w    w     w
       w  w     w   w
        ww        ww
    #include <stdio.h>
    void draw(int n)

    int i, j, k, r, m;
    for(i=1; ① ;i++)
    {
    for(j = 1; j <= 2; j++)
    {
    for(r = 1; r < i; r++) printf(" ");
    printf("w");
    for(k = 1; ② ; k++) printf(" ");
    printf("w");
    for(m = 1; m < i; m++) printf(" ");
    }
    ③ ;
    }
    }
    #include <conio.h>
    int main( )
    {
    int n;
    printf("input a number:");
    ④ ;
    draw(n);
    return 0;
    }
    答案:①i <= n;②k<=2*(n-i);③ printf(“ ”);④scanf(“%d”, &n);

    6、编写函数,将一个整型数组的全部元素逆序存储,即若原来数组元素分别为12345,逆序存储后数组各元素变为54321。函数原型可声明为“void reverse( int * p , int n );”,参数p为指向数组的指针,n为数组中的元素个数。

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 void reverseStr( char *str );
     5 
     6 int main( )
     7 {
     8 char str[100];
     9 puts(“请输入字符串:”);
    10 gets(str);
    11 reverseStr( str );
    12 puts(“逆转后的字符串:”);
    13 puts( str );
    14 return 0;
    15 }
    16 
    17 void reverseStr( char *str )
    18 {
    19 char tmp;
    20 char *pi, *pj;
    21 pi = str; 
    22 pj = str + strlen( str ) – 1;
    23 while (pi < pj )
    24 { tmp = *pi; *pi = *pj; *pj = tmp; 
    25 ++pi; --pj;
    26 }
    27 }

    7、编写程序,用有参有返回值函数实现判断三个数是否能构成三角形。函数原型可声明为:“int isTriangle( double a, double b, double c );”,其中,a,b,c为三角形的三条边,返回值为0或1,0代表不能构成三角形,1代表能构成三角形。请在主函数中调用该函数完成程序的功能。

     1 #include <stdio.h>
     2 int isTriangle( double a, double b, double c);//函数声明
     3 
     4 int main( )
     5 {
     6 double ea, eb, ec;//三角形的三条边
     7 int result; //是否是三角形的判断结果
     8 printf(“Please input the three edges of a triangle:”);
     9 scanf(“%lf%lf%lf”, &ea, &eb, &ec);
    10 result = isTriangle( ea, eb, ec ); //函数调用
    11 if( 0 == result )
    12 printf(“这组边长不能构成三角形
    ”);
    13 else
    14 printf(“这组边长可以构成三角形
    ”);
    15 return 0;
    16 }
    17 
    18 int isTriangle( double a, double b, double c ) //函数定义
    19 {
    20 if( a > 0 && b > 0 && c > 0 && a + b > c && b + c > a && c + a > b )
    21 //三角形任意边长大于0,且 任意两边之和大于第三边
    22 return 1; //可以构成三角形
    23 else
    24 return 0; //不能构成三角形
    25 }

    8、Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types 932, the program should display “ nine three two”.

    #include <stdio.h> 
    int main( )
    {
    int num;
    int iDigit; //每一位上的数字
    printf(“Pleasse input an integer:”);
    scanf(“%d”, &num);
    while ( num ) //当该数不为0时,继续提取其个位上的数字
    {
    iDigit = num % 10; //提取个位
    switch ( iDigit )
    {
    case 0: printf(“Zero “); break;
    case 1: printf(“One “); break;
    case 2: printf(“Two “); break;
    case 3: printf(“Three “); break;
    case 4: printf(“Four “); break;
    case 5: printf(“Five “); break;
    case 6: printf(“Six “); break;
    case 7: printf(“Seven “); break;
    case 8: printf(“Eight “); break;
    case 9: printf(“Night “); break;
    }
    num /= 10; //去掉该数的个位
    }
    printf(“
    ”);
    return 0;
    }
    
    

    9、Write a function to calculate the absolute value of x.

     1 #include <stdio.h>
     2 double absoluteValue( double x );
     3 
     4 int main( )
     5 {
     6 double x;
     7 printf(“请输入x:”);
     8 scanf(“%lf”, &x);
     9 printf(“Absolute Value of x is: %lf
    ”, absoluteValue( x ) );
    10 
    11 printf(“请输入x:”); //第二次调用函数
    12 scanf(“%lf”, &x);
    13 printf(“Absolute Value of x is: %lf
    ”, absoluteValue( x ) );
    14 return 0;
    15 }
    16 
    17 double absoluteValue( double x )
    18 {
    19 if ( x > 0 )
    20 return x;
    21 else
    22 return - x;
    23 }

    10、write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types 932, the program should display “ nine three two”.

     1 #include <stdio.h>
     2  int main( )
     3 {
     4 int num;int iDigit; //每一位上的数字
     5 printf(“Pleasse input an integer:”);
     6 scanf(“%d”, &num);
     7 while ( num ) //当该数不为0时,继续提取其个位上的数字{
     8 iDigit = num % 10; //提取个位
     9 switch ( iDigit )
    10 {
    11 case 0: printf(“Zero “); break;
    12 case 1: printf(“One “); break;
    13 case 2: printf(“Two “); break;
    14 case 3: printf(“Three “); break;
    15 case 4: printf(“Four “); break;
    16 case 5: printf(“Five “); break;
    17 case 6: printf(“Six “); break;
    18 case 7: printf(“Seven “); break;
    19 case 8: printf(“Eight “); break;
    20 case 9: printf(“Night “); break;
    21 }
    22 num /= 10; //去掉该数的个位}
    23 printf(“
    ”);return 0;
    24 }

    11Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types 932, the program should display “ nine three two”.

     1 #include <stdio.h>
     2  int main( )
     3 {
     4 int num;int iDigit; //每一位上的数字
     5 printf(“Pleasse input an integer:”);
     6 scanf(“%d”, &num);
     7 while ( num ) //当该数不为0时,继续提取其个位上的数字{
     8 iDigit = num % 10; //提取个位
     9 switch ( iDigit )
    10 {
    11 case 0: printf(“Zero “); break;
    12 case 1: printf(“One “); break;
    13 case 2: printf(“Two “); break;
    14 case 3: printf(“Three “); break;
    15 case 4: printf(“Four “); break;
    16 case 5: printf(“Five “); break;
    17 case 6: printf(“Six “); break;
    18 case 7: printf(“Seven “); break;
    19 case 8: printf(“Eight “); break;
    20 case 9: printf(“Night “); break;
    21 }
    22 num /= 10; //去掉该数的个位}
    23 printf(“
    ”);return 0;
    24 }

    12从键盘输入10个互不相同的整数,找出其中最小的元素将其与数组中的第一个元进行交换。

     

     1 12.#include <stdio.h>
     2 void getMin( int a[ ], int n); //函数声明
     3 int main( )
     4 {
     5 int array[10];
     6 int i;
     7 printf(“请输入10个互不相等的整数:”);
     8 for ( i=0; i<10; ++i)
     9 scanf(“%d”, &array[i]);
    10 
    11 getMin( array , 10 ); //调用函数来实现功能
    12 
    13 printf(“将最小值换到第1个元素后:”);
    14 for ( i=0; i<10; ++i)
    15 printf(“%5d”, array[i]);
    16 printf(“
    ”);
    17 return 0;
    18 }
    19 void getMin( int a[ ], int n) //函数定义
    20 {
    21 int i;
    22 int tmp;
    23 int minid=0;
    24 for( i=1; i<n; ++i)
    25 if ( a [i] < a [minid] )
    26 minid = i;
    27 //将最小值换到第1个元素
    28 tmp = a[0];
    29 a[0] = a[minid];
    30 a[minid] = tmp;
    31 }

     

    1310本图书,从键盘输入每本图书的价格后,找出价格最高的图书并输出其价格。

     

     1 #include <stdio.h>
     2 double getMaxPrice( double a[ ], int n); //函数声明
     3 int main( )
     4 {
     5 double book[10];
     6 int i;
     7 int maxid;
     8 printf(“请输入10本图书的价格:”);
     9 for ( i=0; i<10; ++i)
    10 scanf(“%lf”, &book[i]);
    11 
    12 maxid = getMaxPrice ( book , 10 ); //调用函数来实现功能
    13 
    14 printf(“最高书价:%.2lf
    ” , book[maxid] );
    15 return 0;
    16 }
    17 double getMaxPrice ( double a[ ], int n) //函数定义
    18 {
    19 int i;
    20 int maxid =0;
    21 for( i=1; i<n; ++i)
    22 if ( a [i] > a [maxid] )
    23 maxid = i;
    24 return maxid;
    25 }

     

    14Writing a function to find the minimum subscript of element in an array ,and return the subscript to the function who calls it.

     

     

     1 #include <stdio.h>
     2 int getMinid( int a[ ], int n); //函数声明
     3 int main( )
     4 {
     5 int array[10];
     6 int i;
     7 int minid;
     8 printf(“请输入10个整数:”);
     9 for ( i=0; i<10; ++i)
    10 scanf(“%d”, &array[i]);
    11 
    12 minid = getMin( array , 10 ); //调用函数来实现功能
    13 
    14 printf(“最小元素的下标:%d
    ” , minid );
    15 return 0;
    16 }
    17 int getMin( int a[ ], int n) //函数定义
    18 {
    19 int i;
    20 int minid=0;
    21 for( i=1; i<n; ++i)
    22 if ( a [i] < a [minid] )
    23 minid = i;
    24 return minid; 
    25 }

    15从键盘输入若干整数(数据个数应小于20),其值在04的范围内,用-1作为输入结束的标志。编程统计输入的整数个数。

     1 #include <stdio.h>
     2 int getCount( int a[ ], int n ); //函数声明
     3 int main( )
     4 {
     5 int array[20] ={0} ; //用来存储最多20个整数
     6 int c;
     7 c = getCount( array , 20 );
     8 printf(“输入的有效数的个数为:%d
    ”, c);
     9 return 0;
    10 }
    11 
    12 int getCount( int a[ ], int n ) //函数定义
    13 {
    14 int tmp; //临时存储输入的数
    15 int count=0; //输入的有效数的个数
    16 printf(“请输入若干个0-4之间的整数(以一1结束输入):”);
    17 scanf(“%d” , &tmp);
    18 while ( tmp != -1 && count <20 )
    19 {
    20 if ( tmp >=0 && tmp <=4 ) //在范围之内
    21 a[ count++] = tmp ; //将值转存到数组对应元素中
    22 scanf(“%d”, &tmp);//输入下一个数
    23 }
    24 return count;
    25 }

    16、

    编写函数,将一个整型数组的全部元素逆序存储,即若原来数组元素分别为1,2,3,4,5,逆序存储后数组各元素变为5,4,3,2,1。函数原型可声明为:“void reverse( int a[ ] , int n );”,参数a为数组,n为数组中的元素个数。

     

     1 #include <stdio.h>
     2 void reverse( int a[ ], int n );
     3 
     4 int main( )
     5 {
     6 int array[10]={0};
     7 int i;
     8 printf(“请输入10个整数:”);
     9 for( i=0; i<10; i++)
    10 scanf(“%d”, &array[i]);
    11 reverse( array, 10); //调用函数逆序存储数组中的数据
    12 printf(“逆序后的元素为:
    ”);
    13 for( i=0; i<10; i++)
    14 printf(“%5d”, array[i]);
    15 printf(“
    ”);
    16 return 0;
    17 }
    18 void reverse( int a[ ], int n )
    19 {
    20 int i;
    21 int tmp;
    22 for( i=0; i<n/2; ++i)
    23 {
    24 tmp = a[i]; a[i] = a[n-i-1]; a[n-i-1] = tmp;
    25 }
    26 }

     

    17、

    编写函数,将一个十进制数转换成一个二进制数(提示:将转换后的二进制数各位的值依次存储在一个一维数组中,要输出时,只要逆序输出这个数组各元素的值即可)。函数原型可声明为:“int transformToBin( int dnum, int bin[ ] ) ;”,参数dnum是要转换的十进制数,bin是存储转换后的二进制值的数组(逆序存储的),返回值是bin数组中元素的个数。

     

      1 #include <stdio.h>
      2 int transformToBin( int dnum, int bin[ ] ) ;
      3 
      4 int main( )
      5 {
      6 int array[32]={0}; //保存转换后的二进制数
      7 int num; //待转换的整数
      8 int cc; //最后得到的二进制总共多少位
      9 printf(“请输入一个整数:”);
     10 scanf(“%d”, &num);
     11 cc = transformToBin( num, array ); //调用转换函数
     12 cc--; //往回退一个元素下标,使cc指向最后一个元素
     13 for( ; cc>=0; cc-- ) //输出转换后的二进制数
     14 printf(“%d”, array[cc]);
     15 printf(“
    ”);
     16 return 0;
     17 }
     18 int transformToBin( int dnum, int bin[ ] )
     19 {
     20 int count = 0;
     21 while ( dnum ) //当dnum还未转换完毕 
     22 {
     23 bin[count++] = dnum % 2; //余数保留到数组对应元素中
     24 dnum /= 2; //数本身除2
     25 }
     26 return count;
     27 }
     28 #include <stdio.h>
     29 int transformToBin( int dnum, int bin[ ] ) ;
     30 
     31 int main( )
     32 {
     33 int array[32]={0}; //保存转换后的二进制数
     34 int num; //待转换的整数
     35 int cc; //最后得到的二进制总共多少位
     36 printf(“请输入一个整数:”);
     37 scanf(“%d”, &num);
     38 cc = transformToBin( num, array ); //调用转换函数
     39 cc--; //往回退一个元素下标,使cc指向最后一个元素
     40 for( ; cc>=0; cc-- ) //输出转换后的二进制数
     41 printf(“%d”, array[cc]);
     42 printf(“
    ”);
     43 return 0;
     44 }
     45 int transformToBin( int dnum, int bin[ ] )
     46 {
     47 int count = 0;
     48 while ( dnum ) //当dnum还未转换完毕 
     49 {
     50 bin[count++] = dnum % 2; //余数保留到数组对应元素中
     51 dnum /= 2; //数本身除2
     52 }
     53 return count;
     54 }
     55 #include <stdio.h>
     56 int transformToBin( int dnum, int bin[ ] ) ;
     57 
     58 int main( )
     59 {
     60 int array[32]={0}; //保存转换后的二进制数
     61 int num; //待转换的整数
     62 int cc; //最后得到的二进制总共多少位
     63 printf(“请输入一个整数:”);
     64 scanf(“%d”, &num);
     65 cc = transformToBin( num, array ); //调用转换函数
     66 cc--; //往回退一个元素下标,使cc指向最后一个元素
     67 for( ; cc>=0; cc-- ) //输出转换后的二进制数
     68 printf(“%d”, array[cc]);
     69 printf(“
    ”);
     70 return 0;
     71 }
     72 int transformToBin( int dnum, int bin[ ] )
     73 {
     74 int count = 0;
     75 while ( dnum ) //当dnum还未转换完毕 
     76 {
     77 bin[count++] = dnum % 2; //余数保留到数组对应元素中
     78 dnum /= 2; //数本身除2
     79 }
     80 return count;
     81 }
     82 #include <stdio.h>
     83 int transformToBin( int dnum, int bin[ ] ) ;
     84 
     85 int main( )
     86 {
     87 int array[32]={0}; //保存转换后的二进制数
     88 int num; //待转换的整数
     89 int cc; //最后得到的二进制总共多少位
     90 printf(“请输入一个整数:”);
     91 scanf(“%d”, &num);
     92 cc = transformToBin( num, array ); //调用转换函数
     93 cc--; //往回退一个元素下标,使cc指向最后一个元素
     94 for( ; cc>=0; cc-- ) //输出转换后的二进制数
     95 printf(“%d”, array[cc]);
     96 printf(“
    ”);
     97 return 0;
     98 }
     99 int transformToBin( int dnum, int bin[ ] )
    100 {
    101 int count = 0;
    102 while ( dnum ) //当dnum还未转换完毕 
    103 {
    104 bin[count++] = dnum % 2; //余数保留到数组对应元素中
    105 dnum /= 2; //数本身除2
    106 }
    107 return count;
    108 }

     

    18、

    假设圆柱的底面积半径为r= 2.5),高为h= 3.5),编写求体积(体积=底面积*高)的程序。

     

     1 #include <stdio.h>
     2 #define PI 3.1415926
     3 int main( )
     4 {
     5 double r = 2.5;
     6 double h = 3.5;
     7 double v=0;
     8 v = (PI * r * r)*h;
     9 printf(“v = %lf
    ”, v);
    10 return 0;
    11 }

     

    19、

    编写函数,将字符串str中的小写字母全部转换成大写字符串。函数原型可声明为:“void toUpperCase( char str[ ]) ; ”

     

    #include <stdio.h>
    #include <string.h>
    
    void toUpperCase( char str[ ]);
    
    int main( )
    {
    char str[100];
    puts(“请输入字符串:”);
    gets(str);
    toUpperCase ( str );
    puts(“转换后的字符串:”);
    puts( str );
    return 0;
    }
    
    void toUpperCase( char str[ ])
    {
    int i;
    i = 0;
    while ( str[i] != ‘0’ )
    {
    if( str[i] >= ‘a’ && str[i] <= ‘z’ )
    str[i] -= 32; //小写字母转换成大写
    ++i;
    }
    }

     

    20、

    编写函数,删除字符串str中的所有ch字符。函数原型可声明为“void deleteAll( char str[ ] , char ch ) ;”,参数str为待处理的字符串,ch为要删除的字符。

     

     1 #include <stdio.h>
     2 #include <string.h>
     3 void deleteAll( char str[ ] , char ch );
     4 
     5 int main( )
     6 {
     7 char str[100] , ch ;
     8 puts(“请输入字符串:”);
     9 gets(str);
    10 printf(“请输入待删除的字符:”);
    11 ch = getchar( );
    12 deleteAll ( str , ch );
    13 puts(“删除之后的字符串:”);
    14 puts( str );
    15 return 0;
    16 }
    17 void deleteAll( char str[ ] , char ch )
    18 {
    19 int oldi; //指向原str的
    20 int newi; //指向删除ch后的str
    21 oldi = 0 ;
    22 newi = 0;
    23 while ( str[ oldi ] != ‘0’ )
    24 {
    25 if( str[oldi] != ch ) //不是要删除的字符
    26 {
    27 str[newi] = str[oldi]; //将其复制到新的字符串中
    28 ++newi; //新字符串增长一个元素
    29 }
    30 ++oldi; //如果是要删除的字符,则会默认被跳过
    31 }
    32 str[newi] = ''; //新字符串置结束标志
    33 }

     

    21、

    编写函数,用字符ch2替换字符串str中的字符ch1(注意:要全部都替换掉)。函数原型可声明为“void replaceAll( char str[ ], char ch1, char ch2 ) ;”

     

     1 #include <stdio.h>
     2 #include <string.h>
     3 void replaceAll( char str[ ], char ch1, char ch2 );
     4 
     5 int main( )
     6 {
     7 char str[100] , ch1 , ch2 ;
     8 puts(“请输入字符串:”);
     9 gets(str);
    10 printf(“请输入要被替换的字符及用来替换的字符(中间不要用空格分隔):”);
    11 ch1 = getchar( );
    12 ch2 = getchar( );
    13 replaceAll ( str , ch1, ch2 );
    14 puts(“替换之后的字符串:”);
    15 puts( str );
    16 return 0;
    17 }
    18 
    19 void replaceAll( char str[ ], char ch1, char ch2 )
    20 {
    21 int i;
    22 i = 0;
    23 while ( str[i] != ‘0’ )
    24 {
    25 if( str[i] == ch1 )
    26 str[i] = ch2;
    27 ++i;
    28 }
    29 }

     

    22、

    编写程序,统计一个字符串中26个字母出现的次数(不区分大小写)。函数原型可声明为:“void countAlpha (char str[ ], int count[] );”,参数str为待处理的字符串,数组count长度为26,用于存放26个字母出现的次数。

     

     1 #include <stdio.h>
     2 #include <string.h>
     3 void countAlpha (char str[ ], int count[] );
     4 
     5 int main( )
     6 {
     7 char str[100];
     8 int count[26]={0};
     9 int i;
    10 puts(“请输入字符串:”);
    11 gets(str); 
    12 countAlpha ( str , count );
    13 puts(“统计结果:”);
    14 for( i=0; i<26; ++i) //输出个数不为0的字母及其个数
    15 if ( count[i] )
    16 printf(“%c------%d
    ”, i+’a’, count[i]);
    17 return 0;
    18 }
    19 void countAlpha (char str[ ], int count[] )
    20 {
    21 int i;
    22 i = 0;
    23 while (str[i] != ‘0’ )
    24 {
    25 if( str[i]>=’a’ && str[i] <= ‘z’)
    26 ++count[ str[i] – ‘a’ ];
    27 else if( str[i]>=’A’ && str[i] <= ‘Z’)
    28 ++count[ str[i] – ‘A’ ];
    29 ++i;
    30 }
    31 }

     

    23、

    编写程序输出下列图案 : 
    *
    ***
    *****
    *******

    1 #include <stdio.h>
    2 int main( )
    3 {
    4 printf(“ *
    ”);
    5 printf(“ * * *
    ”);
    6 printf(“ * * * * *
    ”);
    7 printf(“ * * * * * * *
    ”);
    8 return 0;
    9 }

    24、

    编写程序,从键盘输入两个字符分别存放在变量xy中,要求通过程序交换它们的值。

     

     1 #include <stdio.h>
     2 int main( )
     3 {
     4 char x, y;
     5 char tmp;
     6 printf(“Input two characters:”);
     7 scanf(“%c%c”, &x, &y);
     8 printf(“Before swap: x=%c, y=%c
    ”, x, y);
     9 tmp = x;
    10 x = y;
    11 y = tmp;
    12 printf(“After swap: x=%c, y=%c
    ”, x, y);
    13 return 0;
    14 }

     

    25、

    Write a program to evaluate the polynomial shown here: for x = 2.55.
    3x3-5x2+6.

     1 #include <stdio.h>
     2 #include <math.h>
     3 int main( )
     4 {
     5 double a=3, b=-5,c=2;
     6 double x = 2.55;
     7 double root1, root2;
     8 double delt;
     9 delt = b*b – 4*a*c;
    10 root1 = ( -b + sqrt( delt ) ) / (2 * a) ;
    11 root2 = ( -b - sqrt( delt ) ) / (2 * a) ;
    12 printf(“The two roots are:
    ”);
    13 printf(“root1 = %lf
     root2 = %lf
    ”, root1, root2);
    14 return 0;
    15 }

    26、

    编写程序。功能:从读入的整数数据中,统计大于零的整数个数和小于零的整数个数。用输入零来结束输入,程序中用变量i统计大于零的整数个数,用变量j统计小于零的整数个数。

     

     1 #include <stdio.h>
     2 int main( )
     3 {
     4 int num; //输入的整数
     5 int iCount = 0; //大于0的整数的计数变量
     6 int jCount = 0; //小于0的整数的计数变量
     7 printf(“请输入多个整数:(以0结束输入)”);
     8 scanf(“%d”, &num);
     9 while ( num ) // while ( num != 0 )
    10 {
    11 if ( num > 0 ) ++iCount;
    12 else if ( num < 0 ) ++jCount;
    13 scanf(“%d”, &num);
    14 }
    15 printf(“大于0的个数:%d
    ”, iCount);
    16 printf(“小于0的个数:%d
    ”, jCount);
    17 return 0;
    18 }

     

    27、

    功能:以每行5个数来输出300以内能被717整除的偶数,并求出其和。

     

     1 #include <stdio.h>
     2 int main( )
     3 {
     4 int n; //300以内的数
     5 int count = 0; //满足条件的数的统计变量
     6 int sum = 0; //求和变量
     7 for( n = 1; n <= 300; ++n)
     8 {
     9 if ( n % 2 == 0 && (n % 7 ==0 || n % 17 == 0) ) //是偶数,且能被7或17整除
    10 {
    11 sum += n; //求和
    12 printf(“%5d”, n);//输出n值
    13 ++count;
    14 if( count % 5 == 0 ) //某行够5个数了,则换行
    15 printf(“
    ”);
    16 }
    17 }
    18 printf(“
    ”);
    19 return 0;
    20 }

     

    28、

    Write a function called prime that returns 1 if its argument is a prime number and returns 0 otherwise.

     1 #include <stdio.h> 
     2 #include <math.h>
     3 int prime( int n ); //函数声明
     4 
     5 int main( )
     6 {
     7 int num;
     8 int isPrime;
     9 printf(“Please input an integer:”);
    10 scanf(“%d”, &num);
    11 isPrime = prime( num ); //函数调用
    12 if ( 1 == isPrime )
    13 printf(“ %d is a prime 
    ” , num);
    14 else
    15 printf(“ %d is not a prime
    ” , num );
    16 return 0;
    17 }
    18 int prime( int n ) //函数定义
    19 {
    20 int i;
    21 for( i = 2; i <= sqrt( n ) ; ++i)
    22 if ( n % i == 0 ) break;
    23 
    24 if ( i > sqrt ( n ) )
    25 return 1;
    26 else
    27 return 0;
    28 }

    29、

    功能:计算并输出500以内最大的10个能被1317整除的自然数之和。

     

     1 #include <stdio.h>
     2 int main( )
     3 {
     4 int n; //500-1之间的整数
     5 int sum = 0; //求和变量
     6 int count = 0; //满足条件的数的个数
     7 for ( n=500; n>0; --n)
     8 {
     9 if ( n % 13 == 0 || n % 17 == 0 )
    10 {
    11 sum += n;
    12 ++count;
    13 if ( count >= 10 ) //有10个满足条件的整数,提前结束循环
    14 break;
    15 }
    16 }
    17 printf(“sum = %d
    ”, sum );
    18 return 0;
    19 }

     

    30、

    功能:分别求出一批非零整数中的偶数、奇数的平均值,用零作为终止标记。

     1 #include <stdio.h>
     2 int main( )
     3 {
     4 int n;//整数
     5 int eSum = 0; //奇数的和
     6 int oSum = 0; //偶数的和
     7 int eCount = 0; //奇数的个数
     8 int oCount = 0; //偶数的个数
     9 printf(“请输入若干个整数(以0结束输入):”);
    10 scanf(“%d”, &n);
    11 while ( n ) //当n不为0 
    12 {
    13 if ( n % 2 ) //n为奇数
    14 { eSum += n; ++eCount; }
    15 else //否则, n为偶数
    16 { oSum += n; ++oCount; }
    17 scanf(“%d”, &n); //输入下一个整数
    18 }
    19 printf(“奇数平均值:%f
    ”, 1.0 * eSum / eCount );
    20 printf(“偶数平均值:%f
    ”, 1.0 * oSum / oCount );
    21 return 0;
    22 }

    31、

    功能:百马百担问题:有100匹马,驮100担货,大马驮三担,中马驮2担,两匹小马驮一担,求大、中、小马各多少匹?

     

     1 #include <stdio.h>
     2 int main( )
     3 {
     4 int bigHorse, midHorse, littleHorse; //大、中、小马的数量
     5 for ( bigHorse = 0; bigHorse <= 33; ++bigHorse )
     6 for( midHorse = 0; midHorse <= 50; ++midHorse )
     7 {
     8 littleHorse = 100 – bigHorse – midHorse; //小马数量
     9 if ( littleHorse % 2 == 0 && (3*bigHorse + 2*midHorse + littleHorse / 2 == 100 ) ) //小马数量是偶数,且三种马驮的货是100担
    10 printf(“大马:%d, 中马:%d, 小马:%d
    ”, bigHorse, midHorse, littleHorse );
    11 }
    12 return 0;
    13 }

     

    32、

    功能:百鸡问题:100元买100只鸡,公鸡一只5元钱,母鸡一只3元钱,小鸡一元钱三只,求100元钱能买公鸡、母鸡、小鸡各多少只?

     

     1 #include <stdio.h>
     2 int main( )
     3 {
     4 int cock, hen, chick; //公鸡、母鸡、小鸡的数量
     5 for (cock = 0; cock <= 20; ++ cock)
     6 for(hen = 0; hen <= 33; ++ hen)
     7 {
     8 chick = 100 –cock–hen; //小鸡数量
     9 if (chick % 3 == 0 && (5* cock + 3* hen + chick / 3 == 100 ) ) //小鸡数量是3的倍数,且三种鸡的总钱数是100
    10 printf(“公鸡:%d, 母鸡:%d, 小鸡:%d
    ”, cock, hen, chick);
    11 }
    12 return 0;
    13 }

     

    33、

    功能:在屏幕上用*输出一个漏斗状的图形。

     

     1 #include <stdio.h>
     2 void printFunnel( int n); //函数声明
     3 int main( )
     4 {
     5 int n; //上半部的总行数
     6 printf(“请输入漏斗上半部的总行数:”);
     7 scanf(“%d”, &n);
     8 printFunnel( n ); //调用函数打印漏斗形
     9 return 0;
    10 }
    11 
    12 void printFunnel( int n)
    13 {
    14 int row; //行号
    15 int starCount; // 某行上星号的数量
    16 int spaceCount; //某行上空格的数量
    17 for( row = 1; row <= n; ++row)
    18 {
    19 for( spaceCount = 1; spaceCount <= row – 1 ; ++ spaceCount)
    20 printf(“ “); //打印出某行上星号前的空格
    21 for( starCount = 1; starCount <= 2*( n – row ) + 1; ++starCount )
    22 printf(“* “); //打印出某行上的所有星号
    23 printf(“
    ”); //换行
    24 }
    25 
    26 //打印下半部分(可看成一个n-2行的三角形状)
    27 for( row = 2; row <= n; ++row)
    28 {
    29 for( spaceCount = 1; spaceCount <= n - row ; ++ spaceCount)
    30 printf(“ “); //打印出某行上星号前的空格
    31 for( starCount = 1; starCount <= 2* row - 1; ++starCount )
    32 printf(“* “); //打印出某行上的所有星号
    33 printf(“
    ”); //换行
    34 }
    35 }

     

    34、

    功能:输出1001000之间的各位数字之和能被15整除的所有数,输出时每10个数一行。

     

     1 #include <stdio.h> 
     2 int main( )
     3 {
     4 int n;//100到1000之间的整数
     5 int tmpn;//临时存储n
     6 int sum; //统计n的各位数字之和
     7 int count = 0; //输出数的个数
     8 for ( n = 100; n <= 1000; ++n )
     9 {
    10 tmpn = n; //将n临时存储到tmpn中
    11 sum = 0; //每个n要重新计算各位数字之和
    12 while ( tmpn ) //当tmpn不为0时
    13 {
    14 sum += tmpn % 10;
    15 tmpn /= 10;
    16 }
    17 if ( sum % 15 == 0 ) //如果n满足指定的条件
    18 {
    19 printf(“%5d”, n); //输出当前n值
    20 ++count;
    21 if ( count % 10 == 0 ) //一行输出了10个整数,则换行
    22 printf(“
    ”);
    23 }
    24 }
    25 printf(“
    ”);
    26 return 0;
    27 }

     

    35、

    编写程序,从键盘输入两个数字字符并分别存放在字符型变量xy中,要求通过程序将这两个字符对应的数字相加后输出。

     

     1 #include <stdio.h>
     2 int main( ) 
     3 {
     4 char x , y;
     5 printf(“Input two characters:”);
     6 scanf(“%c%c”, &x, &y);
     7 if( x>=’0’&&x<=’9’ && y>=’0’&&y<=’9’ )
     8 printf(“%d 
    ”, x-'0'+y-'0');
     9 return 0;
    10 }

     

    36、

    某市不同车牌的出租车3公里的起步价和计费分别为:夏利7/公里,3公里以外2.1/公里;富康8/公里,3公里以外2.4/公里;桑塔纳9元,3公里以外2.7/公里。编程:从键盘输入乘车的车型及公里数,输出应付的车资。

     

     1 #include <stdio.h>
     2 int main( )
     3 {
     4 int carType;//车型。1代表夏利;2代表富康;3代表桑塔纳
     5 double xiali = 2.1; //每公里价格
     6 double fukang = 2.4;
     7 double sangtana = 2.7;
     8 double distance; //距离
     9 double totalMoney;//总的收费
    10 printf("请输入您乘坐的车型:1代表夏利;2代表富康;3代表桑塔纳:");
    11 scanf("%d", &carType);
    12 printf("请输入您乘车的总路程:");
    13 scanf("%lf", &distance);
    14 if( carType == 1)//夏利
    15 {
    16 if( distance < 3 )
    17 totalMoney = 7.0;
    18 else
    19 totalMoney = 7 + xiali * (distance – 3);
    20 }
    21 else if( carType == 2 ) //富康
    22 {
    23 if( distance < 3 )
    24 totalMoney = 8.0;
    25 else
    26 totalMoney = 8 + fukang * (distance – 3);
    27 }
    28 else if( carType == 3 ) //富康
    29 {
    30 if( distance < 3 )
    31 totalMoney = 9.0;
    32 else
    33 totalMoney = 9 + sangtana * (distance – 3);
    34 }
    35 
    36 printf("(四舍五入)您的车费为:%.0lf
    ", totalMoney );
    37 return 0;
    38 }

     

    37、

    编写程序,从键盘上输入一个小写字母,将其转化为大写字母。

     1 #include <stdio.h>
     2 int main( ) 
     3 {
     4 char lower;
     5 printf( “Input a lower alpha:”);
     6 scanf(“%c”, &lower);
     7 if( lower>=’a’ && lower<=’z’ )
     8 printf(“%c 
    ”, lower - 32);
     9 return 0;
    10 }

    38、

    编写函数,将数组s1中的全部奇数都复制到数组s2中。函数原型可声明为:“int copyTo( int * s1 , int n, int * s2 ) ; ”,参数s1s2为指向两个数组的指针,n为数组s1中元素的个数,返回值为复制完成后s2中元素的个数。

     

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <time.h>
     4 int copyTo( int *s1, int n , int *s2 );
     5 int main( )
     6 {
     7 int s1[100], s2[100];
     8 int count; //最终奇数的个数
     9 int i;
    10 srand( time ( NULL ) ); //随机种子
    11 for ( i = 0; i < 100; ++i ) //避免每次执行程序重复输入
    12 s1[i] = rand( ) % 1001 ; //随机生成0-1000之间的整数
    13 for ( i = 0; i < 100; ++i ) //打印出数组s1元素
    14 printf(“%5d”, s1[i] ) ;
    15 printf(“
    ”);
    16 count = copyTo( s1, 100, s2 );
    17 
    18 printf(“奇数如下:
    ”);
    19 for ( i = 0; i < count; ++i ) //打印出数组s2元素
    20 printf(“%5d”, s2[i] ) ;
    21 printf(“
    ”);
    22 return 0;
    23 }
    24 int copyTo( int *s1, int n , int *s2 )
    25 {
    26 int *ps1, *ps2;
    27 ps1 = s1; ps2 = s2;
    28 while ( ps1 < s1 + n )
    29 {
    30 if( *ps1 % 2) //是奇数
    31 *ps2++ = *ps1;
    32 ps1++;
    33 }
    34 return ps2 – s2;
    35 }

     

    39、

    编写函数,将字符串中的大写字母转换为小写字母。函数原型可声明为“void strToLow( char * str ) ;”,参数str是要转换的字符串。

     

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 void toLowerCase( char *str);
     5 
     6 int main( )
     7 {
     8 char str[100];
     9 puts(“请输入字符串:”);
    10 gets(str);
    11 toLowerCase ( str );
    12 puts(“转换后的字符串:”);
    13 puts( str );
    14 return 0;
    15 }
    16 
    17 void toLowerCase ( char *str )
    18 {
    19 while ( *str )
    20 {
    21 if(*str >= ‘A’ && *str <= ‘Z’ )
    22 *str += 32; //小写字母转换成大写
    23 ++str;
    24 }
    25 }

     

    40、

    Write a program that asks the user to type in two integer values at the terminal. Test these two number to determine if the first is evenly divisible by the second, and then display an appropriate message at the terminal.

     1 #include <stdio.h>
     2 int main( )
     3 {
     4 int num1, num2;
     5 printf(“Input two integers:”);
     6 scanf(“%d%d”, &num1, &num2);
     7 if ( 0 == num1 % num2 )
     8 printf(“ %d can be evenly divisible by %d
    ”, num1, num2);
     9 else
    10 printf(“ %d can not be evenly divisible by %d
    ”, num1, num2);
    11 return 0;
    12 }

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     



  • 相关阅读:
    if __name__ == '__main__' 该如何理解
    Github下载慢和下载过程中断等情况的解决方案
    Git下载安装及设置详细教程
    冒烟测试是什么?
    100道MySQL数据库经典面试题解析
    Linux 下ZooKeeper安装
    运用Docker+Jenkins+Nginx+Spring Boot 自动化部署项目
    linux本地远程上传&下载阿里云oss的shell脚本实现方法
    运行java项目shell简洁脚本
    阿里云oss利用工具上传图片文件
  • 原文地址:https://www.cnblogs.com/yjh123/p/5576117.html
Copyright © 2011-2022 走看看