C語言輸入練習
⑴ c語言練習題:輸入n個字元串,將它們按字典順序輸出。(請用數組的指針和指針數組兩種方法做) 求計算
//下面是字典序:
#include<stdio.h>
#include<string.h>
voidswap(char*a,char*b)
{
chartemp=*a;
*a=*b;
*b=temp;
}
intnextperm(chara[],intn)//字典序排列(從升序到降序排列(也可從降序到升序))基於ASCII碼准則
{
inti,j,k=-1,l;
for(i=0;i<n-1;i++)//從前向後方向掃描,找到最後一對為升序的相鄰元素(如果不存在,則所有排列已完成)
{
if(a[i]<a[i+1])
k=i;//把最後一對為升序的相鄰元素(靠左邊)的下標賦值給k
}
if(k>=0)//k>=0說明找到一對為升序的相鄰元素
{
l=-1;
for(i=0;i<n;i++)
{
if(a[k]<a[i])l=i;
}
swap(&a[k],&a[l]);//交換下標為k和l的元素
for(i=k+1;i<n;i++)//扭轉索引k+1後面的元素
{
j=n-i+k;
if(i>=j)break;
swap(&a[i],&a[j]);
}
}
if(k==-1)return0;
for(i=0;i<n;i++)printf("%c",a[i]);
printf(" ");
return1;
}
intmain()
{
inti,lens;
charch[10];
printf("Pleaseinputstring(longest10):");
scanf("%s",ch);
lens=strlen(ch);
for(i=0;i<lens;i++)printf("%c",ch[i]);
printf(" ");
while(nextperm(ch,lens));
return0;
}
註:該演算法參考了14世紀印度數學家 納拉亞納潘迪特的思想,
我們偉大的數學家納拉亞納潘迪特的字典序思想原文(英文版的)如下:
-- 國內很多人使用這種技術但他(她)們並不知道該演算法思想的創始者是誰?因為網上所看到的帖子個個都打著原創,其實不然,所以我對此表示很遺憾.因為真正的原創是追溯到14世紀印度數學家 納拉亞納潘迪特
下面是該演算法的英文簡介(目前手頭上只有數學家的英文資料沒有中文的,所以將就著看):
Generation in lexicographic order:
There are many ways to systematically generate all permutations of a given sequence.[16]One classical algorithm, which is both simple and flexible, is based on finding the next permutation inlexicographic ordering, if it exists. It can handle repeated values, for which case it generates the distinct multiset permutations each once. Even for ordinary permutations it is significantly more efficient than generating values for the Lehmer code in lexicographic order (possibly using thefactorial number system) and converting those to permutations. To use it, one starts by sorting the sequence in (weakly)increasingorder (which gives its lexicographically minimal permutation), and then repeats advancing to the next permutation as long as one is found. The method goes back toNarayana Panditain 14th century India, and has been frequently rediscovered ever since.[17]
The following algorithm generates the next permutation lexicographically after a given permutation. It changes the given permutation in-place.
Find the largest indexksuch thata[k] <a[k+ 1]. If no such index exists, the permutation is the last permutation.
Find the largest indexlsuch thata[k] <a[l].
Swap the value ofa[k] with that ofa[l].
Reverse the sequence froma[k+ 1] up to and including the final elementa[n].
For example, given the sequence [1, 2, 3, 4] which starts in a weakly increasing order, and given that the index iszero-based, the steps are as follows:
Indexk= 2, because 3 is placed at an index that satisfies condition of being the largest index that is still less thana[k + 1]which is 4.
Indexl= 3, because 4 is the only value in the sequence that is greater than 3 in order to satisfy the conditiona[k] < a[l].
The values ofa[2]anda[3]are swapped to form the new sequence [1,2,4,3].
The sequence afterk-index a[2]to the final element is reversed. Because only one value lies after this index (the 3), the sequence remains unchanged in this instance. Thus the lexicographic successor of the initial state is permuted: [1,2,4,3].
Following this algorithm, the next lexicographic permutation will be [1,3,2,4], and the 24th permutation will be [4,3,2,1] at which pointa[k] < a[k + 1]does not exist, indicating that this is the last permutation.
⑵ c語言編程練習題1
#include
"stdio.h"
main()
{
double
a=0,b;
int
i;
for(i=1;;i++)
{
printf("Enter
a
number:");
scanf("%lf",&b);
if(b>a)
a=b;
if(b<=0)
break;
}
if(a>=0)
printf("%lf\n",a);
}
經驗證,float數據不夠精確,如輸入100.62,輸出的卻是如100.620003。所以用了更精確的double數據。希望能幫到你。
⑶ c語言多輸入練習
題目中一行中的數據並沒說有多少個,
你定義的a數組只有30個很可能會少
題目中也沒說輸入是100行,行數是可變的,也許不只100行,也許比100行少
我將你的程序作了一些修改:
#include<stdio.h>
void main()
{
//用m存儲最大值,a存儲每次輸入的數字,初始化m為0
int m=0,a;
//由於不知道會輸入的行數,只能採取文件結尾來判斷輸入停止
while(scanf("%d",&a)!=EOF)
{
if(a==0)//當輸入為0的時候輸出最大值
{
printf("%d\n",m);
m=0;
}
else
m=m>a?m:a;//每次m存儲最大值
}
}
⑷ C語言問題 A: A+B 輸入輸出練習I
#include <stdio.h>
int main() {
int a, b;
while ((scanf("%d %d", &a, &b)) != EOF)
printf("%d ", a + b);
return 0;
}