本文目錄一覽:
- 1、c語言如何實現字符串按位翻轉
- 2、用C語言,翻轉字符串,(例如輸入「ABCD」,則輸出「DCBA」)!方法越多越好!!
- 3、C語言 圖形翻轉“
- 4、C語言的矩陣翻轉
- 5、怎樣用c語言表示 翻轉的數 比如把81 翻轉為18
c語言如何實現字符串按位翻轉
字符串按位翻轉可以通過對每個字符按位翻轉實現,首先通過指針偏移或數組下標依次取出字符,然後對單個字符做按位非操作,即~
用C語言,翻轉字符串,(例如輸入「ABCD」,則輸出「DCBA」)!方法越多越好!!
【方法一】
//也是最簡單的有點投機
#include stdio.h
int main(){
char str[]=”ABCD”;
int i;
for(i=3;i=0;i–) printf(“%c”,str[i]);
return 0;
}
【方法二】
/*
用指針的,將第一個與最後一個互換,第二個與倒數第二個互換……直到逆置完成
*/
#include stdio.h
#include string.h
int main(){
char str[]=”ABCD”,*start,*end,t;
int len,i;
len=strlen(str);
start=str;
end=str[len-1];
while(startend){
t=*start;
*start=*end;
*end=t;
start++;
end–;
}
printf(“REVERSED:\n”);
for(i=0;str[i];i++) printf(“%c”,str[i]);
return 0;
}
【方法三】
//用堆棧的性質來實現
#include stdio.h
#include string.h
#include stdlib.h
#define MAXSTACK 100
char pop(struct stack *stk);
void push(char ch,struct stack *stk);
int empty(struct stack *stk);
struct stack{
int top;
char str[MAXSTACK];
};
int main(void)
{
int i,len_str;
char str[MAXSTACK];
struct stack s;
s.top=-1;
printf(“\nInput the string please:”);/*輸入你要逆置的字符串e.g.「ABCD」*/
gets(str);
len_str=strlen(str);
for(i=0;ilen_str;i++)
push(str[i],s);
printf(“The disorder is:”);
for(i=0;ilen_str;i++){
if(!empty(s))
printf(“%c”,pop(s));
}
getch();
return 0;
}
char pop(struct stack *stk){
return stk-str[stk-top–];
}
void push(char ch,struct stack *stk){
if(stk-top==MAXSTACK-1){
printf(“\nOVERFLOW!\n”);
exit(1);
}
else stk-str[++(stk-top)]=ch;
return;
}
int empty(struct stack *stk){
return (stk-top==-1);
}
還可以用遞歸的思想來解決該問題,這裡就不多說了……
C語言 圖形翻轉“
#includestdio.h
main()
{
int a[100][100],i,j,n,m,t,s,k;
scanf(“%d %d %d”,m,n,t);//M為行 N為列 T為翻轉的方式
for(i=0;im;i++)
{ for(j=0;jn;j++)
scanf(“%d”,a[i][j]);
}
if(t==1)
{
for(s=0;si/2;s++)
for(j=0;jn;j++)
{
k=a[i-s-1][j];
a[i-s-1][j]=a[s][j];
a[s][j]=k;
}
}
if(t==-1)
{for(s=0;sj/2;s++)
for(i=0;im;i++)
{
k=a[i][j-s-1];
a[i][j-s-1]=a[i][s];
a[i][s]=k;
}
}
for(i=0;im;i++)
{ for(j=0;jn;j++)
printf(” %d”,a[i][j]);
printf(“\n”);
}
}
C語言的矩陣翻轉
#include stdio.h
int main() {
int M,N,T;
scanf(“%d %d %d”,M,N,T);
int mat[M][N];
printf(“請輸入原始矩陣:\n”);
for(int i=0; iM; i++) {
for(int j=0; jN; j++) {
scanf(“%d”,mat[i][j]);
}
}
printf(“原矩陣為:\n”);
for(int i=0; iM; i++) {
for(int j=0; jN; j++) {
printf(“%d “,mat[i][j]);
}
printf(“\n”);
}
int temp = 0;
if(T==0) {
printf(“左右翻轉後:\n”);
for(int i=0; iM; i++) {
for(int j=0; jN/2; j++) {
temp = mat[i][j];
mat[i][j] = mat[i][N-j-1];
mat[i][N-j-1] = temp;
}
}
for(int i=0; iM; i++) {
for(int j=0; jN; j++) {
printf(“%d “,mat[i][j]);
}
printf(“\n”);
}
}
if(T==1) {
printf(“上下翻轉後:\n”);
for(int i=0; iM/2; i++) {
for(int j=0; jN; j++) {
temp = mat[i][j];
mat[i][j] = mat[N-i-1][j];
mat[N-i-1][j] = temp;
}
}
for(int i=0; iM; i++) {
for(int j=0; jN; j++) {
printf(“%d “,mat[i][j]);
}
printf(“\n”);
}
}
return 0;
}
怎樣用c語言表示 翻轉的數 比如把81 翻轉為18
用c語言表示翻轉的數可以參考下面的代碼:
#include stdio.h
int main()
{int C,D,S;
scanf(“%d”,C);
D=(C%10)*10+C/10;
S=C+D;
printf(“S=%d”,S);
}
擴展資料:
scanf()是C語言中的一個輸入函數。
與printf函數一樣,都被聲明在頭文件stdio.h里,因此在使用scanf函數時要加上#include stdio.h。
(在有一些實現中,printf函數與scanf函數在使用時可以不使用預編譯命令#include stdio.h。)它是格式輸入函數,即按用戶指定的格式從鍵盤上把數據輸入到指定的變量之中。
參考資料來源:百度百科-scanf (計算機語言函數)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/155492.html