本文目錄一覽:
- 1、C語言:找規律
- 2、【題目】找規律: 有一些由3個數組成的數組,它們
- 3、c語言編程,對任意一組數字,如{3,1,4,7,2,1,1,2,2},輸出其中出現次數最多的數字,並顯示出現的次數。
- 4、各位c語言大神求解啊!!如何檢驗一列數里是否含有數字3?謝謝!
- 5、c語言編寫程序求數列1,3,3,3,5,5,5,5,5,7,7,7,7,7,7,7的第四十項?
- 6、一個數列的規律是:1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,…。請你編程找出第n項是幾。n在10000以內。
C語言:找規律
#include stdio.h
void Update(char now[])
{
char result[128] = {‘\0’};
int i = 0;
char now_char;
int now_count = 0;
char new_str[16] = {‘\0’};
for (i = 0; i = strlen(now); i++)
{
if (0 == i)
{
now_char = now[i];
now_count = 1;
continue;
}
if (now[i] != now_char)
{
sprintf(new_str, “%d%c”, now_count, now_char);
strcat(result, new_str);
now_char = now[i];
now_count = 1;
continue;
}
now_count++;
}
strcpy(now, result);
return;
}
void main()
{
int N = 0;
int M = 0;
char ans[128];
scanf(“%d”, N);
while (N–)
{
scanf(“%d”, M);
strcpy(ans, “1”);
while (–M)
Update(ans);
printf(“%s\n”, ans);
}
return;
}
【題目】找規律: 有一些由3個數組成的數組,它們
c語言經典100題:【程序1】題目:有1,2,3,4個數字,能組成多少個互不相同且無重複數字的三位數都是多少1.程序分析:可填在百位,十位,個位的數字都是1,2,3,4.組成所有的排列後再去掉不滿足條件的排列.2.程序源代碼:main(){inti,j,k;printf(“\n”);for(i1;i2)/*如果是閏年且月份大於2,總天數應該加一天*/sum++;printf(“Itisthe%dthday.”,sum);}【程序5】題目:輸入三個整數x,y,z,請把這三個數由小到大輸出.1.程序分析:我們想法把最小的數放到x上,先將x與y進行比較,如果xy則將x與y的值進行交換,然後再用x與z進行比較,如果xz則將x與z的值進行交換,這樣能使x最小.2.程序源代碼:main(){intx,y,z,t;scanf(“%d%d%d”,x,y,z);if(xy){tx;xy;yt;}/*交換x,y的值*/if(xz){tz;zx;xt;}/*交換x,z的值*/if(yz){ty;yz;zt;}/*交換z,y的值*/printf(“smalltobig:%d%d%d\n”,x,y,z);}【程序6】題目:用*號輸出字母c的圖案.1.程序分析:可先用’*’號在紙上寫出字母c,再分行輸出.2.程序源代碼:#include”stdio.h”main(){printf(“helloc-world!\n”);printf(“****\n”);printf(“*\n”);printf(“*\n”);printf(“****\n”);}【程序7】題目:輸出特殊圖案,請在c環境中運行,看一看,verybeautiful!1.程序分析:字符共有256個.不同字符,圖形不一樣.2.程序源代碼:#include”stdio.h”main(){chara176,b219;printf(“%c%c%c%c%c\n”,b,a,a,a,b);printf(“%c%c%c%c%c\n”,a,b,a,b,a);printf(“%c%c%c%c%c\n”,a,a,b,a,a);printf(“%c%c%c%c%c\n”,a,b,a,b,a);printf(“%c%c%c%c%c\n”,b,a,a,a,b);}【程序8】題目:輸出9*9口訣.1.程序分析:分行與列考慮,共9行9列,i控制行,j控制列.2.程序源代碼:#include”stdio.h”main(){inti,j,result;printf(“\n”);for(i1;ib)a:b這是條件運算符的基本例子.2.程序源代碼:main(){intscore;chargrade;printf(“pleaseinputascore\n”);scanf(“%d”,score);gradescore90’a’:(score60’b’:’c’);printf(“%dbelongsto%c”,score,grade);}【程序15】題目:利用條件運算符的嵌套來完成此題:學習成績90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。1.程序分析:(ab)?a:b這是條件運算符的基本例子。2.程序源代碼:main(){intscore;chargrade;printf(“pleaseinputascore\n”);scanf(“%d”,score);gradescore90?’A’:(score60?’B’:’C’);printf(“%dbelongsto%c”,score,grade);}【程序16】題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數.1.程序分析:利用輾除法.2.程序源代碼:main(){inta,b,num1,num2,temp;printf(“pleaseinputtwonumbers:\n”);scanf(“%d,%d”,num1,num2);if(num1{tempnum1;num1num2;num2temp;}anum1;bnum2;while(b!0)/*利用輾除法,直到b為0為止*/{tempa%b;ab;btemp;}printf(“gongyueshu:%d\n”,a);printf(“gongbeishu:%d\n”,num1*num2/a);}【程序17】題目:輸入一行字符,分別統計出其中英文字母,空格,數字和其它字符的個數.1.程序分析:利用while語句,條件為輸入的字符不為’\n’.2.程序源代碼:#include”stdio.h”main(){charc;intletters0,space0,digit0,others0;printf(“pleaseinputsomecharacters\n”);while((cgetchar())!’\n’){if(c’a’c’a’c’0’c0){x1(x2+1)*2;/*第一天的桃子數是第2天桃子數加1後的2倍*/x2x1;day–;}printf(“thetotalis%d\n”,x1);}【程序22】題目:兩個乒乓球隊進行比賽,各出三人.甲隊為a,b,c三人,乙隊為x,y,z三人.已抽籤決定比賽名單.有人向隊員打聽比賽的名單.a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單.1.程序分析:判斷素數的方法:用一個數分別去除2到sqrt(這個數),如果能被整除,則表明此數不是素數,反之是素數.2.程序源代碼:main(){chari,j,k;/*i是a的對手,j是b的對手,k是c的對手*/for(i’x’;ia[j])minj;tema[i];a[i]a[min];a[min]tem;}/*outputdata*/printf(“Aftersorted\n”);for(i0;iprintf(“%5d”,a[i]);}【程序38】題目:求一個3*3矩陣對角線元素之和1.程序分析:利用雙重for循環控制輸入二維數組,再將a[i][i]累加後輸出.2.程序源代碼:main(){floata[3][3],sum0;inti,j;printf(“pleaseinputrectangleelement:\n”);for(i0;i50)againtrue;elseagainfalse;}}【程序46】題目:宏#define命令練習(1)程序源代碼:#include”stdio.h”#defineTRUE1#defineFALSE0#defineSQ(x)(x)*(x)voidmain(){intnum;intagain1;printf(“\40:Programwillstopifinputvaluelessthan50.\n”);while(again){printf(“\40:Pleaseinputnumber”);scanf(“%d”,num);printf(“\40:Thesquareforthisnumberis%d\n”,SQ(num));if(num50)againTRUE;elseagainFALSE;}}【程序47】題目:宏#define命令練習(2)程序源代碼:#include”stdio.h”#defineexchange(a,b){\/*宏定義中允許包含兩道衣裳命令的情形,此時必須在最右邊加上”\”*/intt;\ta;\ab;\bt;\}voidmain(void){intx10;inty20;printf(“x%d;y%d\n”,x,y);exchange(x,y);printf(“x%d;y%d\n”,x,y);}【程序48】題目:宏#define命令練習(3)程序源代碼:#definelag#definesmay)x:y#defineminimum(x,y)(xy)y:xvoidmain(){inta10,b20;#ifdefmaxprintf(“\40:thelargeroneis%d\n”,maximum(a,b));#elseprintf(“\40:theloweroneis%d\n”,minimum(a,b)
c語言編程,對任意一組數字,如{3,1,4,7,2,1,1,2,2},輸出其中出現次數最多的數字,並顯示出現的次數。
如果是任意大小的數字,那麼有些麻煩,可以設定兩個默認值代表最多的數字和最多的次數。接着去輪詢,並計數,如果次數大於默認值,替換即可。遍歷完成即可輸出那兩個值。
如果是0~9,或者字母的話,比較簡單。可以定義定長的數組,數組下標代表具體值,數組的內容代表值出現的次數,遍歷一遍原數組,得到次數。遍歷定長數組,得到值。
第一種方法代碼如下,第二種自己琢磨吧。
#include stdio.h
#include stdlib.h
int main(int argc, char const *argv[])
{
int num = 0; //用來存最多值的數值,默認為0
int coumt = 0; //用來存最多值的個數,默認為0
int temp = 0; //中間變量
int n = 0; //數組的個數
int *p = NULL; //開闢空間的首地址,等價於a[0]
printf(“Pls enter the number of arrays:”);
while(1)
{
scanf(“%d”, n);
if(n = 0)
printf(“Error is scanf,pls try again\n”);
else
break;
}
p = (int *)malloc(sizeof(int) * n);
if(p == NULL)
{
printf(“Error is malloc\n”);
return -1;
}
for (int i = 0; i n; ++i)
{
printf(“Pls enter the num for buf[%d]=”, i+1);
scanf(“%d”, p[i]);
}
for (int i = 0; i n; ++i) //簡單的遍歷查找
{
temp = 0;
for (int j = i; j n; ++j)
{
if (p[i] == p[j])
{
temp++;//計數
}
}
if(coumt temp)//如果次數大於默認值,替換
{
coumt = temp;
num = p[i];
}
}
printf(“The most common number is %d and the coumt is %d\n”, num, coumt);
return 0;
}
#include stdio.h
#include stdlib.h
int main(int argc, char const *argv[])
{
int coumt = 0; //用來存最多值的個數,默認為0
int temp = 0; //中間變量,用於計數
int n = 0; //數組的個數
int *p = NULL; //開闢空間的首地址,等價於a[0]
int *num = NULL; //開闢空間的首地址,等價於a[0]
int flag = 0; //定義一個標誌位,用於計數重複的次數的數值出現
printf(“Pls enter the number of arrays:”);
while(1)
{
scanf(“%d”, n);
if (n = 0)
printf(“Error is scanf,pls try again\n”);
else
break;
}
p = (int *)malloc(sizeof(int) * n);//存放你要的數組
if (p == NULL)
{
printf(“Error is malloc for p\n”);
return -1;
}
num = (int *)malloc(sizeof(int) * n);//存放最多數值的數組,最壞情況,沒有重複數字
if (num == NULL)
{
printf(“Error is malloc for num\n”);
return -1;
}
for (int i = 0; i n; ++i)
{
printf(“Pls enter the num for buf[%d]=”, i+1);
scanf(“%d”, p[i]);
}
for (int i = 0; i n; ++i) //簡單的遍歷查找,找出最大的次數
{
temp = 0;
for (int j = i; j n; ++j)
{
if (p[i] == p[j])
{
temp++;//計數
}
}
if (coumt temp)//如果次數大於默認值,替換
coumt = temp;
}
for (int i = 0; i n; ++i) //簡單的遍歷查找,找出重複的次數
{
temp = 0;
for (int j = i; j n; ++j)
{
if (p[i] == p[j])
{
temp++;//計數
}
}
if (coumt == temp)//如果次數等於最大值,存儲
{
num[flag] = p[i];
flag++;
}
}
for (int i = 0; i flag; ++i)
printf(“The most common number is %d and the coumt is %d\n”, num[i], coumt);
free(p);
free(num);
return 0;
}
各位c語言大神求解啊!!如何檢驗一列數里是否含有數字3?謝謝!
#includestdio.h
int main()
{
int i,a[10],tag;//數組的長度自己定
tag=0;
printf(“please innput data\n”);//輸入數字
for(i=0;i10;i++)
{
scanf(“%d”,a[i]);
getchar();
} //逐個輸入
for(i=0;i10;i++)
{
if(a[i]==3)
{
tag++;
}
else continue;
}
printf(“%d 3 have been found\n”,tag);
}}
c語言編寫程序求數列1,3,3,3,5,5,5,5,5,7,7,7,7,7,7,7的第四十項?
main(){
int i,n;
i=1;
n=15;
while (n=0){
n=n-i;
i=i+2;
}
printf(“%d”,i);
}
或:
#includestdio.h
main()
{
int i,j,k,cont=0;
for(i=1;cont15;i+=2)
{for(j=1;j=i;j++) { baiprintf(“%d “,i); cont++; k=i; }
}printf(“\n”);
printf(“the 15th is:%d\n”,k);
}
擴展資料:
有一類數列,既不是等差數列,也不是等比數列,若將這類數列適當拆開,可分為幾個等差、等比或常見的數列,然後分別求和,再將其合併即可.
例如:an=2n+n-1,可看做是2n與n-1的和
Sn=a1+a2+…+an
=2+0+22+1+23+2+…+2n+n-1
=(2+22+…+2n)+(0+1+…+n-1)
=2(2n-1)/(2-1)+(0+n-1)n/2
=2n+1+n(n-1)/2-2
參考資料來源:百度百科-數列求和
一個數列的規律是:1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,…。請你編程找出第n項是幾。n在10000以內。
#includestdio.h
int main()
{
int i,j,k,n,count;
while(scanf(“%d”,n)!=EOF)
{
for(i=1,count=0,k=0;countn;i++) //從1開始增加,直到出現了n個數為止
{
count+=i; //每到i就意味着增加了i個數
for(j=0;ji;j++,k++) //輸出i個i ,如果不需要輸出序列,只需找到第n個數,此循環可去掉。
{
if(kn) //只輸出n個數
printf(“%d “,i);
else
break;
}
}
printf(“\n第%d項為:%d\n”,n,i-1);
printf(“====================\n”);
}
return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/288535.html