首页 > 程序设计『Programing』 > 两道C语言题目

两道C语言题目

1:给定一个升序排列的数组,要求插入一个新数据后仍然升序输出,例如:原数据为:1,6,9,23,56,95插入新的一个数据50后输出变为:1,6,9,23,50,56,95.



算法(个人答案):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "stdio.h"
#include "windows.h"
 
 
void main(int argc, char* argv[])
{
int inputno,m,j,k,i;
int a[7]={1,6,9,23,56,95};
printf("Source number:\n");
for ( m=0;m<7;m++){
            printf("%d  ",a[m]);
        }
printf("\nPlease input ur number:\n");
scanf("%d",&inputno);
a[6]=inputno;
for ( i=0;i<7 ;i++)
    {
        if( (a[6]>a[i]) && (a[6]<a [i+1])){
            for ( j=7;j>i+1;j--){
                a[j]=a[j-1];
                }
                a[i+1]=inputno;
        }
    }
for ( k=0;k&lt;7;k++){
        printf("%d  ",a[k]);
    }
    //Sleep(10000);
}
</a>

2:打印一个图案,要求中间一排为“+”,例如:输入5时输出为:

算法(个人答案):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio .h>
#include <stdlib .h>
void main()
{
    int inputlines,i,j;
    printf("Please insert ur inputlines to type:\n");
    scanf("%d",&inputlines);
    printf("Output is like this:\n");
    for (i=0;i<inputlines ;i++)
    {
        for (j=0;j<inputlines;j++)
        {
         if (i==inputlines/2){
             putchar ('+');
         }else {
            if (j==inputlines/2){
               putchar('+');
            }else {
            putchar(' ');
            }
        }
        }
        putchar('\n');
    }
}

原创文章,转载请注明: 转载自火星信息安全研究院

本文链接地址: 两道C语言题目: http://www.h4ck.org.cn/2010/03/c-topic/

  1. 还没有评论
评论提交中, 请稍候...

留言

:p 8) :lol: =( :8 ;) :(( :o: :[ :) :D :-| :-[) :bloody: :cool: :choler: :love: :oups: :aie: :beurk:
可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">
Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks