本文目錄一覽:
- 1、C語言DP 問題。。請看圖片。 這題是貪心還是DP? DP 的話 狀態轉移方程是什麼? 請詳細解答。。
- 2、在C語言中,什麼語句是一條限定轉移語句?
- 3、c語言五類單詞的狀態轉換圖?
- 4、怎麼用c語言寫狀態機呀?請舉例說明
- 5、狀態轉換如何用C語言程序實現
- 6、C語言問題,台階問題編程
C語言DP 問題。。請看圖片。 這題是貪心還是DP? DP 的話 狀態轉移方程是什麼? 請詳細解答。。
過了這麼長時間了,也不知道你問題有沒有解決,今天看到你這個題目,覺得你把問題給複雜化了……其實這個題根本不需要用DP,至於貪心,也許存在一個很快的貪心法,可至少我是沒想出來……
我的想法很簡單,從0到m-n,依次求「當以第i個元素為左邊界時,右邊界至少需要定到什麼地方才能保證包含所有的n個數」,然後從這些求出的區段中選擇長度最小的即可。看起來很暴力,但實際上,比如你現在知道了以第0個元素為左邊界時,右邊界至少要定到12,那麼當以第1個元素為左邊界時,右邊界只要從12開始向右滑動進行判斷就可以了,再加上判斷是否包含所有n個數的開銷,演算法的複雜度為O(mn),這個複雜度是完全可接受的。不要在意具體的規模有多大,只要演算法的複雜度合適就可以了。
貼上具體代碼
#include stdio.h
#include stdlib.h
#include string.h
int flag[101],data[100000];
int m,n;
int inline meet()
{
int i;
for(i=1;i=n;++i)
if(!flag[i])
return 0;
return 1;
}
int main()
{
int min;
int c,i,j;
scanf(“%d”,c);
while(c–)
{
memset(flag,0,sizeof(flag));
scanf(“%d%d”,m,n);
for(i=0;im;++i)
scanf(“%d”,data+i);
for(i=0;!meet();++i)
++flag[data[i]];
j=i-1;
min=i;
if(min==n||min==m)
goto next;
for(i=1;i=m-n;++i)
{
–flag[data[i-1]];
if(meet())
{
if(j-i+1min)
{
min=j-i+1;
if(min==n)
goto next;
}
}
else
{
while(++jm)
{
++flag[data[j]];
if(meet())
{
if(j-i+1min)
{
min=j-i+1;
if(min==n)
goto next;
}
break;
}
}
if(j=m)
goto next;
}
}
next:
printf(“%d\n”,min);
}
return 0;
}
這個代碼沒怎麼優化過,但是今天我冒死潛入你們dlut的oj上提交了一下,結果ac了,用了110ms左右吧,時間上好像還可以,你可以再優化一下。
有問題請追問
在C語言中,什麼語句是一條限定轉移語句?
C語言中提供4種轉移語句:
goto,break,
continue和return
1、
return
表示從被調函數返回到主調函數繼續執行,返回時可附帶一個返回值,由return後面的參數指定。
2、goto語句也稱為無條件轉移語句,結構化程序中一般不推薦使用。
3、break語句是一條限定轉移語句,只能用在switch
語句或循環語句中,
其作用是跳出switch語句或跳出本層循環,轉去執行後面的程序。
4、continue語句是一條限定轉移語句,作用是結束本次循環,即不再執行循環體中continue
語句之後的語句,轉入下一次循環條件的判定與執行。
根據你後半句功能描述,應該是continue語句。
c語言五類單詞的狀態轉換圖?
: 無
Table Updated;typename datatype:;
int main()
{
LinkList
tmpString = list: 無
*************************************************/
inFileStream;: CountWord()
Table Accessed; clrIndex++ )
{
counts[clrIndex] = 0: 操作是否成功;/
}
需要鏈表.Locate(index+1); 單詞計數
3; 轉換
while ( inFileStream;typename datatype1/
}
return 0;datatype= 97 ch list / counts[index] .close(), ios; 待檢查的字元
Output****************************
FileName: string str /time:in ); lenth: main()
Table Accessed;OK\
cout : 1;
bool GetInput( LinkList
}
}
cout . GetInput() // 儲存輸入數據的鏈表
Output;fstream: char* filename /: 無
Table Updated;
#include clrIndex
ifstream inFileStream ( inFile !”= 122) )
{
return true, wordList ) )
{
cout /.0 build this moudle
***************************************************/,
CountWord()
Called By: 操作是否成功.Peek().: 讀取輸入流並存入鏈表中
Calls.tmp” readChar!;;’ );n”
if ( : 無
Return: 無
Table Accessed; list );
}
string tmpString;
TransformFile( “!outFileStream || ;*************************************************
Function. CountWord() /*************************************************
Function; endl;
#include “*************************************************
Function;linklist.size(): 單詞計數
Calls;
/, char* outFile ); list )
{
ifstream inFileStream ( filename .get(readChar) )
{
if ( IsValidChar(readChar) )
{
outFileStream
bool IsValidChar( char ch ); strInput; 儲存輸入數據的鏈表
Output: 無
*************************************************/: 無
Input;/= 65 ch , GetInput():out ); index++ )
{
list;;
}
else
{
tmpString += strInput[index], TransformFile(); 將文件轉換為有效的格式
2!”.cpp
Author;
bool GetInput( LinkList list: WordCount: 狀態
Return; , LinkList
cox 2007/: 無
Return: 無
*************************************************/ “: 鏈表類;
int *counts = new int [lenth];
cin
}
cout : IsValidChar()
Called By;
return false: IsValidChar()
Description;;
UpperCase( tmpRead ); list );/typename datatype = ” index 1/Transfroming;
template
//;,自己寫一個吧;/: 操作是否成功,”: 無
Input: 無
Table Updated; “: 文件中指定單詞個數的統計
Version;An error accord dring count;
}
/ )
{
list: 主函數
Calls;
bool IsValidChar( char ch )
{
if ( (ch .Locate(index+1).h”.Lenth(): 將文件去掉標點符號;*************************************************
Function; 待計數的文件
LinkList
while ( inFileStream : 結果
Return;
cout : 無
*************************************************/
inFileStream.close().0
Function List;
template
return true;Input your words to count;
string tmpString = “
}
else
{
return false; wordList. UpperCase() //, “”: char* inFile /\: UpperCase()
Description; index++ )
{
if ( str[index] ‘: 強制轉換成大寫
Calls;
}
}
return true;
tmpString = list: “!”test1: 無
Input!; ” tmpRead )
{
for ( int index = 0, 並按照單詞以空格分組
Calls, ios;iostream;stringdatatypedatatype: 無
Called By; 字元強制轉換為大寫
History;”/
template Bad input;typename datatype.; endl;0’12
Description;test1!CountWord( “datatype
using namespace std: 操作是否成功
Others; index++ )
{
list: 2007/.Lenth(); “: char ch / 檢查是否有效的英語字母
4;
}
if ( : TransformFile()
Description;
}
char readChar = ‘*************************************************
Function, true為成功
Others;
bool TransformFile( char* inFile;
for ( int index = 0, char* outFile )
{
ofstream outFileStream( outFile;
outFileStream; ” 待轉換的字元
Output.Peek();/: TransformFile()
Table Accessed;
}
}
return true; 輸入文件名
char* outFile /: LinkList:in );
cout ;
string tmpRead;, ios: main()
Table Accessed:: 無
*************************************************/
tmpString = ” index
bool TransformFile( char* inFile: GetInput()
Description; 96 )
{
str[index] -= 32: main()
Description; index : 無
Input;
bool CountWord( char* filename:;” tmpString list )
{
string strInput = “: 無
Input; endl;
}
/
bool UpperCase( string str )
{
for ( int index = 0;, true為成功
Others.tmp”: main()
Table Accessed;desc; strInput; list.txt” str;datatype.Insert(tmpString);; endl;
#include
}
}
}
for ( int index = 0!GetInput( wordList ) )
{
cout
bool UpperCase( string str ): 無
Return: 檢查是否有效的英語字元
Calls:
1: 無
Table Updated;string
template
}
/ 從輸入流讀取數據
5: 無
*************************************************/ “
if ( ;;
return true;
bool CountWord( char* filename;/datatype, split by \
#include : 鏈表類
Called By;
strInput += “: 鏈表類. IsValidChar() / 輸出文件名
Output;
for ( int clrIndex = 0!;Open files error;12 1, UpperCase()
Called By: 無
Input: true為是
Others;*************************************************
Function;
int lenth = list;; index
UpperCase( tmpString )!inFileStream )
{
cout ,\: CountWord()
Description; “, true為成功
Others: cox Version : 1: 無
Return. TransformFile() /, LinkList.Lenth(); ” index++ )
{
if ( strInput[index] == ‘.”: 無
Called By: 無
Output;/test1;
}
/.close();/ list /
if ( tmpString == tmpRead )
{
counts[index]++:
: 無
Table Updated;author ;
}
else
{
outFileStream : 無
Table Updated;version \linklist\,’ endl: 0
Others; “= 90) || (ch
}
}
/.size().0 Date
回復:
給你思路,1。一個單詞一個單詞地掃描,每遇到一個空格就算一個單詞
2。利用switch語句,將每個單詞和switch語句中的case的值比較,進行printf替換
回復:
已有的單詞; #define ROWS 256 #define COLS 32 static FILE *fp; void storage(char *pt[];);
puts(“a’n\\).txt」(不包括引號)的文本文件,那麼將視為選項a
int b(int count);;i,a[count];
static char a[ROWS][COLS];stdio.h”---在程序結束之前重新排序存儲數組中的單詞到文件中; #include “.txt”);j, i)) continue, len;);0′, int count) { int i; while(i開始創建詞庫”i,每個單詞的長度不超過31
static FILE *fp;);還要做些什麼;---定義文件指針; MENU “‘ }
return flag; char get_option(void);count;string,會創建一個「word;---接收用戶的選項;);a’),arr);n”
for(i=0;;i++) pt[i]=a[i],再見;);
}
if(check(a[i]. 顯示已有的單詞 b;)!\, int count);i++) {
printf(“開始創建詞庫”:內部鏈接:” } }
puts(“,32*sizeof(char).h”, int count); int check(char arr[]; puts(a[i]);---對輸入的單詞進行分辨;
void c(char *pt[], int count) { int i;words;i在新行輸入END結束輸入, int count);count;);
puts(“.\,count);\i++)
for(j=0;
return ch; break,文件作用域;pt[i][j]; ---為exit()函數提供原型;): “,count;
while((ch=getchar())。若要退出,3)==0) {
count+=i;;
int flag=0; int b(int count);
void storage(char *pt[];count;) { puts(“.h”---字元處理函數原型;)。若輸入「a; ”
for(i=0; start=(int)ftell(fp)/ pt[j]=temp;;; puts(” puts(“,3)==0) {
count=i,stderr);不能打開或建立文件, count);t\。
char get_option(void);string:”!=’,j;ctype;
if(strncmp(a[i].h”; }
if(input==’ int start;t**********************************************************\ }
storage(pt; break; } }
if(input==’strlen(arr);」(不包括引號););
puts(”
puts(“, a[i])==1) {
fflush(stdin); }
int b(int count) { int i; }
void c(char *pt[]!”END”對單詞進行排序;count;);i++) for(j=i+1,0L; char ch;stdio;---字元串處理函數原型;ROWSscanf(“);)
{
puts(“i++)
{
printf(“i
for(i=0,32*sizeof(char); break;) {
while((ch=getchar()),若輸入 ni hao ,c或者d; } } }
int check(char arr[]。 #include ”
for(i=0; }
return count; puts(“); #define ROWS 256
#define COLS 32---定義「字典」的大小;
char *pt[ROWS];t\
if((fp=fopen(”
puts(”
if(strncmp(a[i],請不要點DOS窗口的小叉叉;
char ptr[ROWS][COLS];END”請輸入單詞(每行一個)”
puts(“d’?”n\,j,b, i)) continue,”, int count),start;t*********************歡迎使用字典排版系統*******************\%s”
static char a[ROWS][COLS],然後要求輸入單詞, a[i])==1) {
fflush(stdin); c(pt; puts(pt[i]),fp);重複的單詞;b’ctype.h”ROWS;\?”,SEEK_END););
puts(“,fp)==0) { i=0,並加以處理;
i=count; }
void storage(char *pt[]. 添加新單詞”。
#include “---定義數組,防止誤操作; rewind(fp);i請輸入a;在新行輸入END結束輸入,並存儲到文件中;
int check(char arr[],”i++)
if(strncmp(a[i];n’!=’;該數組的作用是將文件的內容複製進來;c’ }
for(i=0;words。因為程序在結束之前;
for(i=0;count; “. 對已有的單詞進行排序 d; int main(void) {
int i.h”))==NULL) {
fputs(“n”,”i,pt[j]) }
char get_option(void) {
char ch;
return 0;n\;
void c(char *pt[]; flag=1; #include “!”:”).”
while(icount,實際數組元素位置未改變;n” }
fflush(stdin),對數組中的單詞重新排序; count=b(count);d’ char input;i++) if(isalpha(arr[i])==0) {
printf(“);
puts(“,strlen(a[count])+1)==0) {
puts(“c; while((input=get_option()); #include ” pt[i]=pt[j];a; fclose(fp);i:內部鏈接;%s”您要做些什麼;j++) ptr[i][j]=pt[i][j];stdlib;);;stdlib;\, int count);
char *temp.h”.h”
fp=fopen(“---完成選項c的作用--通過指針對數組排序; for(i=0;---完成選項b的作用--接收新單詞;32,count;;a+”) ; i++;
if(fread(a. 退出”。因為處理數組比處理文件方便;) {
puts(“:可存放256個單詞; } i++;---
#include “||ch exit(1); }
fseek(fp; rewind(fp);)
{
if(input==’!=’,輸入d即可, count),文件作用域;%s不是一個單詞; #include “‘);n”0) {
temp=pt[i];ROWSscanf(“, int count),並且提示並剔除重複的單詞;謝謝使用;w+” flag=1程序第一次運行時.txt”, int count) { int i,將視為單詞 ni ; } }
puts(“j++) {
if(strcmp(pt[i];
puts(”
c(pt;請輸入新的單詞(每行一個)” #include ” count=start,”
fwrite(ptr; if(check(a[i]
回復:
#includestdio.h
#includestring.h
void main()
{
char str[30];
scanf(“%s”,str);
if(strcmp(str,”em”)==0)
printf(“them\n”);
else if(strcmp(str,”cuz”)==0)
printf(“because\n”);
else if(strcmp(str,”gratz”)==0)
printf(“grateful\n”);
else if(strcmp(str,”i”)==0)
printf(“I\n”);
else if(strcmp(str,”nah”)==0)
printf(“no\n”);
else if(strcmp(str,”pos”)==0)
printf(“them\n”);
else if(strcmp(str,”sez”)==0)
printf(“supoosed\n”);
else if(strcmp(str,”tanz”)==0)
printf(“thanks\n”);
else if(strcmp(str,”wuz”)==0)
printf(“was\n”);
else
printf(“cannot find any words\n”);
}//試驗過了
回復:
for(int i = 0, string[j]) ;比較字元串大小, string[i]) , string[j]) == 1)//:\
int main()
{
char string[10][50].h i++)
scanf(” i
strcpy(string[j]; j++)
if(strcmp(string[i]; 5; i++ )
for(int j = i+1.h*冒泡排序*/
#include , temp) ;
for(int i = 0; j ); i
}
/
}
PS;/);
for(int i = 0;
printf(“, temp[50];
/string; 6;stdio;
return 0;n” 6:\/ i++ )
puts(string[i]),可以用strcmp
{
strcpy(temp;%s”輸出排好序的6個單詞;/輸出
printf(“;n”請輸入6個單詞;交換要strcpy
strcpy(string[i]; 6, string[i]); i #include :若有不明白的地方
怎麼用c語言寫狀態機呀?請舉例說明
c語言寫狀態機之前:
1、確定一共有多少種狀態,這裡的狀態有開和關,細分還有say thankyou 和警告
2、確定狀態之間的遷移條件
如果按照四種狀態:開、關、說謝謝、警告,那麼這四種狀態之前的遷移條件很明顯了
分兩個函數:
1、檢查是否需要遷移狀態;
2、遷移狀態.
遍歷各種狀態檢查是否有狀態需要發生遷移.一般用一個switch將各種狀態列出,然後在各種狀態裡面用if檢查是否需要遷移狀態,如果需要遷移,做好標記.
再次遍歷各種狀態,檢查哪些狀態做了標記,遷移到新狀態,並做相應的操作,比如進入關的時候,做關門動作。
典型的狀態機結構:
enum { state_A, state_B, state_C } state = state_A;
while(1)
{
switch ( state )
{
case state_A:
if ( event_A ) // 這裡也可以用switch
{
action_1(); // 在某狀態下發生某事件執行某個動作,並轉入下個狀態
state = state_B;
}
else if ( event_B )
{
}
else
{
}
break;
case state_B:
… …
}
}
狀態轉換如何用C語言程序實現
就是說a如果成立那狀態就可以由1直接到3了
int state = 0;
if(a)
{
state = 3;
if(b)
{
state = 2;
}
}
C語言問題,台階問題編程
#include stdio.h
int fuck(int n, int m)
{
if(n == 1 || n == 0) return 1;
if(n 0) return 0;
int total = 0;
int i = 1;
for(; i m; ++i)
{
total += fuck(n – i, m);
}
return total;
}
int main()
{
int a;
scanf(“%d”, a);
int n[a];
int i;
for(i = 0; i a; ++i)
{
scanf(“%d”, n[i]);
}
int b;
scanf(“%d”, b);
int m[b];
for(i = 0; i a; ++i)
{
scanf(“%d”, n[i]);
}
for(i = 0; i a i b; ++i)
{
printf(“%d”, fuck(n[i], m[i]));
}
return 0;
}
沒編譯器,你先編譯試試,應該沒錯
望採納~
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/301049.html