日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言数组问题解析

發(fā)布時(shí)間:2023/12/19 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言数组问题解析 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

#import <Foundation/Foundation.h>

#define COUNT 10


int main(int argc, const char * argv[])

{

? ? //1、隨機(jī)產(chǎn)生20個(gè)[10 , 50]的正整數(shù)存到數(shù)組中,并求數(shù)組中的所有元素最大值、最小值、平均值以及各元素之和。第二大值。

//? ? int max = 0, min = 0, sum = 0, secondMax = 0;

//? ? float ave = 0.0;

//? ? int a[20] = {0};

//? ? //給數(shù)組元素賦值

//? ? for (int i = 0; i < 20; i++) {

//? ? ? ? a[i] = arc4random() % (50 - 10 + 1) + 10;

//? ? ? ? printf("%d ", a[i]);

//? ? }

//? ? printf("\n");

//? ? //求數(shù)組元素的最大值以及第二大值

//? ? for (int i = 0; i < 20; i++) {

//? ? ? ? if (a[i] > max) {

//? ? ? ? ? ? secondMax = max;

//? ? ? ? ? ? max = a[i];

//? ? ? ? } else if (a[i] != max && a[i] > secondMax) {

//? ? ? ? ? ? secondMax = a[i];

//? ? ? ? }

//? ? }

//? ? min = a[0];

//? ? //求數(shù)組元素的最小值

//? ? for (int i = 1; i < 20; i++) {

//? ? ? ? if (a[i] < min) {

//? ? ? ? ? ? min = a[i];

//? ? ? ? }

//? ? }

//? ? //求數(shù)組元素的和

//? ? for (int i = 0; i < 20; i++) {

//? ? ? ? sum += a[i];

//? ? }

//? ? ave = sum / 20.0;

//? ? printf("max = %d, min = %d, secondMax = %d, sum = %d, ave = %.2f\n", max, min, secondMax, sum, ave);

?? ?

? ? //2.編程在一個(gè)已知的字符串中查找最長單詞,假定字符串中只含字母和空格,用空格來分隔單詞。

//? ? char str[255] = {0};

//? ? printf("請(qǐng)輸入一個(gè)字符串:\n");

//? ? scanf("%[^\n]", str);

? ? gets(str);

//? ? int maxLength = 0, maxIndex = 0;

//? ? int length = 0;

//? ? int i = 0;

//? ? while (str[i] != '\0') {

//? ? ? ? if (str[i] != ' ') {

//? ? ? ? ? ? length++;

//? ? ? ? } else {

//? ? ? ? ? ? if (maxLength < length) {

//? ? ? ? ? ? ? ? maxLength = length;

//? ? ? ? ? ? ? ? maxIndex = i - maxLength;

//? ? ? ? ? ? }

//? ? ? ? ? ? length = 0;

//? ? ? ? }

//? ? ? ? i++;

//? ? }

//? ? if (maxLength < length) {

//? ? ? ? maxLength = length;

//? ? ? ? maxIndex = i - maxLength;

//? ? }

//? ? for (int j = maxIndex; j < maxIndex + maxLength; j++) {

//? ? ? ? printf("%c", str[j]);

//? ? }

?? ?

? ? //3、耶穌有13個(gè)門徒,其中有一個(gè)就是出賣耶穌的叛徒,請(qǐng)用排除法找出這位叛徒:13人圍坐一圈,從第一個(gè)開始報(bào)號(hào):123123...。凡是報(bào)到“3”就退出圈子,最后留在圈子內(nèi)的人就是出賣耶穌的叛徒。請(qǐng)找出它原來的序號(hào)。

//? ? int a[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};

//? ? int number = 13; //記錄圈內(nèi)剩余的人數(shù)

//? ? int count = 0; //通過計(jì)算器來模擬報(bào)數(shù)

//? ? int i = 0; //循環(huán)變量

//? ? while (number > 1) {

//? ? ? ? if (a[i] != 0) {

//? ? ? ? ? ? count++;

//? ? ? ? }

//? ? ? ? if (count == 3) {

//? ? ? ? ? ? a[i] = 0;? //讓該元素置0,模擬踢出圈子

//? ? ? ? ? ? count = 0; //然后計(jì)數(shù)器重新歸0

//? ? ? ? ? ? number--;? //踢出圈子之后人數(shù)-1

//? ? ? ? }

//? ? ? ? i++;

//? ? ? ? if (i == 13) {

//? ? ? ? ? ? i = 0; //當(dāng)?shù)阶詈笠粋€(gè)人的時(shí)候, 再轉(zhuǎn)到第一個(gè)人繼續(xù)報(bào)數(shù)

//? ? ? ? }

//? ? }

//? ? for (int j = 0; j < 13; j++) {

//? ? ? ? if (a[j] > 0) {

//? ? ? ? ? ? printf("叛徒就是第%d個(gè)人.", a[j]);

//? ? ? ? }

//? ? }

?? ?

? ? //4.將兩個(gè)排好序的數(shù)組,合并到另外一個(gè)數(shù)組中,并且合并之后的數(shù)組也是有序的。

? ? int a[10] = {0};

? ? int b[10] = {0};

? ? int c[20] = {0};

? ? //給數(shù)組a賦值

? ? for (int i = 0; i < 10; i++) {

? ? ? ? a[i] = arc4random() % (40 - 20 + 1) + 20;

? ? }

? ? //給數(shù)組b賦值

? ? for (int i = 0; i < 10; i++) {

? ? ? ? b[i] = arc4random() % (40 - 20 + 1) + 20;

? ? }

? ? //將數(shù)組a排序

? ? for (int i = 0; i < 10 - 1; i++) {

? ? ? ? for (int j = 0; j < 10 - 1 - i; j++) {

? ? ? ? ? ? if (a[j] > a[j + 1]) {

? ? ? ? ? ? ? ? int temp = a[j];

? ? ? ? ? ? ? ? a[j] = a[j + 1];

? ? ? ? ? ? ? ? a[j + 1] = temp;

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? //將數(shù)組b排序

? ? for (int i = 0; i < 10 - 1; i++) {

? ? ? ? for (int j = 0; j < 10 - 1 - i; j++) {

? ? ? ? ? ? if (b[j] > b[j + 1]) {

? ? ? ? ? ? ? ? int temp = b[j];

? ? ? ? ? ? ? ? b[j] = b[j + 1];

? ? ? ? ? ? ? ? b[j + 1] = temp;

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? //輸出數(shù)組a

? ? for (int i = 0; i < 10; i++) {

? ? ? ? printf("%d ", a[i]);

? ? }

? ? printf("\n");

? ? //輸出數(shù)組b

? ? for (int j = 0; j < 10; j++) {

? ? ? ? printf("%d ", b[j]);

? ? }

? ? printf("\n");

? ? //將數(shù)組a和數(shù)組b合并到數(shù)組c

? ? int i = 0, j = 0, k = 0;

? ? while (i < 10 && j < 10 ) {

? ? ? ? if (a[i] > b[j]) {

? ? ? ? ? ? c[k++] = b[j++];

? ? ? ? } else {

? ? ? ? ? ? c[k++] = a[i++];

? ? ? ? }

? ? }

? ? while (i < 10) {

? ? ? ? c[k++] = a[i++];

? ? }

? ? while (j < 10) {

? ? ? ? c[k++] = b[j++];

? ? }

? ? //輸出排序好的數(shù)組c

? ? for (int m = 0; m < 20; m++) {

? ? ? ? printf("%d ", c[m]);

? ? }

?? ?

? ? //5.有一分?jǐn)?shù)序列:2/13/25/38/513/821/13...求出這個(gè)數(shù)列的前20項(xiàng)之和。

//? ? int element = 2, demo = 1;

//? ? int n = 0;

//? ? float sum = 2.0;

//? ? printf("請(qǐng)輸入求前幾項(xiàng)的和:\n");

//? ? scanf("%d", &n);

//? ? int temp = 0;

//? ? for (int i = 1; i < n; i++) {

//? ? ? ? temp = element;

//? ? ? ? element = element + demo;

//? ? ? ? demo = temp;

//? ? ? ? sum += element * 1.0 / demo;

//? ? }

//? ? printf("sum = %.2f\n", sum);

?? ?

? ? //6.給一個(gè)不多于5位的正整數(shù),要求:一、求它是幾位數(shù),二、逆序打印出各位數(shù)字。

//? ? int number = 0;

//? ? printf("請(qǐng)輸入一個(gè)不多于5位的正整數(shù):\n");

//? ? scanf("%d", &number);

//? ? int count = 0;

//? ? int copyNumber = number;

//? ? while (copyNumber) {

//? ? ? ? count++;

//? ? ? ? copyNumber /= 10;

//? ? }

//? ? printf("%d\n", count);

//? ? for (int i = 0; i < count; i++) {

//? ? ? ? printf("%d", number % 10);

//? ? ? ? number /= 10;

//? ? }

?? ?

? ? //7.,一個(gè)5位數(shù),判斷它是不是回文數(shù)。即12321是回文數(shù),個(gè)位與萬位相同,十位與千位相同。

//? ? int number = 0;

//? ? printf("請(qǐng)輸入一個(gè)數(shù):\n");

//? ? scanf("%d", &number);

//? ? int count = 0;

//? ? //先求出數(shù)字的位數(shù)

//? ? int copyNumber = number;

//? ? while (copyNumber) {

//? ? ? ? count++;

//? ? ? ? copyNumber /= 10;

//? ? }

//? ? int temp = 1;

//? ? for (int i = 0; i < count - 1; i++) {

//? ? ? ? temp *= 10;

//? ? }

//? ? BOOL isTrue = YES;

//? ? copyNumber = number;

//? ? for (int i = 0; i < count / 2; i++) {

//? ? ? ? int higher = number / temp;

//? ? ? ? int lower = number % 10;

//? ? ? ? if (higher != lower) {

//? ? ? ? ? ? isTrue = NO;

//? ? ? ? ? ? break;

//? ? ? ? }

//? ? ? ? number %= temp;

//? ? ? ? number /= 10;

//? ? ? ? temp /= 100;

//? ? }

//? ? if (isTrue) {

//? ? ? ? printf("%d是回文數(shù)", copyNumber);

//? ? } else {

//? ? ? ? printf("%d不是回文數(shù)", copyNumber);

//? ? }

? ? //8.請(qǐng)輸入星期幾的第一個(gè)字母來判斷一下是星期幾,如果第一個(gè)字母一樣,則繼續(xù)判斷第二個(gè)字母。

//? ? char letter = 0;

//? ? printf("請(qǐng)輸入第一個(gè)字母:\n");

//? ? scanf("%c", &letter);

//? ? switch (letter) {

//? ? ? ? case 'M':

//? ? ? ? ? ? printf("Monday");

//? ? ? ? ? ? break;

//? ? ? ? case 'T':

//? ? ? ? {

//? ? ? ? ? ? rewind(stdin); //清除緩沖區(qū)

//? ? ? ? ? ? //getchar(); 或者取出一個(gè)字符丟棄,丟棄的是\n

//? ? ? ? ? ? printf("請(qǐng)?jiān)佥斎氲诙€(gè)字符:\n");

//? ? ? ? ? ? scanf("%c", &letter);

//? ? ? ? ? ? switch (letter) {

//? ? ? ? ? ? ? ? case 'h':

//? ? ? ? ? ? ? ? ? ? printf("Thursday");

//? ? ? ? ? ? ? ? ? ? break;

//? ? ? ? ? ? ? ? case 'u':

//? ? ? ? ? ? ? ? ? ? printf("Tuesday");

//? ? ? ? ? ? ? ? ? ? break;

//? ? ? ? ? ? ? ? default:

//? ? ? ? ? ? ? ? ? ? break;

//? ? ? ? ? ? }

//? ? ? ? }

//? ? ? ? case 'W':

//? ? ? ? ? ? printf("Wednesday.");

//? ? ? ? ? ? break;

//? ? ? ? case 'F':

//? ? ? ? ? ? printf("Friday");

//? ? ? ? ? ? break;

//? ? ? ? case 'S':

//? ? ? ? {

//? ? ? ? ? ? rewind(stdin); //清除緩沖區(qū)

//? ? ? ? ? ? //getchar(); 或者取出一個(gè)字符丟棄,丟棄的是\n

//? ? ? ? ? ? printf("請(qǐng)?jiān)佥斎氲诙€(gè)字符:\n");

//? ? ? ? ? ? scanf("%c", &letter);

//? ? ? ? ? ? switch (letter) {

//? ? ? ? ? ? ? ? case 'a':

//? ? ? ? ? ? ? ? ? ? printf("Saturday");

//? ? ? ? ? ? ? ? ? ? break;

//? ? ? ? ? ? ? ? case 'u':

//? ? ? ? ? ? ? ? ? ? printf("Sunday");

//? ? ? ? ? ? ? ? ? ? break;

//? ? ? ? ? ? ? ? default:

//? ? ? ? ? ? ? ? ? ? break;

//? ? ? ? ? ? }

//? ? ? ? }

//? ? ? ? default:

//? ? ? ? ? ? break;

//? ? }

?? ?

? ? //9.1000000個(gè)數(shù),每個(gè)數(shù)取值范圍是0-999999

? ? //找出其中重復(fù)的數(shù),重復(fù)次數(shù)。

? ? //第一種方式

//? ? int a[COUNT] = {0};

//? ? int number = 0;

//? ? for (int i = 0; i < COUNT; i++) {

//? ? ? ? number = arc4random() % COUNT;

//? ? ? ? printf("%d ", number);

//? ? ? ? a[number]++;

//? ? }

//? ? printf("\n");

//? ? for (int i = 0; i < COUNT; i++) {

//? ? ? ? if (a[i] > 1) {

//? ? ? ? ? ? printf("%d : %d\n", i, a[i]);

//? ? ? ? }

//? ? }

? ? //第二種形式

//? ? int a[COUNT] = {0};

//? ? for (int i = 0; i < COUNT ; i++) {

//? ? ? ? a[i] = arc4random() % COUNT;

//? ? ? ? printf("%d ", a[i]);

//? ? }

//? ? printf("\n");

//? ? int temp = 0; //臨時(shí)變量,用來存儲(chǔ)取出來的數(shù)

//? ? int count = 1; //記錄重復(fù)的個(gè)數(shù)

//? ? for (int i = 0; i < COUNT; i++) {

//? ? ? ? temp = a[i];

//? ? ? ? for (int j = i + 1; j < COUNT; j++) {

//? ? ? ? ? ? if (a[j] == -1) {

//? ? ? ? ? ? ? ? continue;

//? ? ? ? ? ? }

//? ? ? ? ? ? if (a[j] == temp) {

//? ? ? ? ? ? ? ? count++;

//? ? ? ? ? ? ? ? a[j] = -1; //將已經(jīng)計(jì)算過的數(shù)置為-1.

//? ? ? ? ? ? }

//? ? ? ? }

//? ? ? ? if (count > 1) {

//? ? ? ? ? ? printf("%d : %d\n", temp, count);

//? ? ? ? ? ? count = 1;

//? ? ? ? }

//? ? }

? ? return 0;

}



轉(zhuǎn)載于:https://blog.51cto.com/wsx199312/1440767

總結(jié)

以上是生活随笔為你收集整理的c语言数组问题解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。