C言語クラシックプログラミング282ケース12

アレイ:

094要素の合計を求める

2次元配列を使用して、3行3列の配列を保存し、二重ループを使用して配列の各要素にアクセスしてから、各要素を累積します。

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>

  main()
{
    
    
    int i, j, a[3][3], sum = 0;
  
  	for(i =0; i < 3; i++)
	{
    
    
		for(j = 0; j < 3 ; j++)
		{
    
    
		 scanf("%d", &a[i][j]);
		}
	}
	
	for(i =0; i < 3; i++)
	{
    
    
		for(j = 0; j < 3 ; j++)
		{
    
    
			printf(" %4d", a[i][j]);
			sum += a[i][j];
		}
		printf("\n");
	}
	
	printf("sum = %d\n", sum);
  	printf("\n");
   
 }

0952次元配列を使用してデータを保存する

キーボードから2次元配列要素の割り当てを実現し、2次元配列を表示します

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>

  main()
{
    
    
    int i, j, a[2][3];
  
  	for(i =0; i < 2; i++)
	{
    
    
		for(j = 0; j < 3 ; j++)
		{
    
    
		  printf(" a[%d][%d]:", i, j);
		  scanf("%d", &a[i][j]);
		}
	}
	
	for(i =0; i < 2; i++)
	{
    
    
		for(j = 0; j < 3 ; j++)
		{
    
    
			printf(" %4d", a[i][j]);
		}
		printf("\n");
	}

  	printf("\n");
 }
 

096文字列に含まれる単語の数を数える

文字の行を入力し、単語の数を数えます。単語はスペースで区切る必要があり、最後の文字をスペースにすることはできません。

1gets()から文字列を取得します。
入力文字配列を決定します最初の文字がターミネータ/スペースであるかどうか
。2while()ループを使用して文字配列のスペースをカウントし、単語をカウントします。

_______me_____________
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>

  main()
{
    
    
    char a[100];
    int i = 0, count = 1;
    
    gets(a);
    
    while(a[i] != '\0')
	{
    
    
		
	    if(a[i] == ' ')
	    {
    
    
	    	count++;
		}
		i++;
		
	 } 
	 
	printf("%d", count);
  	printf("\n");
 }
 
————————官方——————————
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>

  main()
{
    
    
    char astring[100];
    int index = 0, iword = 1;
    char cblack;
    
    gets(astring);
    
    if(astring[0] == '\0')
    {
    
    
    	printf("There is no char!\n");
    	
	}
	else if(astring[0] == ' ')
	{
    
    
	   printf("first char just is a black!\n"); 
	} 
	else
	{
    
    
		for(index = 0; astring[index] != '\0'; index++) 
	    {
    
    
	    	cblack = astring[index];
	    	if(cblack ==' ')
	    	{
    
    
	    		iword++;
			}
		}
		
		printf("%d",iword);
	} 
  	printf("\n");
 }
 

097文字列コピー関数を実装するためにstrcpy()関数を使用しないでください

strcpy()関数を使用して文字列コピー関数を実装しないでください。主にgets()関数とputs()関数を使用して、文字の取得と出力を実現してください。

ここでは、文字列がb文字列にコピーされ、文字列を100文字で完全に埋めることはできず、ターミネータを使用して終了を判断できます。

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>

  main()
{
    
    
    char astring[100], bstring[100];
    int i = 0;
   
    gets(astring);
    while(astring[i] != '\0')
    {
    
    
    	bstring[i] = astring[i];
    	i++;
	}
 
    puts(bstring);
  	printf("\n");
 }
 

098データを逆の順序で保存する

5つのデータを任意に入力すると、プログラムはこれらの5つのデータを逆の順序で保存します。

配列bを定義し、配列aの値を逆の順序で配列bに保存します。
、デメリット:もう1つのアレイが定義されているため、冗長性が発生します

——————————ME——————————————————
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>

  main()
{
    
    
    int a[5], i, b[5], j;
    
    for(i = 0; i < 5; i++)
    {
    
    
    	scanf("%d", &a[i]);
	}
    
    for(i = 0,j = 4; i < 5; i++, j--)
    {
    
    
    	b[i] = a[j];
	}

	for(i = 0; i < 5; i++)
    {
    
    
    	printf("%4d", b[i]);
	}

  	printf("\n");
 }

中心点が左右反転し、

——————————官方——————————————
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>

  main()
{
    
    
    int a[5], i, temp;
    
    for(i = 0; i < 5; i++)
    {
    
    
    	scanf("%d", &a[i]);
	}
    
    for(i = 0; i < 5; i++)
    {
    
    
    	printf("%4d", a[i]);
	}
	printf("\n");
 
    for(i = 0; i < 2; i++)
    {
    
    
    	temp = a[i];
    	a[i] = a[4 - i];
    	a[4 - i] temp;
	}
    
	for(i = 0; i < 5; i++)
    {
    
    
    	printf("%4d", a[i]);
	}
	 
  	printf("\n");
 }
 

099隣接する要素の合計

キーボードから10個の整数を入力し、それらを配列aに保存し、a内の2つの隣接する要素の合計を見つけるようにプログラムし、それを配列bに保存します。

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>

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

100票カウント

選挙区長は、合計3名の候補者で、選挙に参加した人数と各人の選挙内容を入力し、3名の候補者の最終投票数と無効投票数を出力します。

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>

  main()
{
    
    
    int x[100], i, num;
	int  a = 0, b = 0, c = 0, d = 0;
    printf("输入选举人\n");
    scanf("%d", &num);
    
    for(i = 0; i <num; i++)
    {
    
    
    	scanf("%d", &x[i]);
	}
    
    for(i = 0; i < num; i++)
    {
    
    
        if(x[i] == 1)
        {
    
    
        	a++; 
		}
		else if(x[i] == 2)
        {
    
    
        	b++; 
		}
		else  if(x[i] == 3)
        {
    
    
        	c++; 
		}
		else
		{
    
    
			d++;
		}
	}
	printf("a候选人票数:%d\n", a);
	printf("b候选人票数:%d\n", b);
	printf("c候选人票数:%d\n", c);
	printf("无效票数:%d\n", c);
  	printf("\n");
 }

————————
参考書:C言語での古典的なプログラミングの282例
コンパイラソフトウェア:Dev-C ++

おすすめ

転載: blog.csdn.net/qq_41070511/article/details/113106276