算法笔记--http://codeup.cn/problem.php?cid=100000569(1)

1.题目描述
有一个已排好序的数组,要求输入一个数后,按原来排序的规律将它插入到数组中。

假设数组长度为10,数组中前9个数(这9个数要求从键盘上输入,输入时要满足自小到大的输入顺序)已经按从小到大进行排序。

然后再从键盘上输入一个整数,将此整数插入到前有序的9个数中,使得最终的10个数依然是从小到大有序的。

输入
第一行输入以空格分隔的9个整数数,要求按从小到大的顺序输入。

第二行输入一个整数

输出
从小到大输出这10个数,每个数一行。

#include<stdio.h>

void main(){

    int i,a[10];
    memset(a,0,sizeof(a));
    for(i=0;i<9;i++){
        scanf("%d",&a[i]);
    }
    int b;
    scanf("%d",&b);
    i=8;
    while(b<a[i]){
        a[i+1]=a[i];
        i--;

    }
    //i++;
    a[++i]=b;//注意这里
    for(int j=0;j<10;j++){
        printf("%d\n",a[j]);
    }

    }



2.题目描述
将一个长度为10的整型数组中的值按逆序重新存放。

如:原来的顺序为1,2,3,4,5,6,7,8,9,0,要求改为0,9,8,7,6,5,4,3,2,1

输入
从键盘上输入以空格分隔的10个整数。

输出
按相反的顺序输出这10个数,每个数占一行。

#include<stdio.h>

void main(){

    int i,a[10],b[10],temp;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(a));
    for(i=0;i<10;i++){
        scanf("%d",&a[i]);
    }
    for(i=0;i<10;i++){
        b[i]=a[9-i];
    }
    for(int j=0;j<10;j++){
        printf("%d\n",b[j]);
    }

    }



3.题目描述
按要求输入如下格式的杨辉三角

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

最多输出10层

输入
输入只包含一个正整数n,表示将要输出的杨辉三角的层数。
输出
对应于该输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开

#include<stdio.h>

void main(){

    int i,j,n;
    scanf("%d",&n);
    int a[n][n];
    memset(a,0,sizeof(a));
    for(i=0;i<n;i++){
            for(j=0;j<=i;j++){
                if(j==0||j==i){
                    a[i][j]=1;
                }
                else{
                    a[i][j]=a[i-1][j-1]+a[i-1][j];
                }
                printf("%d ",a[i][j]);
            }
            printf("\n");
    }


    }



4.题目描述
有一行电文,已按如下规律译成密码:

A–>Z a–>z

B–>Y b–>y

C–>X c–>x

… …

即第一个字母变成第26个字母,第i个字母变成第(26-i+1)个字母,非字母字符不变。要求根据密码译回原文,并输出。

输入
输入一行密文
输出
解密后的原文,单独占一行。

#include<stdio.h>
#include<string.h>
 void main() {
	char str1[10];
	int i;
	gets(str1);
	for (i = 0; i < 10; i++) {
		if (str1[i] >= 'a' && str1[i] <= 'z')

			str1[i]=97+122-str1[i];
		if (str1[i] >= 'A' && str1[i] <= 'Z')

		str1[i]=65+90-str1[i];
	}
	puts(str1);
    }



5.题目描述
比较两个字符串s1和s2的大小,如果s1>s2,则输出一个正数;若s1=s2,则输出0;若s1<s2,则输出一个负数。

要求:不用strcpy函数;两个字符串用gets函数读入。

例如:“A"与"C"相比,由于"A”<“C”,应输出负数,同时由于"A"与"C"的ASCII码差值为2,因此应输出"-2"。

同理:"And"和"Aid"比较,根据第2个字符比较的结果,“n"比"i"大5,因此应该输出"5”

输入
输入2行字符串

输出
一个整数,表示这两个字符串 比较的差值,单独占一行。

#include<stdio.h>
#include<string.h>
#define N 100
 void main() {
	char str1[N]="",str2[N]="";

	gets(str1);
	gets(str2);
	int n=0;
	for (int i = 0; i < N-1; i++) {
		n+=str1[i]-str2[i];

	}
	printf("%d",n);
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy InRelease' is not signed. W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://security.ubuntu.com/ubuntu jammy-security InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://security.ubuntu.com/ubuntu jammy-security InRelease' is not signed. W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy-updates InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy-updates InRelease' is not signed. W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy-backports InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy-backports InRelease' is not signed. E: Problem executing scripts APT::Update::Post-Invoke 'rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true' E: Sub-process returned an error code
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值