本文目錄一覽:
- 1、C語言給數組中的元素按順序編號
- 2、C語言編程問題:給數組中的元素按順序編號
- 3、如何編寫C語言程序使得輸入編號就可以顯示其他信息
- 4、如何編寫C語言程序使得輸入編號就可以顯示其他的個人信息
- 5、c語言 整數編號
C語言給數組中的元素按順序編號
首先,for(i=0;i10;i++)
for(j=0;j10;j++)
for(i=0;i10;i++)
這三個for循環是打錯了?
其次你的比較那兒也有問題,試想,當執行到7的時候,7和1比,count = 1,7再和第二個1比,count等於2,後面兩個不變,但是你往b數組中存放的時候,count又加了1,肯定就變成3了,9同理,把演算法再改改
C語言編程問題:給數組中的元素按順序編號
最直接的辦法,用一個數組來作hash,時間複雜度最低,但是有最大值的限制,如下:
#includestdio.h
#define MAX_NUM 10000
#define n 10
int main()
{
int b[MAX_NUM] = {0};
int a[n],i,j;
int max = 0;
for(i=0;in;i++)
{
scanf(“%d”,a[i]);
if(a[i] = MAX_NUM)
{
printf(“invalid value [%d]! max than %d\n”,a[i], MAX_NUM);
return -1;
}
b[a[i]] = 1;
if(a[i] max)
{
max = a[i];
}
}
int idx = 1;
for(i = 0; i = max; i++)
{
if(b[i] 0)
{
b[i] = idx;
idx++;
}
}
for(i=0;in – 1;i++)
printf(“%d,”,b[a[i]]);
printf(“%d\n”, b[a[n – 1]]);
return 0;
}
———–
修改過的:
#includestdio.h
#define MAX_NUM 10000
#define n 10
int main() {
int b[MAX_NUM] = {0};
int a[n],i,j;
int max = 0;
for(i=0;in;i++) {
scanf(“%d”,a[i]);
if(a[i] = MAX_NUM)
return -1;
b[a[i]] = 1;
max = a[i] max ? a[i] : max;
}
int idx = 1;
for(i = 0; i = max; i++)
b[i] = b[i] 0 ? idx++ : b[i];
for(i=0;in – 1;i++)
printf(“%d,”,b[a[i]]);
printf(“%d\n”, b[a[n – 1]]);
return 0;
}
不過代碼長度有這麼重要麼?代碼的可讀性和正確性才是第一位的! 如果太糾結於長度,不如把所有代碼都寫道一行算了。多爽:「看,我的代碼一行就搞定了;」
如何編寫C語言程序使得輸入編號就可以顯示其他信息
簡單點的,你可以定義兩個數組A和B,A存放編號,B存放編號對應的其他信息,寫一個函數,返回類型為string,參數為編號,遍歷A數組,當找到編號匹配時記下是第i個元素,然後提取B中第i個元素對應的值並且返回。
如何編寫C語言程序使得輸入編號就可以顯示其他的個人信息
1、寫一個結構體數組用來記錄信息
這裡我寫了一個可以存儲一個人的姓名、電話、郵箱的結構體。
struct note{
char name[100];
char phone[100];
char mail[100];
}people[1000];
2、用文件儲存更加方便
p=fopen(“list.txt”,”r”);
if(p==NULL)
{
fclose(p);
p=fopen(“list.txt”,”w”);
fclose(p);
}
3、寫一個簡單的界面(可以用死循環)
while(1)
{
n=0;
p=fopen(“list.txt”,”r”);
while(fscanf(p,”%s%s%s”,people[n].name,people[n].phone,people[n].mail)!=EOF)
n++;
fclose(p);
///——–一次循環更新一次數據
4、寫一個簡單的查找程序
int k;
cout”輸入1讀取,輸入2輸入”endl;
cink;
if(k==1)
{
cout”輸入信息”endl;
char s[100];
cins;
bool ok=0;
for(i=0;istrlen(s);i++)
if(s[i]=’0’s[i]=’9′)
ok=1;
//自動識別輸入的是姓名還是電話號碼
if(ok==0)
{
//cout”通過姓名找到聯繫人”endl;
//system(“pause”);
bool you=0;
for(i=0;in;i++)
if(strcmp(s,people[i].name)==0)
{
you=1;
cout”姓名”people[i].nameendl;
cout”電話號碼”people[i].phoneendl;
cout”郵箱”people[i].mailendl;
}
if(you==0)
cout”沒有通過姓名找到聯繫人”endl;
}
if(ok==1)
{
//cout”通過電話找聯繫人”endl;
//system(“pause”);
bool you=0;
for(i=0;in;i++)
if(strcmp(s,people[i].phone)==0)
{
you=1;
cout”姓名”people[i].nameendl;
cout”電話號碼”people[i].phoneendl;
cout”郵箱”people[i].mailendl;
}
if(you==0)
cout”沒有通過電話找到聯繫人”endl;
}
5、添加信息的代碼
if(k==2)
{
p1=fopen(“list.txt”,”a+”);
char ss[1000];
cout”請輸入姓名”endl;
cinss;
fprintf(p1,”%s\n”,ss);
cout”請輸入電話”endl;
cinss;
fprintf(p1,”%s\n”,ss);
cout”請輸入郵箱”endl;
cinss;
fprintf(p1,”%s\n”,ss);
fclose(p1);
}
}
最終的程序
#include cstdio
#include cmath
#include cstring
#include algorithm
#include iostream
#include cstring
#include cmath
using namespace std;
FILE *p,*p1;
struct note{
char name[100];
char phone[100];
char mail[100];
}people[1000];
int main()
{
int n=0,i,j;
p=fopen(“list.txt”,”r”);
if(p==NULL)
{
fclose(p);
p=fopen(“list.txt”,”w”);
fclose(p);
}
while(1)
{
n=0;
p=fopen(“list.txt”,”r”);
while(fscanf(p,”%s%s%s”,people[n].name,people[n].phone,people[n].mail)!=EOF)
n++;
fclose(p);
///—————
int k;
cout”輸入1讀取,輸入2輸入”endl;
cink;
if(k==1)
{
cout”輸入信息”endl;
char s[100];
cins;
bool ok=0;
for(i=0;istrlen(s);i++)
if(s[i]=’0’s[i]=’9′)
ok=1;
if(ok==0)
{
//cout”通過姓名找到聯繫人”endl;
//system(“pause”);
bool you=0;
for(i=0;in;i++)
if(strcmp(s,people[i].name)==0)
{
you=1;
cout”姓名”people[i].nameendl;
cout”電話號碼”people[i].phoneendl;
cout”郵箱”people[i].mailendl;
}
if(you==0)
cout”沒有通過姓名找到聯繫人”endl;
}
if(ok==1)
{
//cout”通過電話找聯繫人”endl;
//system(“pause”);
bool you=0;
for(i=0;in;i++)
if(strcmp(s,people[i].phone)==0)
{
you=1;
cout”姓名”people[i].nameendl;
cout”電話號碼”people[i].phoneendl;
cout”郵箱”people[i].mailendl;
}
if(you==0)
cout”沒有通過電話找到聯繫人”endl;
}
}
if(k==2)
{
p1=fopen(“list.txt”,”a+”);
char ss[1000];
cout”請輸入姓名”endl;
cinss;
fprintf(p1,”%s\n”,ss);
cout”請輸入電話”endl;
cinss;
fprintf(p1,”%s\n”,ss);
cout”請輸入郵箱”endl;
cinss;
fprintf(p1,”%s\n”,ss);
fclose(p1);
}
}
return 0;
}
c語言 整數編號
這是你原來的方法,我改了兩行,
一個是put[j]=str[i],這一行要改成put[j]=str[i]-‘0’,這是從char 到 int的轉換,如果不變就成了把put[j]賦值成字元型str[i]的值了,
另一個在後面那個函數,return have使用之後,後面的代碼就不執行了,就好象短路一樣,
要放在後面,或是有條件的返回
可是改了這兩處之後還是不對,排出來結果差的太多,好象是你的冒泡內部的問題,要重新換種方法試試..我對排序很外行,查不出來根本所在
#include stdio.h
struct nn
{ int no;
int num;
};
typedef struct nn DATA;
int number( char * , DATA []);
int main( )
{
DATA b[100];
char sa[500]; //
int i, n;
gets( sa );
n = number( sa, b );
for ( i=0; in; i++ )
printf(“%d %d\n”, b[i].num, b[i].no );
return 0;
}
//
void bubbleSort(int arr[],int n)
{
int i,j,t;
for(i=0;in-1;i++)
{
for(j=0;jn-i-1;j++)
{
if(arr[j+1]arr[j])
{
t=arr[j+1];
arr[j+1]=arr[j];
arr[j]=t;
}
}
}
}
int number( char * str, DATA b[] )
{
int put[251],anshunxu[251],bianhao[251];
int i,j=0,have;
// 得數的個數
for(i=0;str[i]!=’\0′;i++)
{
if(str[i]!=’ ‘)
{
put[j]=str[i]-‘0’;
j++;
}
}
put[j]=’\0′;
have=j;
j=0;
//return have;
// 把put複製到anshunxu
for(i=0;ihave;i++)
{
anshunxu[i]=put[i];
}
// 冒泡排序
bubbleSort(anshunxu,have);
// 得到編號的數組
for(i=0;ihave;i++)
{
for(j=0;jhave;j++)
{
if(put[i]==anshunxu[j])
{
bianhao[i]=j+1;
}
}
}
bianhao[i]=’\0′;
for(i=0;ihave;i++)
{
b[i].num=put[i];
b[i].no=bianhao[i];
}
return have;
}
以上回答你滿意么?
原創文章,作者:XKYW,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/131596.html